Esempio n. 1
0
        public static void LineToPolygon(CCPoint[] points, float stroke, ccVertex2F[] vertices, int offset, int nuPoints)
        {
            int     num;
            CCPoint cCPoint;

            nuPoints = nuPoints + offset;
            if (nuPoints <= 1)
            {
                return;
            }
            stroke = stroke * 0.5f;
            int num1 = nuPoints - 1;

            for (int i = offset; i < nuPoints; i++)
            {
                num = i * 2;
                CCPoint cCPoint1 = points[i];
                if (i == 0)
                {
                    cCPoint = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(cCPoint1, points[i + 1])));
                }
                else if (i != num1)
                {
                    CCPoint cCPoint2 = points[i + 1];
                    CCPoint cCPoint3 = points[i - 1];
                    CCPoint cCPoint4 = CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(cCPoint2, cCPoint1));
                    CCPoint cCPoint5 = CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(cCPoint3, cCPoint1));
                    float   single   = (float)Math.Acos((double)CCPointExtension.ccpDot(cCPoint4, cCPoint5));
                    if (single >= ccMacros.CC_DEGREES_TO_RADIANS(70f))
                    {
                        cCPoint = (single >= ccMacros.CC_DEGREES_TO_RADIANS(170f) ? CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(cCPoint2, cCPoint3))) : CCPointExtension.ccpNormalize(CCPointExtension.ccpMidpoint(cCPoint4, cCPoint5)));
                    }
                    else
                    {
                        cCPoint = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpMidpoint(cCPoint4, cCPoint5)));
                    }
                }
                else
                {
                    cCPoint = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(points[i - 1], cCPoint1)));
                }
                cCPoint           = CCPointExtension.ccpMult(cCPoint, stroke);
                vertices[num]     = new ccVertex2F(cCPoint1.x + cCPoint.x, cCPoint1.y + cCPoint.y);
                vertices[num + 1] = new ccVertex2F(cCPoint1.x - cCPoint.x, cCPoint1.y - cCPoint.y);
            }
            offset = (offset == 0 ? 0 : offset - 1);
            for (int j = offset; j < num1; j++)
            {
                num = j * 2;
                int        num2         = num + 2;
                ccVertex2F _ccVertex2F  = vertices[num];
                ccVertex2F _ccVertex2F1 = vertices[num + 1];
                ccVertex2F _ccVertex2F2 = vertices[num2];
                ccVertex2F _ccVertex2F3 = vertices[num2 + 1];
                float      single1      = 0f;
                bool       flag         = !CCVertex.LineIntersect(_ccVertex2F.x, _ccVertex2F.y, _ccVertex2F3.x, _ccVertex2F3.y, _ccVertex2F1.x, _ccVertex2F1.y, _ccVertex2F2.x, _ccVertex2F2.y, out single1);
                if (!flag && (single1 < 0f || single1 > 1f))
                {
                    flag = true;
                }
                if (flag)
                {
                    vertices[num2]     = _ccVertex2F3;
                    vertices[num2 + 1] = _ccVertex2F2;
                }
            }
        }
Esempio n. 2
0
        public static void LineToPolygon(CCPoint[] points, float stroke, ccVertex2F[] vertices, int offset, int nuPoints)
        {
            nuPoints += offset;
            if (nuPoints <= 1)
            {
                return;
            }

            stroke *= 0.5f;

            int idx;
            int nuPointsMinus = nuPoints - 1;

            for (int i = offset; i < nuPoints; i++)
            {
                idx = i * 2;
                CCPoint p1 = points[i];
                CCPoint perpVector;

                if (i == 0)
                {
                    perpVector = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(p1, points[i + 1])));
                }
                else if (i == nuPointsMinus)
                {
                    perpVector = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(points[i - 1], p1)));
                }
                else
                {
                    CCPoint p2 = points[i + 1];
                    CCPoint p0 = points[i - 1];

                    CCPoint p2p1 = CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(p2, p1));
                    CCPoint p0p1 = CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(p0, p1));

                    // Calculate angle between vectors
                    float angle = (float)Math.Acos(CCPointExtension.ccpDot(p2p1, p0p1));

                    if (angle < ccMacros.CC_DEGREES_TO_RADIANS(70))
                    {
                        perpVector = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpMidpoint(p2p1, p0p1)));
                    }
                    else if (angle < ccMacros.CC_DEGREES_TO_RADIANS(170))
                    {
                        perpVector = CCPointExtension.ccpNormalize(CCPointExtension.ccpMidpoint(p2p1, p0p1));
                    }
                    else
                    {
                        perpVector = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(p2, p0)));
                    }
                }
                perpVector = CCPointExtension.ccpMult(perpVector, stroke);

                vertices[idx]     = new ccVertex2F(p1.x + perpVector.x, p1.y + perpVector.y);
                vertices[idx + 1] = new ccVertex2F(p1.x - perpVector.x, p1.y - perpVector.y);
            }

            // Validate vertexes
            offset = (offset == 0) ? 0 : (offset - 1);
            for (int i = offset; i < nuPointsMinus; i++)
            {
                idx = i * 2;
                int idx1 = idx + 2;

                ccVertex2F p1 = vertices[idx];
                ccVertex2F p2 = vertices[idx + 1];
                ccVertex2F p3 = vertices[idx1];
                ccVertex2F p4 = vertices[idx1 + 1];

                float s = 0f;
                //BOOL fixVertex = !ccpLineIntersect(ccp(p1.x, p1.y), ccp(p4.x, p4.y), ccp(p2.x, p2.y), ccp(p3.x, p3.y), &s, &t);
                bool fixVertex = !LineIntersect(p1.x, p1.y, p4.x, p4.y, p2.x, p2.y, p3.x, p3.y, out s);
                if (!fixVertex)
                {
                    if (s < 0.0f || s > 1.0f)
                    {
                        fixVertex = true;
                    }
                }

                if (fixVertex)
                {
                    vertices[idx1]     = p4;
                    vertices[idx1 + 1] = p3;
                }
            }
        }