Exemplo n.º 1
0
        public void DrawLine(CCPoint from, CCPoint to, float lineWidth, CCColor4B color)
        {
            var cl = color;

            var a = from;
            var b = to;

            var n = CCPoint.Normalize(CCPoint.PerpendicularCCW(a - b));

            var lww = lineWidth;
            var nw  = n * lww;
            var v0  = b - nw;
            var v1  = b + nw;
            var v2  = a - nw;
            var v3  = a + nw;

            // Triangles from beginning to end
            AddTriangleVertex(new CCV3F_C4B(v1, cl));
            AddTriangleVertex(new CCV3F_C4B(v2, cl));
            AddTriangleVertex(new CCV3F_C4B(v0, cl));

            AddTriangleVertex(new CCV3F_C4B(v1, cl));
            AddTriangleVertex(new CCV3F_C4B(v2, cl));
            AddTriangleVertex(new CCV3F_C4B(v3, cl));

            dirty = true;
        }
Exemplo n.º 2
0
        public void DrawSegment(CCPoint from, CCPoint to, float radius, CCColor4F color)
        {
            var cl = color;

            var a = from;
            var b = to;

            var n = CCPoint.Normalize(CCPoint.PerpendicularCCW(a - b));

            var lww = radius;
            var nw  = n * lww;
            var v0  = b - nw;
            var v1  = b + nw;
            var v2  = a - nw;
            var v3  = a + nw;

            // Triangles from beginning to end
            AddTriangleVertex(new CCV3F_C4B(v1, cl));
            AddTriangleVertex(new CCV3F_C4B(v2, cl));
            AddTriangleVertex(new CCV3F_C4B(v0, cl));

            AddTriangleVertex(new CCV3F_C4B(v1, cl));
            AddTriangleVertex(new CCV3F_C4B(v2, cl));
            AddTriangleVertex(new CCV3F_C4B(v3, cl));

            var mb = (float)Math.Atan2(v1.Y - b.Y, v1.X - b.X);
            var ma = (float)Math.Atan2(v2.Y - a.Y, v2.X - a.X);

            // Draw rounded line caps
            DrawSolidArc(a, radius, ma, MathHelper.Pi, color);
            DrawSolidArc(b, radius, mb, MathHelper.Pi, color);

            dirty = true;
        }
Exemplo n.º 3
0
        public void DrawSegment(CCPoint from, CCPoint to, float radius, CCColor4F color)
        {
            var cl = color.ToColor();

            var a = from;
            var b = to;

            var n = CCPoint.Normalize(CCPoint.PerpendicularCCW(a - b));

            var lww = radius;
            var nw  = n * lww;
            var v0  = b - nw;
            var v1  = b + nw;
            var v2  = a - nw;
            var v3  = a + nw;

            // Triangles from beginning to end
            triangleVertices.Add(new VertexPositionColor(v1.ToVector3(), cl));
            triangleVertices.Add(new VertexPositionColor(v2.ToVector3(), cl));
            triangleVertices.Add(new VertexPositionColor(v0.ToVector3(), cl));

            triangleVertices.Add(new VertexPositionColor(v1.ToVector3(), cl));
            triangleVertices.Add(new VertexPositionColor(v2.ToVector3(), cl));
            triangleVertices.Add(new VertexPositionColor(v3.ToVector3(), cl));

            var mb = (float)Math.Atan2(v1.Y - b.Y, v1.X - b.X);
            var ma = (float)Math.Atan2(v2.Y - a.Y, v2.X - a.X);

            // Draw rounded line caps
            DrawSolidArc(a, radius, ma, MathHelper.Pi, color);
            DrawSolidArc(b, radius, mb, MathHelper.Pi, color);

            dirty = true;
        }
