public SimplePolygon(Vector2[] points) { if (points.Length < 3) throw new ArgumentException("Insufficent vertices."); var face = new Face(); Vertex firstVertex = new Vertex(points[0]); this.firstEdge = new HalfEdge(); firstEdge.Origin = firstVertex; firstEdge.Face = face; face.ConnectedEdge = firstEdge; var previousEdge = firstEdge; foreach (var point in points.Skip(1)) { var edge = new HalfEdge(); edge.Origin = new Vertex(point); edge.Face = face; edge.Previous = previousEdge; previousEdge.Next = edge; previousEdge = edge; } firstEdge.Previous = previousEdge; previousEdge.Next = firstEdge; if (!PolygonIsSimpleAndHasSortedVertices()) throw new ArgumentException("Polygon is not simple."); }
public bool Intersects(HalfEdge other) { Vector2 u0 = this.Origin.Position, u = this.Vector, v0 = other.Origin.Position, v = other.Vector; double crossProductZ = Vector2.Cross(u, v); if (crossProductZ == 0) return false; double k = Vector2.Cross(v0 - u0, u) / crossProductZ, l = Vector2.Cross(v0 - u0, v) / crossProductZ; if (k <= 0 || k >= 1 || l <= 0 || l >= 1) return false; return true; }
public Vector2 GetIntersection(HalfEdge other) { Vector2 u0 = this.Origin.Position, u = this.Vector, v0 = other.Origin.Position, v = other.Vector; double crossProductZ = Vector2.Cross(u, v); if (crossProductZ == 0) throw new ArgumentException("Edges are parallel."); double k = Vector2.Cross(v0 - u0, u) / crossProductZ, l = Vector2.Cross(v0 - u0, v) / crossProductZ; if (k <= 0 || k >= 1 || l <= 0 || l >= 1) throw new ArgumentException("Edges do not intersect each other."); Debug.Assert(u0 + k * u == v0 + l * v); return u0 + k * u; }
protected HalfEdge GetNextEdge(HalfEdge current) { try { return current.Next; } catch (NullReferenceException) { throw new ArgumentNullException("Edges are not closing."); } }
public HalfEdgeNode(HalfEdge edge) { this.edge = edge; }