public static Polygon2f FromCapsule(Vector2f center, float radius, float length, int segments) { int num = segments + 1; Polygon2f polygon = new Polygon2f(num * 2); float pi = (float)Math.PI; float fseg = segments; float half = length * 0.5f; for (int i = 0; i < num; i++) { float theta = pi * i / fseg; float x = radius * (float)Math.Cos(theta); float y = radius * (float)Math.Sin(theta); polygon.Positions[i] = center + new Vector2f(x, y + half); } for (int i = 0; i < num; i++) { float theta = pi * i / fseg + pi; float x = radius * (float)Math.Cos(theta); float y = radius * (float)Math.Sin(theta); polygon.Positions[i + num] = center + new Vector2f(x, y - half); } polygon.CalculatePolygon(); polygon.MakeCCW(); return(polygon); }
private static Polygon2f CreatePolygon(int index) { CGAL_PointToPolygon(index); int numPoints = CGAL_NumPolygonPoints(); int numHoles = CGAL_NumPolygonHoles(); Polygon2f polygon = new Polygon2f(numPoints); for (int i = 0; i < numPoints; i++) { polygon.Positions[i] = CGAL_GetPolygonPoint2f(i); } for (int i = 0; i < numHoles; i++) { int holePoints = CGAL_NumHolePoints(i); Polygon2f hole = new Polygon2f(holePoints); for (int j = 0; j < holePoints; j++) { hole.Positions[j] = CGAL_GetHolePoint2f(i, j); } polygon.AddHole(hole); } polygon.CalculatePolygon(); return(polygon); }
public static Polygon2f FromTriangle(Vector2f A, Vector2f B, Vector2f C) { Polygon2f polygon = new Polygon2f(3); polygon.Positions[0] = A; polygon.Positions[1] = B; polygon.Positions[2] = C; polygon.CalculatePolygon(); polygon.MakeCCW(); return(polygon); }
public static Polygon2f FromBox(Vector2f min, Vector2f max) { Polygon2f polygon = new Polygon2f(4); polygon.Positions[0] = min; polygon.Positions[1] = new Vector2f(max.x, min.y); polygon.Positions[2] = max; polygon.Positions[3] = new Vector2f(min.x, max.y); polygon.CalculatePolygon(); polygon.MakeCCW(); return(polygon); }
private static Polygon2f CreatePolygon(int index) { int numPoints = CGAL_NumPolygonPoints(index); Polygon2f polygon = new Polygon2f(numPoints); for (int i = 0; i < numPoints; i++) { polygon.Positions[i] = CGAL_GetPolygonPoint2f(index, i); } polygon.CalculatePolygon(); return(polygon); }
public static Polygon2f FromStar4(Vector2f center, float scale) { Polygon2f polygon = new Polygon2f(8); polygon.Positions[0] = new Vector2f(0, -1) * scale; polygon.Positions[1] = new Vector2f(0.2f, -0.2f) * scale; polygon.Positions[2] = new Vector2f(1, 0) * scale; polygon.Positions[3] = new Vector2f(0.2f, 0.2f) * scale; polygon.Positions[4] = new Vector2f(0, 1) * scale; polygon.Positions[5] = new Vector2f(-0.2f, 0.2f) * scale; polygon.Positions[6] = new Vector2f(-1, 0) * scale; polygon.Positions[7] = new Vector2f(-0.2f, -0.2f) * scale; polygon.CalculatePolygon(); polygon.MakeCCW(); return(polygon); }
public static List <Polygon2f> Partition(Polygon2f polygon, PARTITION_METHOD method = PARTITION_METHOD.APPROX) { if (!polygon.IsSimple) { throw new ArgumentException("Polygon must be simple."); } if (!polygon.IsCCW) { throw new ArgumentException("Polygon must have counter clock wise orientation. Reverse polygon."); } if (polygon.HasHoles) { throw new NotImplementedException("Polygon with holes not implemented."); } CGAL_LoadPoints(polygon.Positions, polygon.Positions.Length); int numPolygons = PerformPartition(method); List <Polygon2f> partition = new List <Polygon2f>(numPolygons); for (int i = 0; i < numPolygons; i++) { int numPoints = CGAL_GetPolygonSize(i); Polygon2f poly = new Polygon2f(numPoints); for (int j = 0; j < numPoints; j++) { poly.Positions[j] = CGAL_GetPolygonVector2f(i, j); } poly.CalculatePolygon(); partition.Add(poly); } CGAL_Clear(); return(partition); }
public static Polygon2f FromCircle(Vector2f center, float radius, int segments) { Polygon2f polygon = new Polygon2f(segments); float pi = (float)Math.PI; float fseg = segments; for (int i = 0; i < segments; i++) { float theta = 2.0f * pi * i / fseg; float x = radius * (float)Math.Cos(theta); float y = radius * (float)Math.Sin(theta); polygon.Positions[i] = center + new Vector2f(x, y); } polygon.CalculatePolygon(); polygon.MakeCCW(); return(polygon); }
public static Polygon2f Simplify(Polygon2f polygon, float threshold, SIMPLIFY_METHOD method = SIMPLIFY_METHOD.SQUARE_DIST) { if (!polygon.IsSimple) { throw new ArgumentException("Polygon must be simple."); } if (polygon.HasHoles) { throw new NotImplementedException("Polygon with holes not implemented."); } if (polygon.VerticesCount < 3) { Polygon2f simplified = new Polygon2f(polygon.Positions); simplified.CalculatePolygon(); return(simplified); } else { CGAL_LoadPoints(polygon.Positions, polygon.Positions.Length); int size = PerformSimplification(threshold, method); Polygon2f simplified = new Polygon2f(size); for (int i = 0; i < size; i++) { simplified.Positions[i] = CGAL_GetSimplifiedVector2f(i); } simplified.CalculatePolygon(); CGAL_Clear(); return(simplified); } }