Search code examples
linetranslationpointsegment

How to find this specific ratio of a point between two line segments?


enter image description here

I have two line segments, defined by points AB and CD and a point P, somewhere inside the irregular square ABDC. I need to find a ratio going from 0 to 1, depending on where point P lies between lines AB and CD. If P lies anywhere on AB it should be 0, if it lies anywhere on CD it should be 1. In the illustration it should be around 0.55 to 0.6 I suppose.

If the two lines were parallel I could just measure the distance to both lines, so it would be easy.

I've tried playing around with all kinds of angles and distances between the various points, but I can't seem to find any constant which I could base my function off of.

My guess is that I have to somehow translate line AB into CD and find the point in the "translation progress" where P lies on that "translating" line, exactly at this moment of the translation, if that makes sense..

I struggle to properly explain this problem, but I hope this was somewhat understandable.


Solution

  • Like Strawberry suggested, I have to calculate the convergence point of AB and CD. Call this point X, then calculate the intersection point of PX with BD and measure the distance from that intersection point to B. This distance divided by the length of BD is exactly the ratio I was looking for!