public override void Update(GameSettings settings, GameTime gameTime) { DrawString("Use the mouse to create a polygon."); DrawString("Simple: " + _vertices.IsSimple()); DrawString("Convex: " + _vertices.IsConvex()); DrawString("CCW: " + _vertices.IsCounterClockWise()); DrawString("Area: " + _vertices.GetArea()); PolygonError returnCode = _vertices.CheckPolygon(); if (returnCode == PolygonError.NoError) DrawString("Polygon is supported in Velcro Physics"); else DrawString("Polygon is NOT supported in Velcro Physics. Reason: " + returnCode); DebugView.BeginCustomDraw(ref GameInstance.Projection, ref GameInstance.View); for (int i = 0; i < _vertices.Count; i++) { Vector2 currentVertex = _vertices[i]; Vector2 nextVertex = _vertices.NextVertex(i); DebugView.DrawPoint(currentVertex, 0.1f, Color.Yellow); DebugView.DrawSegment(currentVertex, nextVertex, Color.Red); } DebugView.DrawPoint(_vertices.GetCentroid(), 0.1f, Color.Green); AABB aabb = _vertices.GetAABB(); DebugView.DrawAABB(ref aabb, Color.HotPink); DebugView.EndCustomDraw(); base.Update(settings, gameTime); }
/// <summary> /// Describes whether validate polygon /// </summary> /// <param name="polygon">The polygon</param> /// <returns>The bool</returns> private static bool ValidatePolygon(Vertices polygon) { PolygonError errorCode = polygon.CheckPolygon(); if (errorCode == PolygonError.InvalidAmountOfVertices || errorCode == PolygonError.AreaTooSmall || errorCode == PolygonError.SideTooSmall || errorCode == PolygonError.NotSimple) { return(false); } if ( errorCode == PolygonError .NotCounterClockWise) //NotCounterCloseWise is the last check in CheckPolygon(), thus we don't need to call ValidatePolygon again. { polygon.Reverse(); } if (errorCode == PolygonError.NotConvex) { polygon = GiftWrap.GetConvexHull(polygon); return(ValidatePolygon(polygon)); } return(true); }
public override void Update(FarseerPhysicsGameSettings settings) { DrawString("Use the mouse to create a polygon."); DrawString("Simple: " + _vertices.IsSimple()); DrawString("Convex: " + _vertices.IsConvex()); DrawString("CCW: " + _vertices.IsCounterClockWise()); DrawString("Area: " + _vertices.GetArea()); PolygonError returnCode = _vertices.CheckPolygon(); if (returnCode == PolygonError.NoError) { DrawString("Polygon is supported in Farseer Physics Engine"); } else { DrawString("Polygon is NOT supported in Farseer Physics Engine. Reason: " + returnCode); } for (int i = 0; i < _vertices.Count; i++) { Vector2 currentVertex = _vertices[i]; Vector2 nextVertex = _vertices.NextVertex(i); DebugView.DrawPoint(currentVertex, 0.1f, Color.Yellow); DebugView.DrawSegment(currentVertex, nextVertex, Color.Red); } DebugView.DrawPoint(_vertices.GetCentroid(), 0.1f, Color.Green); AABB aabb = _vertices.GetAABB(); DebugView.DrawAABB(ref aabb, Color.HotPink); base.Update(settings); }
private static bool ValidatePolygon(Vertices polygon) { PolygonError errorCode = polygon.CheckPolygon(); if (errorCode == PolygonError.InvalidAmountOfVertices || errorCode == PolygonError.AreaTooSmall || errorCode == PolygonError.SideTooSmall || errorCode == PolygonError.NotSimple) return false; if (errorCode == PolygonError.NotCounterClockWise) //NotCounterCloseWise is the last check in CheckPolygon(), thus we don't need to call ValidatePolygon again. polygon.Reverse(); if (errorCode == PolygonError.NotConvex) { polygon = GiftWrap.GetConvexHull(polygon); return ValidatePolygon(polygon); } return true; }
public static List <Vertices> ConvexPartition(Vertices vertices, TriangulationAlgorithm algorithm, bool discardAndFixInvalid = true, float tolerance = 0.001f) { if (vertices.Count <= 3) { return new List <Vertices> { vertices } } ; List <Vertices> results; switch (algorithm) { case TriangulationAlgorithm.Earclip: if (Settings.SkipSanityChecks) { Debug.Assert(!vertices.IsCounterClockWise(), "The Earclip algorithm expects the polygon to be clockwise."); } else { if (vertices.IsCounterClockWise()) { Vertices temp = new Vertices(vertices); temp.Reverse(); results = EarclipDecomposer.ConvexPartition(temp, tolerance); } else { results = EarclipDecomposer.ConvexPartition(vertices, tolerance); } } break; case TriangulationAlgorithm.Bayazit: if (Settings.SkipSanityChecks) { Debug.Assert(vertices.IsCounterClockWise(), "The polygon is not counter clockwise. This is needed for Bayazit to work correctly."); } else { if (!vertices.IsCounterClockWise()) { Vertices temp = new Vertices(vertices); temp.Reverse(); results = BayazitDecomposer.ConvexPartition(temp); } else { results = BayazitDecomposer.ConvexPartition(vertices); } } break; case TriangulationAlgorithm.Flipcode: if (Settings.SkipSanityChecks) { Debug.Assert(vertices.IsCounterClockWise(), "The polygon is not counter clockwise. This is needed for Bayazit to work correctly."); } else { if (!vertices.IsCounterClockWise()) { Vertices temp = new Vertices(vertices); temp.Reverse(); results = FlipcodeDecomposer.ConvexPartition(temp); } else { results = FlipcodeDecomposer.ConvexPartition(vertices); } } break; case TriangulationAlgorithm.Seidel: results = SeidelDecomposer.ConvexPartition(vertices, tolerance); break; case TriangulationAlgorithm.SeidelTrapezoids: results = SeidelDecomposer.ConvexPartitionTrapezoid(vertices, tolerance); break; case TriangulationAlgorithm.Delauny: results = CDTDecomposer.ConvexPartition(vertices); break; default: throw new ArgumentOutOfRangeException("algorithm"); } if (discardAndFixInvalid) { for (int i = results.Count - 1; i >= 0; i--) { Vertices polygon = results[i]; PolygonError errorCode = polygon.CheckPolygon(); if (errorCode == PolygonError.InvalidAmountOfVertices || errorCode == PolygonError.AreaTooSmall || errorCode == PolygonError.SideTooSmall || errorCode == PolygonError.NotSimple) { results.RemoveAt(i); } else if (errorCode == PolygonError.NotCounterClockWise) { polygon.Reverse(); } else if (errorCode == PolygonError.NotConvex) { results[i] = GiftWrap.GetConvexHull(polygon); } } } return(results); } }