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 an开发者_如何学Pythony other way of determining collinearity of three points in iOS Thanks arnieterm
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
}
精彩评论