Stewart Stoakes
Stewart Stoakes

Reputation: 847

How to tell if a point is part of a line?

Given a line define by two points (A,B) how can I tell if another Point (C) is on that line or not, does WPF have a built in function as Im using paths to represent the lines or will I have to write a function. If the latter is true any help would be appreciated, thanks.

Upvotes: 0

Views: 375

Answers (1)

Mike Dinescu
Mike Dinescu

Reputation: 55720

Three points are co-linear if the angle they form is 180 degrees. Or stated a different way, the slope of the line segments AB, AC and BC are all equal.

Solving for slopes: [AB].y/[AB].x = [AC].y/[AC].x gives the following nice function:

bool AreCollinear(Point A, Point B, Point C) 
{
    double slopesDelta = (A.y - B.y) * (A.x - C.x) - (A.y - C.y) * (A.x - B.x);
    double tolerance = 1e-6;                // substitute for your own tolerance
    return tolerance > Math.Abs(slopesDelta);
}

Upvotes: 2

Related Questions