How to determine whether the point lies on a straight line. I will specify - straight thick, that is, in fact, it is an inclined rectangle.

I tried using the formula taken from this question and with a small error.

In the case of oblique lines, everything works. But if the line is horizontal or vertical, then the calculation using this formula is equal to Infinity . And if there are several straight lines, then, accordingly, it is impossible to determine on which of them the point is located

Please tell me the algorithm.

    1 answer 1

    The easiest way to use the pseudoscalar product.

    If the line is given by two points A(x1, y1) and B(x2, y2) , and the third point C(x, y) , then it is necessary to calculate the value

     dx1 = x2 - x1; dy1 = y2 - y1; dx = x - x1; dy = y - y1; S = dx1 * dy - dx * dy1; 

    This value is equal to the double (oriented) area of ​​the triangle, composed of three points, and is zero if the three points lie on one straight line.

    It works, obviously, regardless of the verticality / horizontal straight lines.


    You can calculate the distance from point C to line AB by dividing by the length of the segment AB . The values ​​of dx1 , dy1 and |AB| = Math.Sqrt(dx1 * dx1 + dy1 * dy1) |AB| = Math.Sqrt(dx1 * dx1 + dy1 * dy1) can be calculated in advance.

    If your straight line has a given thickness d , you can compare with it:

     ab = Math.Sqrt(dx1 * dx1 + dy1 * dy1); h = S / ab; if (Math.Abs(h) < d/2) { // точка лежит на толстой прямой 
    • I have already tried this method - a link to the solution in the description. My line is thick, and in fact it is an inclined rectangle. And if I use the error, then there are problems with vertical / horizontal lines - DOC_tr
    • @DOC_tr: This is not the method you are referring to. This method never gets infinity. - VladD
    • Yes you are right. There is no infinity. And setting an error of 1000 I was able to catch a click on the desired line. Thanks - DOC_tr
    • @DOC_tr: Please! - VladD
    • @DOC_tr: You can calculate the distance from a point to a straight line. Added in response. - VladD