- Home /
 
 
               Question by 
               gabrieltva · Mar 12, 2018 at 10:54 PM · 
                c#mathgeometry  
              
 
              Get / Calculate edges of a 3D object - C #
I need to get all vertices in the edge position of a determinate object 3D (in this case a path).
See the image: 
My question is, how can I get only the edges vertices, like the image?
 
                 
                questao-vertices-en.png 
                (22.0 kB) 
               
 
              
               Comment
              
 
               
              Answer by Xarbrough · Mar 13, 2018 at 01:54 AM
You can use a convex-hull-algorithm to find the outer edge of a point set. There are a few to pick from, but here's an example of Jarvis march:
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class FindConvexHull : MonoBehaviour 
 {
     public List<Vector2> points;
     public float gizmoSize = 0.1f;
 
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.white;
         foreach(var point in points)
             Gizmos.DrawSphere(point, gizmoSize);
 
 
         var hull = ConvexHull(points);
         if(hull != null)
         {
             Gizmos.color = Color.green;
             for (int i = 0; i < hull.Count - 1; i++) 
             {
                 Gizmos.DrawLine(hull[i], hull[i+1]);
             }
             Gizmos.DrawLine(hull[0], hull[hull.Count - 1]);
         }
     }
 
     private List<Vector2> ConvexHull(List<Vector2> points)
     {
         List<Vector2> hull = new List<Vector2>();
 
         if(points.Count < 3)
             return hull;
 
         int leftMost = 0;
         for (int i = 0; i < points.Count; i++) 
         {
             if(points[i].x < points[leftMost].x)
                 leftMost = i;
         }
 
         int pointA = leftMost;
         int pointB;
         do
         {
             hull.Add(points[pointA]);
             pointB = (pointA + 1) % points.Count;
 
             for (int i = 0; i < points.Count; i++) 
             {
                 if(GetTurn(points[pointA], points[i], points[pointB]) == 2)
                     pointB = i;
             }
 
             pointA = pointB;
         } 
         while (pointA != leftMost);
 
         return hull;
     }
 
     private float GetTurn(Vector2 a, Vector2 b, Vector2 c)
     {
         var val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
 
         if (val == 0) 
             return 0;
 
         if (val > 0)
             return 1;
 
         else
             return 2;
     }
 }
 
 
              Your answer