Exemplo n.º 4
0
        public void DrawLine(CCPoint from, CCPoint to, float lineWidth, CCColor4B color, CCLineCap lineCap = CCLineCap.Butt)
        {
            System.Diagnostics.Debug.Assert(lineWidth >= 0, "Invalid value specified for lineWidth : value is negative");
            if (lineWidth <= 0)
            {
                return;
            }

            var cl = color;

            var a = from;
            var b = to;

            var normal = CCPoint.Normalize(a - b);

            if (lineCap == CCLineCap.Square)
            {
                var nr = normal * lineWidth;
                a += nr;
                b -= nr;
            }

            var n = CCPoint.PerpendicularCCW(normal);

            var nw = n * lineWidth;
            var v0 = b - nw;
            var v1 = b + nw;
            var v2 = a - nw;
            var v3 = a + nw;

            // Triangles from beginning to end
            AddTriangleVertex(new CCV3F_C4B(v1, cl));
            AddTriangleVertex(new CCV3F_C4B(v2, cl));
            AddTriangleVertex(new CCV3F_C4B(v0, cl));

            AddTriangleVertex(new CCV3F_C4B(v1, cl));
            AddTriangleVertex(new CCV3F_C4B(v2, cl));
            AddTriangleVertex(new CCV3F_C4B(v3, cl));

            if (lineCap == CCLineCap.Round)
            {
                var mb = (float)Math.Atan2(v1.Y - b.Y, v1.X - b.X);
                var ma = (float)Math.Atan2(v2.Y - a.Y, v2.X - a.X);

                // Draw rounded line caps
                DrawSolidArc(a, lineWidth, -ma, -MathHelper.Pi, color);
                DrawSolidArc(b, lineWidth, -mb, -MathHelper.Pi, color);
            }

            dirty = true;
        }
Exemplo n.º 5
0
        public static void DrawLine(CCPoint origin, CCPoint destination, CCColor4B color)
        {
            var a = origin;
            var b = destination;

            var n = CCPoint.Normalize(CCPoint.PerpendicularCCW(a - b));

            var lww = LineWidth * 0.5f;
            var nw  = n * lww;
            var v0  = b - nw;
            var v1  = b + nw;
            var v2  = a - nw;
            var v3  = a + nw;

            // Triangles from beginning to end
            batch.AddVertex(v1, color, PrimitiveType.TriangleList);
            batch.AddVertex(v2, color, PrimitiveType.TriangleList);
            batch.AddVertex(v0, color, PrimitiveType.TriangleList);

            batch.AddVertex(v1, color, PrimitiveType.TriangleList);
            batch.AddVertex(v2, color, PrimitiveType.TriangleList);
            batch.AddVertex(v3, color, PrimitiveType.TriangleList);
        }
Exemplo n.º 6
0
        public void DrawPolygon(CCPoint[] verts, int count, CCColor4F fillColor, float borderWidth,
                                CCColor4F borderColor)
        {
            var extrude = new ExtrudeVerts[count];

            for (int i = 0; i < count; i++)
            {
                var v0 = verts[(i - 1 + count) % count];
                var v1 = verts[i];
                var v2 = verts[(i + 1) % count];

                var n1 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v1 - v0));
                var n2 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v2 - v1));

                var offset = (n1 + n2) * (1.0f / (CCPoint.Dot(n1, n2) + 1.0f));
                extrude[i] = new ExtrudeVerts()
                {
                    offset = offset, n = n2
                };
            }

            bool outline = (borderColor.A > 0.0f && borderWidth > 0.0f);

            var colorFill  = new CCColor4B(fillColor);
            var borderFill = new CCColor4B(borderColor);

            float inset = (!outline ? 0.5f : 0.0f);

            for (int i = 0; i < count - 2; i++)
            {
                var v0 = verts[0] - (extrude[0].offset * inset);
                var v1 = verts[i + 1] - (extrude[i + 1].offset * inset);
                var v2 = verts[i + 2] - (extrude[i + 2].offset * inset);

                AddTriangleVertex(new CCV3F_C4B(v0, colorFill)); //__t(v2fzero)
                AddTriangleVertex(new CCV3F_C4B(v1, colorFill)); //__t(v2fzero)
                AddTriangleVertex(new CCV3F_C4B(v2, colorFill)); //__t(v2fzero)
            }

            for (int i = 0; i < count; i++)
            {
                int j  = (i + 1) % count;
                var v0 = verts[i];
                var v1 = verts[j];

                var n0 = extrude[i].n;

                var offset0 = extrude[i].offset;
                var offset1 = extrude[j].offset;

                if (outline)
                {
                    var inner0 = (v0 - (offset0 * borderWidth));
                    var inner1 = (v1 - (offset1 * borderWidth));
                    var outer0 = (v0 + (offset0 * borderWidth));
                    var outer1 = (v1 + (offset1 * borderWidth));

                    AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); //__t(v2fneg(n0))
                    AddTriangleVertex(new CCV3F_C4B(inner1, borderFill)); //__t(v2fneg(n0))
                    AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); //__t(n0)

                    AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); //__t(v2fneg(n0))
                    AddTriangleVertex(new CCV3F_C4B(outer0, borderFill)); //__t(n0)
                    AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); //__t(n0)
                }
                else
                {
                    var inner0 = (v0 - (offset0 * 0.5f));
                    var inner1 = (v1 - (offset1 * 0.5f));
                    var outer0 = (v0 + (offset0 * 0.5f));
                    var outer1 = (v1 + (offset1 * 0.5f));

                    AddTriangleVertex(new CCV3F_C4B(inner0, colorFill)); //__t(v2fzero)
                    AddTriangleVertex(new CCV3F_C4B(inner1, colorFill)); //__t(v2fzero)
                    AddTriangleVertex(new CCV3F_C4B(outer1, colorFill)); //__t(n0)

                    AddTriangleVertex(new CCV3F_C4B(inner0, colorFill)); //__t(v2fzero)
                    AddTriangleVertex(new CCV3F_C4B(outer0, colorFill)); //__t(n0)
                    AddTriangleVertex(new CCV3F_C4B(outer1, colorFill)); //__t(n0)
                }
            }

            dirty = true;
        }
