project point to line
Created by: Durman
We have a line that consists with many edges and we have one or several points. The goal is to find find closest to given point (or points) point lying on the line. I made small algorithm that can reach this goal.
The algorithm can be divided on two parts. First part consist from finding closest point of the line to given point. I used KDTree from mathutils but faced with next problem.
Unexpected solution of this problem was to subdivide the line to smaller edges. I expect that this approach can gives wrong result time to times but it should work in most cases.
By default subdivision is equal to smallest edge in the line but also I made parameter that don't let to default subdivision have too small value.
Second part of the algorithm to find projection given point to edges that coinciding with found point on the line. For this task there are all that we need in mathutils. Product vector to vector and scalar multiplication of vectors. The scalar multiplication gives as a sign that says projected vector has same direction or not with another vector.
And final result.