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?
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?
You can use a [convex-hull-algorithm][1] 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*, hull[i+1]);*
}*
Gizmos.DrawLine(hull[0], hull[hull.Count - 1]);*
}*
}*
private List ConvexHull(List 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*.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*, 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;*
* }*
}