コード例 #1
0
ファイル: NGraphics.cs プロジェクト: mengtest/u1
        public void DrawPolygon(Vector2[] points, Color fillColor)
        {
            int numVertices = points.Length;

            if (numVertices < 3)
            {
                return;
            }

            int numTriangles = numVertices - 2;
            int i, restIndexPos, numRestIndices;
            int k = 0;

            Alloc(numVertices);

            for (i = 0; i < numVertices; i++)
            {
                vertices[i] = new Vector3(points[i].x, -points[i].y);
            }

            // Algorithm "Ear clipping method" described here:
            // -> https://en.wikipedia.org/wiki/Polygon_triangulation
            //
            // Implementation inspired by:
            // -> http://polyk.ivank.net
            // -> Starling

            AllocTriangleArray(numTriangles * 3);

            sRestIndices.Clear();
            for (i = 0; i < numVertices; ++i)
            {
                sRestIndices.Add(i);
            }

            restIndexPos   = 0;
            numRestIndices = numVertices;

            Vector2 a, b, c, p;
            int     otherIndex;
            bool    earFound;
            int     i0, i1, i2;

            while (numRestIndices > 3)
            {
                earFound = false;
                i0       = sRestIndices[restIndexPos % numRestIndices];
                i1       = sRestIndices[(restIndexPos + 1) % numRestIndices];
                i2       = sRestIndices[(restIndexPos + 2) % numRestIndices];

                a = points[i0];
                b = points[i1];
                c = points[i2];

                if ((a.y - b.y) * (c.x - b.x) + (b.x - a.x) * (c.y - b.y) >= 0)
                {
                    earFound = true;
                    for (i = 3; i < numRestIndices; ++i)
                    {
                        otherIndex = sRestIndices[(restIndexPos + i) % numRestIndices];
                        p          = points[otherIndex];

                        if (ToolSet.IsPointInTriangle(ref p, ref a, ref b, ref c))
                        {
                            earFound = false;
                            break;
                        }
                    }
                }

                if (earFound)
                {
                    triangles[k++] = i0;
                    triangles[k++] = i1;
                    triangles[k++] = i2;
                    sRestIndices.RemoveAt((restIndexPos + 1) % numRestIndices);

                    numRestIndices--;
                    restIndexPos = 0;
                }
                else
                {
                    restIndexPos++;
                    if (restIndexPos == numRestIndices)
                    {
                        break;                                                     // no more ears
                    }
                }
            }
            triangles[k++] = sRestIndices[0];
            triangles[k++] = sRestIndices[1];
            triangles[k++] = sRestIndices[2];

            FillColors(fillColor);
            UpdateMesh();
        }