mozman / ezdxf

Python interface to DXF

Home Page:https://ezdxf.mozman.at

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

is_point_on_line_2d

grzegorzswitek opened this issue · comments

Describe the bug
If I understand the assumptions of the 'is_point_on_line_2d' function correctly, the abs_tol parameter should specify the maximum distance from the line that a point can be in order for the test to be successful..? If so, the function works incorrectly for abs_tol != 0

To Reproduce

from ezdxf.math import Vec2, is_point_on_line_2d

start = Vec2(0, 0, 0)
end = Vec2(100, 0, 0)
point = Vec2(50, 0.1, 0)

test = is_point_on_line_2d(point, start, end, abs_tol=0.2)  # test = False

As I recall the function is just to test if the point is on the line and takes a shortcut to improve speed by not calculating the actual distance from the line. Therefore, abs_tol is only used to compare against zero and should be a very small value. To get the actual distance, use the distance_point_line_2d() function from the same module.

the distance function:

distance = math.fabs((start - point).det(end - point)) / (end - start).magnitude

the test for on line:

    on_line = (
        math.fabs(
            (end_y - start_y) * point_x
            - (end_x - start_x) * point_y
            + (end_x * start_y - end_y * start_x)
        )
       <= abs_tol
    )

This looks different but is basically the same, except for the division by then magnitude.
The calculation of the magnitude is the expensive part.

Thank you. By the way - good job.