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 Getting closest point on surface of a polygon

Discussion in 'Scripting' started by SpiderJones, Oct 7, 2021.

  1. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    224
    Hi,

    I have an array of Vector3 objects (Vector3[]) that form a polygon. And I have a point in space (Vector3). Can someone point me to a solution for finding the closest point on the polygon to the point in space? I have several polygons and I want to find the closest one to the point in space.

    Thanks!
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    It isn't exactly a trivial problem. First you need to determine if the point is inside the polygon, which means you need to know what constitutes the inside of the polygon. Then, if the point isn't inside the polygon, you can do a point-line distance to find the closest point on each edge of the polygon to find the closest line, and the point on that line.

    It's been awhile since I used it, but I recall the Triangle.Net library had methods to generate polygons from points. I assume it would also have some methods to get closest point on the polygon to that point.
     
  3. yukunlinykl

    yukunlinykl

    Joined:
    Dec 17, 2021
    Posts:
    21