Exemplo n.º 7
0
        public void DrawPolygon(CCPoint[] verts, int count, CCColor4F fillColor, float borderWidth,
                                CCColor4F borderColor, bool closePolygon = true)
        {
            var polycount = count;

            var colorFill  = new CCColor4B(fillColor);
            var borderFill = new CCColor4B(borderColor);

            bool outline = (borderColor.A > 0.0f && borderWidth > 0.0f);
            bool fill    = fillColor.A > 0.0f;

            var numberOfTriangles = outline ? (3 * polycount - 2) : (polycount - 2);

            if (numberOfTriangles > 0 && fill && closePolygon)
            {
                for (int i = 1; i < polycount - 1; i++)
                {
                    AddTriangleVertex(new CCV3F_C4B(verts[0], colorFill));
                    AddTriangleVertex(new CCV3F_C4B(verts[i], colorFill));
                    AddTriangleVertex(new CCV3F_C4B(verts[i + 1], colorFill));
                }
            }
            else
            {
                for (int i = 0; i < polycount - 1; i++)
                {
                    DrawLine(verts[i], verts[i + 1], borderWidth, colorFill);
                }
            }

            if (outline)
            {
                var extrude = new ExtrudeVerts[polycount];

                for (int i = 0; i < polycount; i++)
                {
                    var v0 = verts[(i - 1 + polycount) % polycount];
                    var v1 = verts[i];
                    var v2 = verts[(i + 1) % polycount];

                    var n1 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v1 - v0));
                    var n2 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v2 - v1));

                    var offset = (n1 + n2) * (1.0f / (CCPoint.Dot(n1, n2) + 1.0f));
                    extrude[i] = new ExtrudeVerts()
                    {
                        offset = offset, n = n2
                    };
                }

                float inset = (!outline ? 0.5f : 0.0f);

                for (int i = 0; i < polycount - 2; i++)
                {
                    var v0 = verts[0] - (extrude[0].offset * inset);
                    var v1 = verts[i + 1] - (extrude[i + 1].offset * inset);
                    var v2 = verts[i + 2] - (extrude[i + 2].offset * inset);

                    AddTriangleVertex(new CCV3F_C4B(v0, colorFill)); //__t(v2fzero)
                    AddTriangleVertex(new CCV3F_C4B(v1, colorFill)); //__t(v2fzero)
                    AddTriangleVertex(new CCV3F_C4B(v2, colorFill)); //__t(v2fzero)
                }

                for (int i = 0; i < polycount - 1; i++)
                {
                    int j  = (i + 1) % polycount;
                    var v0 = verts[i];
                    var v1 = verts[j];

                    var offset0 = extrude[i].offset;
                    var offset1 = extrude[j].offset;

                    var inner0 = (v0 - (offset0 * borderWidth));
                    var inner1 = (v1 - (offset1 * borderWidth));
                    var outer0 = (v0 + (offset0 * borderWidth));
                    var outer1 = (v1 + (offset1 * borderWidth));

                    AddTriangleVertex(new CCV3F_C4B(inner0, borderFill));
                    AddTriangleVertex(new CCV3F_C4B(inner1, borderFill));
                    AddTriangleVertex(new CCV3F_C4B(outer1, borderFill));

                    AddTriangleVertex(new CCV3F_C4B(inner0, borderFill));
                    AddTriangleVertex(new CCV3F_C4B(outer0, borderFill));
                    AddTriangleVertex(new CCV3F_C4B(outer1, borderFill));
                }

                if (closePolygon)
                {
                    for (int i = polycount - 1; i < polycount; i++)
                    {
                        int j  = (i + 1) % polycount;
                        var v0 = verts[i];
                        var v1 = verts[j];

                        var offset0 = extrude[i].offset;
                        var offset1 = extrude[j].offset;

                        var inner0 = (v0 - (offset0 * borderWidth));
                        var inner1 = (v1 - (offset1 * borderWidth));
                        var outer0 = (v0 + (offset0 * borderWidth));
                        var outer1 = (v1 + (offset1 * borderWidth));

                        AddTriangleVertex(new CCV3F_C4B(inner0, borderFill));
                        AddTriangleVertex(new CCV3F_C4B(inner1, borderFill));
                        AddTriangleVertex(new CCV3F_C4B(outer1, borderFill));

                        AddTriangleVertex(new CCV3F_C4B(inner0, borderFill));
                        AddTriangleVertex(new CCV3F_C4B(outer0, borderFill));
                        AddTriangleVertex(new CCV3F_C4B(outer1, borderFill));
                    }
                }
            }

            dirty = true;
        }
