I have two given CGPoints A and B and one another CGPoint C as obtained from within touchesEnded event. I want to determine whether the three points lie in a straight line for which I have used the below given formula: For point A(x1, y1), B(x2, y2) and C(x3, y3) x1(y2 - y3) + x2 (y3-y1) + x3(y1-y2) = 0 But the formula doesn't helpful at all. is there any other way of determining collinearity of three points in iOS Thanks arnieterm
Asked
Active
Viewed 883 times
0
-
2possible duplicate of [How can you determine a point is between two other points on a line segment?](http://stackoverflow.com/questions/328107/how-can-you-determine-a-point-is-between-two-other-points-on-a-line-segment) – DarkDust Jul 22 '11 at 14:26
-
Duplicate question is for Python, however the math is the same (and even the code is almost the same). – DarkDust Jul 22 '11 at 14:27
1 Answers
1
This is not really an iOS question, or even a programming question -- it's an algorithm question.
I'm not sure if the algorithm you were given is correct -- see the cross-product answer given in comments for what I think of as the correct answer. In what way do you find your formula not helpful? Here it is in code, btw. (code typed in browser, not checked):
CGPoint p1;
CGPoint p2;
CGPoint p3;
const float closeEnough = 0.00001; // because floats rarely == 0
float v1 = p1.x * (p2.y - p3.y);
float v2 = p2.x * (p3.y - p1.y);
float v3 = p3.x * (p1.y - p2.y);
if (ABS(v1 + v2 + v3) < closeEnough)
{
// your algorithm is true
}

Olie
- 24,597
- 18
- 99
- 131
-
This formula will only work when the three points are in straight line. In my case the user touch point does not appear exactly inline with start and end points. – Parvez Qureshi Jul 25 '11 at 06:13
-
If instead I attempt to find angle between these 3 points. Will it helps? The problem is user is tapping on a set of points laid out in a matrix form. Even if I consider the angle then what must be the range of angles needs to considered? – Parvez Qureshi Jul 25 '11 at 13:04