public gEdge(gVertex start, gVertex end) { StartVertex = start; EndVertex = end; Length = StartVertex.DistanceTo(EndVertex); Direction = gVector.ByTwoVertices(StartVertex, EndVertex); }
public bool ContainsVertex(gVertex vertex) { gVertex maxVertex = vertices.OrderByDescending(v => v.DistanceTo(vertex)).First(); double maxDistance = vertex.DistanceTo(maxVertex) * 1.5; gVertex v2 = gVertex.ByCoordinates(vertex.X + maxDistance, vertex.Y, vertex.Z); gEdge ray = gEdge.ByStartVertexEndVertex(vertex, v2); gVertex coincident = null; int windNumber = 0; foreach (gEdge edge in edges) { gBase intersection = ray.Intersection(edge); if (edge.StartVertex.Y <= vertex.Y) { if (edge.EndVertex.Y > vertex.Y && intersection != null && intersection.GetType() == typeof(gVertex)) { ++windNumber; } } else { if (edge.EndVertex.Y <= vertex.Y && intersection != null && intersection.GetType() == typeof(gVertex)) { --windNumber; } } } //If intersections is odd, returns true, false otherwise //return (intersections % 2 == 0) ? false : true; return(windNumber != 0); }
internal gPolygon AddVertex(gVertex v, gEdge intersectingEdge) { //Assumes that vertex v intersects one of polygons edges. gPolygon newPolygon = (gPolygon)this.Clone(); // Assign the polygon Id to the new vertex. v.polygonId = this.id; // Getting the index of the intersecting edge's start vertex and // inserting the new vertex at the following index. int index = newPolygon.vertices.IndexOf(intersectingEdge.StartVertex); newPolygon.vertices.Insert(index + 1, v); // Rebuilding edges. newPolygon.edges.Clear(); int verticesCount = newPolygon.vertices.Count; for (var i = 0; i < verticesCount; i++) { int nextIndex = (i + 1) % verticesCount; newPolygon.edges.Add(new gEdge(newPolygon.vertices[i], newPolygon.vertices[nextIndex])); } return(newPolygon); }
public static gVector ByTwoVertices(gVertex start, gVertex end) { var x = end.X - start.X; var y = end.Y - start.Y; var z = end.Z - start.Z; var length = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2)); return(new gVector(x, y, z, length)); }
public static gPolygon ByVertices(List <gVertex> vertices, bool isExternal) { gPolygon polygon = new gPolygon(-1, isExternal); polygon.vertices = vertices; int vertexCount = vertices.Count; for (var j = 0; j < vertexCount; j++) { int next_index = (j + 1) % vertexCount; gVertex vertex = vertices[j]; gVertex next_vertex = vertices[next_index]; polygon.edges.Add(new gEdge(vertex, next_vertex)); } return(polygon); }
/// <summary> /// Method to return the other end vertex of the gEdge /// </summary> /// <param name="vertex"></param> /// <returns></returns> public gVertex GetVertexPair(gVertex vertex) { return((StartVertex.Equals(vertex)) ? EndVertex : StartVertex); }
/// <summary> /// gEdge constructor by line /// </summary> /// <param name="line">line</param> /// <returns name="edge">edge</returns> //public static gEdge ByLine(Line line) //{ // gVertex start = gVertex.ByCoordinates(line.StartPoint.X, line.StartPoint.Y, line.StartPoint.Z); // gVertex end = gVertex.ByCoordinates(line.EndPoint.X, line.EndPoint.Y, line.EndPoint.Z); // return new gEdge(start, end); //} #endregion /// <summary> /// Method to check if vertex belongs to edge /// </summary> /// <param name="vertex"></param> /// <returns></returns> public bool Contains(gVertex vertex) { return(StartVertex.Equals(vertex) || EndVertex.Equals(vertex)); }
/// <summary> /// gEdge constructor by start and end vertices /// </summary> /// <param name="start">Start vertex</param> /// <param name="end">End gVertex</param> /// <returns name="edge">edge</returns> public static gEdge ByStartVertexEndVertex(gVertex start, gVertex end) { return(new gEdge(start, end)); }
public double DistanceTo(gVertex vertex) { return(vertex.DistanceTo(this)); }
public gBase Intersection(gEdge edge) { // http://mathworld.wolfram.com/Line-LineIntersection.html if (!this.IsCoplanarTo(edge)) { return(null); } if (edge.Contains(this.StartVertex)) { return(StartVertex); } if (edge.Contains(this.EndVertex)) { return(EndVertex); } var a = this.Direction; var b = edge.Direction; var c = gVector.ByTwoVertices(this.StartVertex, edge.StartVertex); var cxb = c.Cross(b); var axb = a.Cross(b); var dot = cxb.Dot(axb); // if dot == 0 it means that they are parallels if (Threshold(dot, 0)) { //Fully contains the test edge if (edge.StartVertex.OnEdge(this) && edge.EndVertex.OnEdge(this)) { return(edge); } else if (this.StartVertex.OnEdge(edge) || this.EndVertex.OnEdge(edge)) { gVertex[] vertices = new gVertex[4] { this.StartVertex, this.EndVertex, edge.StartVertex, edge.EndVertex }; var sorted = vertices.OrderBy(v => v.Y).ThenBy(v => v.X).ThenBy(v => v.Z).ToList(); return(gEdge.ByStartVertexEndVertex(sorted[1], sorted[2])); } else { return(null); } } double s = (dot) / Math.Pow(axb.Length, 2); // s > 1, means that "intersection" vertex is not on either edge // s == NaN means they are parallels so never intersect if (s < 0 || s > 1 || Double.IsNaN(s)) { return(null); } gVertex intersection = this.StartVertex.Translate(a.Scale(s)); if (intersection.Equals(edge.StartVertex)) { return(edge.StartVertex); } if (intersection.Equals(edge.EndVertex)) { return(edge.EndVertex); } if (!intersection.OnEdge(edge)) { return(null); } return(intersection); }
internal void AddVertex(gVertex vertex) { vertex.polygonId = this.id; vertices.Add(vertex); }