Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to get the shortest distance from a point in space to a line segment?

Discussion in 'Scripting' started by ali_aboshady, Apr 18, 2021.

  1. ali_aboshady

    ali_aboshady

    Joined:
    Mar 26, 2017
    Posts:
    3
    I have 2 lines and a point in space. I want to see which line the point is on. So a way to do this, is to get the distances from the point to each line and compare them. How to do that?
    Also, how to get the coordinates of the intersection point of the green line and the black line?
    Thanks
    Untitled.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  3. ali_aboshady

    ali_aboshady

    Joined:
    Mar 26, 2017
    Posts:
    3
    Hmmm search on google? What a nice and new idea! Wish I thought of that before coming here.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    Well, if you did, you would have found things like this for example. You can adapt a lot of things from there. And if you only need this in the editor, you don't have to adapt at all, just use these functions.
     
    ali_aboshady likes this.
  5. ali_aboshady

    ali_aboshady

    Joined:
    Mar 26, 2017
    Posts:
    3
    Thank you this is really helpful. And I did search on google A LOT and read many threads but didn't find exactly what I was looking for.
     
  6. chengwang2077

    chengwang2077

    Joined:
    Nov 23, 2019
    Posts:
    131
    I seem to have written this function before, you can refer to it
    Code (CSharp):
    1.         public static Vector3 NearestPoint(Line line, Vector3 pos)
    2.         {
    3.             var dotProduct1 = DotProduct((line.endpoint2 - line.endpoint1), (pos - line.endpoint1));
    4.             if (dotProduct1 <= 0) return line.endpoint1;
    5.             var dotProduct2 = DotProduct((line.endpoint1 - line.endpoint2), (pos - line.endpoint2));
    6.             if (dotProduct2 <= 0) return line.endpoint2;
    7.             var projection = dotProduct1 / line.Length;
    8.             return line.endpoint1 + (line.endpoint2 - line.endpoint1).normalized * projection;
    9.         }
    10.  
    11.         public static float DotProduct(Vector3 v1, Vector3 v2)
    12.         {
    13.             return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
    14.         }
    Code (CSharp):
    1.     public class Line
    2.     {
    3.         public Vector3 endpoint1;
    4.         public Vector3 endpoint2;
    5.  
    6.         public Line(Vector3 endpoint1, Vector3 endpoint2)
    7.         {
    8.             this.endpoint1 = endpoint1;
    9.             this.endpoint2 = endpoint2;
    10.         }
    11.  
    12.         public float Length => (endpoint2 - endpoint1).magnitude;
    13.     }