コード例 #1
0
ファイル: PolygonPanel.cs プロジェクト: prepare/box2c
        void ValidatePolygon()
        {
            HideError();

            if (PolyData.Vertices.Count <= 1 || PolyData.Vertices.Count > Box2DSettings.b2_maxPolygonVertices)
            {
                ShowError("Polygon vertice count", "Polygons can only contain between 2 and 8 vertices.\nNote that a polygon of 2 vertices is called an edge (Box2D 2.1.2).");
            }
            else if (PolyData.Vertices.Count > 2)
            {
                // Compute normals. Ensure the edges have non-zero length.
                for (int i = 0; i < PolyData.Vertices.Count; ++i)
                {
                    int  i1   = i;
                    int  i2   = i + 1 < PolyData.Vertices.Count ? i + 1 : 0;
                    Vec2 edge = PolyData.Vertices[i2] - PolyData.Vertices[i1];

                    if (!(edge.LengthSquared() > float.Epsilon * float.Epsilon))
                    {
                        ShowError("Edge length", "An edge has an incredibly small length. This may be due\nto vertices too close together.");
                        return;
                    }
                }

                for (int i = 0; i < PolyData.Vertices.Count; ++i)
                {
                    int  i1   = i;
                    int  i2   = i + 1 < PolyData.Vertices.Count ? i + 1 : 0;
                    Vec2 edge = PolyData.Vertices[i2] - PolyData.Vertices[i1];

                    for (int j = 0; j < PolyData.Vertices.Count; ++j)
                    {
                        // Don't check vertices on the current edge.
                        if (j == i1 || j == i2)
                        {
                            continue;
                        }

                        Vec2 r = PolyData.Vertices[j] - PolyData.Vertices[i1];

                        // Your polygon is non-convex (it has an indentation) or
                        // has colinear edges.
                        float s = edge.Cross(r);
                        if (!(s > 0.0f))
                        {
                            ShowError("Non-convex/Collinear", "Your polygon is non-convex or has collinear edges.\nTry using the \"Reverse\" button; if that doesn't work,\nthe problem lies within the spacing of your vertices.");
                            return;
                        }
                    }
                }

                Vec2 centroid;
                try
                {
                    PolygonShape.ComputeCentroid(out centroid, PolyData.Vertices);
                }
                catch
                {
                    ShowError("Polygon area", "The area of your polygon is incredibly small.");
                }
            }
        }