Longest distance between straight line and a curve.
Hello everyone.
I´ve read a lot of questions that were "similar" to mine but no one has been good for me. The thing is that i want to know the longest distance from a curve and a straight line. The curve line is the touch input of the user, every point, and the straight line is from the start point to the end point.
What´s the best way to achieve that?
Thank you.
If it's a random curve drawn by the user, i don't see any other way than calculating the distance for every single point.
Answer by hexagonius · Jan 28, 2017 at 02:51 PM
I can think of two solutions to this. Since you're curve is created from touch points of the user, we look for the farthest point to your start-end vector:
For each point, get the vector from start to it. Then use Vector3.project to scale the start-end vector so the connection between it's tip and the tip of the first vector form a perpendicular vector. the lenght of this vector is what you need to compare when doing this with each point. (See this link for Vector3.project to see what I mean).
When the user has finished his drag, place a transform at the start with it's up vector pointing towards the end and it's forward vector into the scene (hopefully the view is 2D :) ). Then transform each point into it's local coordiante system (transform.InverseTransformPoint). The x value of the transformed vector is the distance from the start-end vector to the point transformed.