/// <summary> /// Removes any colinear vertices in the polygon /// </summary> public void Simplify() { // Remove colinear vertices for (var i = 0; i < Vertices.Count - 2; i++) { var v1 = Vertices[i]; var v2 = Vertices[i + 2]; var p = Vertices[i + 1]; var line = new Line(v1, v2); // If the midpoint is on the line, remove it if (line.ClosestPoint(p).EquivalentTo(p)) { Vertices.RemoveAt(i + 1); } } }
/// <summary> /// Creates a polygon from a list of points /// </summary> /// <param name="vertices">The vertices of the polygon</param> public Polygon(IEnumerable <Vector3> vertices) { var verts = vertices.ToList(); // Remove colinear vertices for (var i = 0; i < verts.Count - 2; i++) { var v1 = verts[i]; var v2 = verts[i + 2]; var p = verts[i + 1]; var line = new Line(v1, v2); // If the midpoint is on the line, remove it if (line.ClosestPoint(p).EquivalentTo(p)) { verts.RemoveAt(i + 1); } } Vertices = verts; }
public DisplacementPoint GetClosestDisplacementPoint(Line line) { return GetPoints() .OrderBy(x => (x.Location - line.ClosestPoint(x.Location)).LengthSquared()) .FirstOrDefault(); // This way is more accurate, but too slow. /* // First, do an intersection test on the triangles and get the closest one var tri = GetTriangles() .Select(triangle => new { triangle, point = GetIntersectionPoint(triangle.Select(x => x.Location).ToList(), line) }) .Where(x => x.point != null) .OrderBy(x => (line.Start - x.point).LengthSquared()) .Select(x => x.triangle) .FirstOrDefault(); // If we found an intersect, return the point that is closest to the line if (tri != null) { var coord = tri .OrderBy(x => (x.Location - line.ClosestPoint(x.Location)).LengthSquared()) .First(); var ret = GetPoints().FirstOrDefault(x => x.Location == coord.Location); // Null check for sanity - shouldn't ever have a triangle without a matching point if (ret != null) return ret; } // If we didn't find an intersect, just return the point with the shortest distance from the line return GetPoints() .OrderBy(x => (x.Location - line.ClosestPoint(x.Location)).LengthSquared()) .FirstOrDefault(); */ }