public void AddEdge(T from, T to) { if (from == null) { throw new ArgumentNullException(nameof(from)); } if (to == null) { throw new ArgumentNullException(nameof(to)); } if (!Vertices.Contains(from, EqualityComparer)) { throw new ArgumentException($"Cannot add edge from nonexistant vertex {from}"); } if (!Vertices.Contains(to, EqualityComparer)) { throw new ArgumentException($"Cannot add edge to nonexistant vertex {to}"); } if (Edges.Any(edge => EqualityComparer.Equals(edge.From, from) && EqualityComparer.Equals(edge.To, to))) { throw new ArgumentException($"Edge from vertex {from} to vertex {to} already exists"); } _edges.Add(new Edge <T>(from, to)); }
public void AddVertex(IVertex <V> vertex) { if (!Vertices.Contains(vertex)) { Vertices.Add(vertex); } }
public void AddVertex(Vertex <T> vertex) { if (vertex != null && vertex.NeighboringVertices.Count == 0 && !Vertices.Contains(vertex)) { Vertices.Add(vertex); } }
public void AddVertex(ILinkedVertex <V, E> vertex) { if (!Vertices.Contains(vertex)) { Vertices.Add(vertex); } }
public override bool RemoveVertex(DVertex <T> vertex) { if (!Vertices.Contains(vertex)) { return(false); } for (int i = 0; i < Edges.Count; i++) { if (Edges[i].StartingPoint == vertex) { Edges.RemoveAt(i); i--; } else if (Edges[i].EndingPoint == vertex) { Edges[i].StartingPoint.Neighbors.RemoveAt(Edges[i].StartingPoint.Neighbors.FindIndex((a) => { return(a.EndingPoint == vertex); })); Edges.RemoveAt(i); i--; } } Vertices.Remove(vertex); return(true); }
public void RemoveEdge(T from, T to) { if (from == null) { throw new ArgumentNullException(nameof(from)); } if (to == null) { throw new ArgumentNullException(nameof(to)); } if (!Vertices.Contains(from, EqualityComparer)) { throw new ArgumentException($"Cannot remove edge from nonexistant vertex {from}"); } if (!Vertices.Contains(to, EqualityComparer)) { throw new ArgumentException($"Cannot remove edge to nonexistant vertex {to}"); } foreach (var edge in Edges) { if (EqualityComparer.Equals(edge.From, from) && EqualityComparer.Equals(edge.To, to)) { _edges.Remove(edge); return; } } throw new ArgumentException($"Cannot remove nonexistant edge from vertex {from} to vertex {to}"); }
public void AddVertex(Vector2 vert) { if (!Vertices.Contains(vert)) { Vertices.Add(vert); } }
/// <summary> /// Prepares the polygons. /// </summary> /// <param name="polygon1">The polygon1.</param> /// <param name="polygon2">The polygon2.</param> /// <param name="poly1">The poly1.</param> /// <param name="poly2">The poly2.</param> /// <param name="intersections">The intersections.</param> /// <param name="error">The error.</param> /// <returns></returns> private static int PreparePolygons(Vertices polygon1, Vertices polygon2, out Vertices poly1, out Vertices poly2, out List <EdgeIntersectInfo> intersections, out PolyClipError error) { error = PolyClipError.None; // Make a copy of the polygons so that we dont modify the originals, and // force vertices to integer (pixel) values. //Note: We no longer use pixels. Rounding removed. poly1 = new Vertices(polygon1); poly2 = new Vertices(polygon2); // Find intersection points if (!VerticesIntersect(poly1, poly2, out intersections)) { // No intersections found - polygons do not overlap. error = PolyClipError.NoIntersections; return(-1); } // Add intersection points to original polygons, ignoring existing points. foreach (EdgeIntersectInfo intersect in intersections) { if (!poly1.Contains(intersect.IntersectionPoint)) { poly1.Insert(poly1.IndexOf(intersect.EdgeOne.EdgeStart) + 1, intersect.IntersectionPoint); } if (!poly2.Contains(intersect.IntersectionPoint)) { poly2.Insert(poly2.IndexOf(intersect.EdgeTwo.EdgeStart) + 1, intersect.IntersectionPoint); } } // Find starting point on the edge of polygon1 // that is outside of the intersected area // to begin polygon trace. int startingIndex = -1; int currentIndex = 0; do { if (!poly2.PointInPolygonAngle(poly1[currentIndex])) { startingIndex = currentIndex; break; } currentIndex = poly1.NextIndex(currentIndex); } while (currentIndex != 0); // If we dont find a point on polygon1 thats outside of the // intersect area, the polygon1 must be inside of polygon2, // in which case, polygon2 IS the union of the two. if (startingIndex == -1) { error = PolyClipError.Poly1InsidePoly2; } return(startingIndex); }
public void addVertex(Vector2 pt1, Vector2 pt2) { var dist = pt2 - pt1; if (dist.Length() > 100) { var midPoint = (pt1 + pt2) / 2; addVertex(pt1, midPoint); addVertex(midPoint, pt2); } else { pt1 = pt1 * Bloodbender.pixelToMeter; pt2 = pt2 * Bloodbender.pixelToMeter; if (mapVertices.Contains(pt1)) { mapVertices.Add(pt2); return; } mapVertices.Add(pt1); mapVertices.Add(pt2); } }
public void AddToList(Vertex <T> first) { if (!Vertices.Contains(first)) { Vertices.Add(first); } }
/// <summary> /// Find the vertex /// </summary> /// <param name="a_vertex0"></param> /// <param name="a_vertex1"></param> /// <returns></returns> public Vector2?OtherVertex(Vector2 a_vertex0, Vector2 a_vertex1) { if (!Vertices.Contains(a_vertex0) || !Vertices.Contains(a_vertex1)) { throw new GeomException("One of the vertices not contained in triangle"); } return(Vertices.ToList().Find(v => v != a_vertex0 && v != a_vertex1)); }
/// <summary> /// Finds edge that connects to the vertex and is not equal to given edge. /// </summary> /// <param name="a_edge"></param> /// <param name="a_vertex"></param> /// <returns></returns> public TriangleEdge OtherEdge(TriangleEdge a_edge, Vector2 a_vertex) { if (!Edges.Contains(a_edge) || !Vertices.Contains(a_vertex)) { throw new GeomException("Edge or vertex not contained in triangle"); } return(Edges.Find(e => e != a_edge && e.IsEndpoint(a_vertex))); }
public void AddState(State s) { if (Vertices.Contains(s)) { return; } AddVertex(s); }
public void AddVertex(Vertex <T> vertex) { if (vertex == null || Vertices.Contains(vertex)) { return; } Vertices.Add(vertex); }
public override void AddVertex(Vertex <T> vertex) { if (Vertices.Contains(vertex)) { throw new Exception("Vertex already exists"); } Vertices.Add(vertex); }
/// <summary> /// Removes a vertex from this graph. /// </summary> /// <remarks> /// <para> /// It cannot remove vertex from sub-graphs, for this /// one has to invoke specific method on sub-graph, if the sub-graph supports it. /// </para> /// <para> /// To remove a vertex from sub-graph one has invoke RemoveVertex on that particular sub-graph. /// Class <see cref="Graph{TVertex,TEdge}"/> provides method <see cref="Graph{TVertex,TEdge}.RemoveVertexWithEdges"/>, /// which is capable of removing a vertex from a sub-graph and which also removes all edges that contain the vertex. /// </para> /// </remarks> /// <param name="vertex">Vertex to be removed.</param> public void RemoveVertex(TVertex vertex) { Contract.Requires(vertex != null); Contract.Requires( Vertices.Contains(vertex), "RemoveVertex: given vertex is not part of the graph. See the API documentation for more details."); vertices.Remove(vertex); RaiseChanged(); }
public bool RemoveVertex(Vertex <T> vertex) { if (Vertices.Contains(vertex)) { vertex.NeighboringVertices = null; Vertices.Remove(vertex); return(true); } return(false); }
public override bool RemoveEdge(Vertex <T> a, Vertex <T> b) { if (!(Vertices.Contains(a) && Vertices.Contains(b) && Vertices[Vertices.IndexOf(a)].Neighbors.Contains(b))) { return(false); } Vertices[Vertices.IndexOf(a)].Neighbors.Remove(b); Vertices[Vertices.IndexOf(b)].Neighbors.Remove(a); return(true); }
public override void PurgeOrphanEdges() { for (int i = 0; i < Edges.Count; i++) { if (!Vertices.Contains(Edges[i].Source) || !Vertices.Contains(Edges[i].Destination)) { Edges.RemoveAt(i); i--; } } }
/// <summary> /// Updates the with. /// </summary> /// <param name="face">The face.</param> public override void UpdateWith(PolygonalFace face) { var numFaces = Faces.Count; double[] inBetweenPoint; var distance = MiscFunctions.SkewedLineIntersection(face.Center, face.Normal, Anchor, Axis, out inBetweenPoint); var fractionToMove = 1 / numFaces; var moveVector = Anchor.crossProduct(face.Normal); if (moveVector.dotProduct(face.Center.subtract(inBetweenPoint, 3)) < 0) { moveVector = moveVector.multiply(-1); } moveVector.normalizeInPlace(3); /**** set new Anchor (by averaging in with last n values) ****/ Anchor = Anchor.add(new[] { moveVector[0] * fractionToMove * distance, moveVector[1] * fractionToMove * distance, moveVector[2] * fractionToMove * distance }, 3); /* to adjust the Axis, we will average the cross products of the new face with all the old faces */ var totalAxis = new double[3]; for (var i = 0; i < numFaces; i++) { var newAxis = face.Normal.crossProduct(Faces[i].Normal); if (newAxis.dotProduct(Axis, 3) < 0) { newAxis.multiply(-1); } totalAxis = totalAxis.add(newAxis, 3); } var numPrevCrossProducts = numFaces * (numFaces - 1) / 2; totalAxis = totalAxis.add(Axis.multiply(numPrevCrossProducts), 3); /**** set new Axis (by averaging in with last n values) ****/ Axis = totalAxis.divide(numFaces + numPrevCrossProducts).normalize(3); foreach (var v in face.Vertices) { if (!Vertices.Contains(v)) { Vertices.Add(v); } } var totalOfRadii = Vertices.Sum(v => MiscFunctions.DistancePointToLine(v.Position, Anchor, Axis)); /**** set new Radius (by averaging in with last n values) ****/ Radius = totalOfRadii / Vertices.Count; base.UpdateWith(face); }
public override bool AddEdge(Vertex <T> a, Vertex <T> b, int distance = 0) { if (!(Vertices.Contains(a) && Vertices.Contains(b))) { return(false); } Vertices[Vertices.IndexOf(a)].Neighbors.Add(b); Vertices[Vertices.IndexOf(b)].Neighbors.Add(a); return(true); }
public override void AddEdge(GraphVertex <T> v1, GraphVertex <T> v2) { if (!Vertices.Contains(v1) || !Vertices.Contains(v2)) { return; } if (v1.Neighbours.Contains(v2)) { return; } v1.Neighbours.Add(v2); }
private void CreateNewConnection(Edge <TValue> shortestEdge) { if (!Vertices.Contains(shortestEdge.Head)) { AddVertex(shortestEdge.Head); } if (!Vertices.Contains(shortestEdge.Tail)) { AddVertex(shortestEdge.Tail); } AddEdge(shortestEdge); }
public bool AddNode(int node) { if (currentVertexCount < MaxSize) { if (!Vertices.Contains(node)) { Vertices[currentVertexCount] = node; currentVertexCount++; return(true); } } return(false); }
// Seems debatable whether the method should throw on source/target vertex not in the quiver. Not throwing for now. public bool ContainsArrow(Arrow <TVertex> arrow) { if (arrow is null) { throw new ArgumentNullException(nameof(arrow)); } if (!Vertices.Contains(arrow.Source)) { return(false); } return(AdjacencyLists[arrow.Source].Contains(arrow.Target)); }
/// <summary> /// Updates the with. /// </summary> /// <param name="face">The face.</param> public override void UpdateWith(PolygonalFace face) { var numFaces = Faces.Count; var distance = MiscFunctions.SkewedLineIntersection(face.Center, face.Normal, Anchor, Axis, out var inBetweenPoint); var fractionToMove = 1 / numFaces; var moveVector = Anchor.Cross(face.Normal); if (moveVector.Dot(face.Center.Subtract(inBetweenPoint)) < 0) { moveVector = moveVector * -1; } moveVector = moveVector.Normalize(); /**** set new Anchor (by averaging in with last n values) ****/ Anchor = Anchor + new Vector3( moveVector.X * fractionToMove * distance, moveVector.Y * fractionToMove * distance, moveVector.Z * fractionToMove * distance ); /* to adjust the Axis, we will average the cross products of the new face with all the old faces */ var totalAxis = new Vector3(); foreach (var oldFace in Faces) { var newAxis = face.Normal.Cross(oldFace.Normal); if (newAxis.Dot(Axis) < 0) { newAxis = -1 * newAxis; } totalAxis = totalAxis + newAxis; } var numPrevCrossProducts = numFaces * (numFaces - 1) / 2; totalAxis = totalAxis + (Axis * numPrevCrossProducts); /**** set new Axis (by averaging in with last n values) ****/ Axis = totalAxis.Normalize(); foreach (var v in face.Vertices) { if (!Vertices.Contains(v)) { Vertices.Add(v); } } var totalOfRadii = Vertices.Sum(v => MiscFunctions.DistancePointToLine(v.Coordinates, Anchor, Axis)); /**** set new Radius (by averaging in with last n values) ****/ Radius = totalOfRadii / Vertices.Count; base.UpdateWith(face); }
public void DeleteShape() { if (Vertices.Where(x => Vertices.Contains(x)).Count() >= 2) { for (int i = 1; i < Vertices.Count; i++) { MainWindow.DrawLine((int)Vertices[i - 1].X, (int)Vertices[i - 1].Y, (int)Vertices[i].X, (int)Vertices[i].Y, PolyThickness, ColorPoly, true); } } if (IsFilled) { EdgeTableFill(FillColor, true); } }
public void AddVertex(T vertex) { if (vertex == null) { throw new ArgumentNullException(nameof(vertex)); } if (Vertices.Contains(vertex, EqualityComparer)) { throw new ArgumentException($"Vertex {vertex} already in graph"); } _vertices.Add(vertex); }
/// <summary> /// Updates the with. /// </summary> /// <param name="face">The face.</param> public override void UpdateWith(PolygonalFace face) { Normal = Normal.multiply(Faces.Count).add(face.Normal, 3).divide(Faces.Count + 1); Normal.normalizeInPlace(); var newVerts = new List <Vertex>(); var newDistanceToPlane = 0.0; foreach (var v in face.Vertices.Where(v => !Vertices.Contains(v))) { newVerts.Add(v); newDistanceToPlane += v.Position.dotProduct(Normal, 3); } DistanceToOrigin = (Vertices.Count * DistanceToOrigin + newDistanceToPlane) / (Vertices.Count + newVerts.Count); base.UpdateWith(face); }
public void AddVertex(int id) { if (Vertices.Contains(id)) { throw new ArgumentException($"Vertice with same id ({id}) exists"); } if (AdjacencyList.ContainsKey(id)) { throw new ArgumentException($"{id} this key already exists in adjacency list"); } Vertices.Add(id); AdjacencyList.Add(id, new LinkedList <int>()); }
/// <summary> /// /// </summary> /// <param name="entrance"></param> /// <param name="last"></param> /// <returns></returns> private Vertices CreateSimplePolygon(Vector2 entrance, Vector2 last) { bool entranceFound = false; bool endOfHull = false; Vertices polygon = new Vertices(32); Vertices hullArea = new Vertices(32); Vertices endOfHullArea = new Vertices(32); Vector2 current = Vector2.Zero; #region Entrance check // Get the entrance point. //todo: alle möglichkeiten testen if (entrance == Vector2.Zero || !InBounds(ref entrance)) { entranceFound = SearchHullEntrance(out entrance); if (entranceFound) { current = new Vector2(entrance.X - 1f, entrance.Y); } } else { if (IsSolid(ref entrance)) { if (IsNearPixel(ref entrance, ref last)) { current = last; entranceFound = true; } else { Vector2 temp; if (SearchNearPixels(false, ref entrance, out temp)) { current = temp; entranceFound = true; } else { entranceFound = false; } } } } #endregion if (entranceFound) { polygon.Add(entrance); hullArea.Add(entrance); Vector2 next = entrance; do { // Search in the pre vision list for an outstanding point. Vector2 outstanding; if (SearchForOutstandingVertex(hullArea, out outstanding)) { if (endOfHull) { // We have found the next pixel, but is it on the last bit of the hull? if (endOfHullArea.Contains(outstanding)) { // Indeed. polygon.Add(outstanding); } // That's enough, quit. break; } // Add it and remove all vertices that don't matter anymore // (all the vertices before the outstanding). polygon.Add(outstanding); hullArea.RemoveRange(0, hullArea.IndexOf(outstanding)); } // Last point gets current and current gets next. Our little spider is moving forward on the hull ;). last = current; current = next; // Get the next point on hull. if (GetNextHullPoint(ref last, ref current, out next)) { // Add the vertex to a hull pre vision list. hullArea.Add(next); } else { // Quit break; } if (next == entrance && !endOfHull) { // It's the last bit of the hull, search on and exit at next found vertex. endOfHull = true; endOfHullArea.AddRange(hullArea); // We don't want the last vertex to be the same as the first one, because it causes the triangulation code to crash. if (endOfHullArea.Contains(entrance)) endOfHullArea.Remove(entrance); } } while (true); } return polygon; }
/// <summary> /// Prepares the polygons. /// </summary> /// <param name="polygon1">The polygon1.</param> /// <param name="polygon2">The polygon2.</param> /// <param name="poly1">The poly1.</param> /// <param name="poly2">The poly2.</param> /// <param name="intersections">The intersections.</param> /// <param name="error">The error.</param> /// <returns></returns> private static int PreparePolygons(Vertices polygon1, Vertices polygon2, out Vertices poly1, out Vertices poly2, out List<EdgeIntersectInfo> intersections, out PolyUnionError error) { error = PolyUnionError.None; // Make a copy of the polygons so that we dont modify the originals, and // force vertices to integer (pixel) values. poly1 = Round(polygon1); poly2 = Round(polygon2); // Find intersection points if (!VerticesIntersect(poly1, poly2, out intersections)) { // No intersections found - polygons do not overlap. error = PolyUnionError.NoIntersections; return -1; } // Add intersection points to original polygons, ignoring existing points. foreach (EdgeIntersectInfo intersect in intersections) { if (!poly1.Contains(intersect.IntersectionPoint)) { poly1.Insert(poly1.IndexOf(intersect.EdgeOne.EdgeStart) + 1, intersect.IntersectionPoint); } if (!poly2.Contains(intersect.IntersectionPoint)) { poly2.Insert(poly2.IndexOf(intersect.EdgeTwo.EdgeStart) + 1, intersect.IntersectionPoint); } } // Find starting point on the edge of polygon1 // that is outside of the intersected area // to begin polygon trace. int startingIndex = -1; int currentIndex = 0; do { if (!PointInPolygonAngle(poly1[currentIndex], poly2)) { startingIndex = currentIndex; break; } currentIndex = poly1.NextIndex(currentIndex); } while (currentIndex != 0); // If we dont find a point on polygon1 thats outside of the // intersect area, the polygon1 must be inside of polygon2, // in which case, polygon2 IS the union of the two. if (startingIndex == -1) { error = PolyUnionError.Poly1InsidePoly2; } return startingIndex; }