Exemplo n.º 8
0
        static void VertexLineToPolygon(CCPoint[] points, float stroke, CCV3F_C4B_T2F[] vertices, int offset, int nuPoints)
        {
            nuPoints += offset;
            if (nuPoints <= 1)
            {
                return;
            }

            stroke *= 0.5f;

            int idx;
            int nuPointsMinus = nuPoints - 1;

            float rad70  = MathHelper.ToRadians(70);
            float rad170 = MathHelper.ToRadians(170);

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

                if (i == 0)
                {
                    perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(p1 - points[i + 1]));
                }
                else if (i == nuPointsMinus)
                {
                    perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(points[i - 1] - p1));
                }
                else
                {
                    CCPoint p2 = points[i + 1];
                    CCPoint p0 = points[i - 1];

                    CCPoint p2p1 = CCPoint.Normalize(p2 - p1);
                    CCPoint p0p1 = CCPoint.Normalize(p0 - p1);

                    // Calculate angle between vectors
                    var angle = (float)Math.Acos(CCPoint.Dot(p2p1, p0p1));

                    if (angle < rad70)
                    {
                        perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(CCPoint.Midpoint(p2p1, p0p1)));
                    }
                    else if (angle < rad170)
                    {
                        perpVector = CCPoint.Normalize(CCPoint.Midpoint(p2p1, p0p1));
                    }
                    else
                    {
                        perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(p2 - p0));
                    }
                }

                perpVector = perpVector * stroke;

                vertices[idx].Vertices     = new CCVertex3F(p1.X + perpVector.X, p1.Y + perpVector.Y, 0);
                vertices[idx + 1].Vertices = new CCVertex3F(p1.X - perpVector.X, p1.Y - perpVector.Y, 0);
            }

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

                CCVertex3F p1 = vertices[idx].Vertices;
                CCVertex3F p2 = vertices[idx + 1].Vertices;
                CCVertex3F p3 = vertices[idx1].Vertices;
                CCVertex3F p4 = vertices[idx1 + 1].Vertices;

                float s;
                bool  fixVertex = !VertexLineIntersect(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].Vertices     = p4;
                    vertices[idx1 + 1].Vertices = p3;
                }
            }
        }