FairyGUI.NGraphics.DrawPolygon C# (CSharp) Method

DrawPolygon() public method

public DrawPolygon ( Vector2 points, Color fillColor, Color allColors ) : void
points UnityEngine.Vector2
fillColor UnityEngine.Color
allColors UnityEngine.Color
return void
        public void DrawPolygon(Vector2[] points, Color fillColor, Color[] allColors)
        {
            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];

            if (allColors != null)
            {
                int colorCnt = allColors.Length;
                Color32[] arr = this.colors;
                for (i = 0; i < numVertices; i++)
                    arr[i] = allColors[i % colorCnt];
            }
            else
                FillColors(fillColor);

            UpdateMesh();
        }

Usage Example

 static public int DrawPolygon(IntPtr l)
 {
     try {
         FairyGUI.NGraphics    self = (FairyGUI.NGraphics)checkSelf(l);
         UnityEngine.Vector2[] a1;
         checkArray(l, 2, out a1);
         UnityEngine.Color a2;
         checkType(l, 3, out a2);
         self.DrawPolygon(a1, a2);
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }