Exemplo n.º 1
0
        public void DrawArrow(Vector2 start, Vector2 end, float length, float width, bool drawStartIndicator,
                              Color color)
        {
            // Draw connection segment between start- and end-point
            DrawSegment(start, end, color);

            // Precalculate halfwidth
            float halfWidth = width / 2;

            // Create directional reference
            Vector2 rotation = (start - end);

            rotation.Normalize();

            // Calculate angle of directional vector
            float angle = (float)Math.Atan2(rotation.X, -rotation.Y);
            // Create matrix for rotation
            Matrix rotMatrix = Matrix.CreateRotationZ(angle);
            // Create translation matrix for end-point
            Matrix endMatrix = Matrix.CreateTranslation(end.X, end.Y, 0);

            // Setup arrow end shape
            CCVector2[] verts = new CCVector2[3];
            verts[0] = new CCVector2(0, 0);
            verts[1] = new CCVector2(-halfWidth, -length);
            verts[2] = new CCVector2(halfWidth, -length);

            // Rotate end shape
            var rotAffineTransform = MatrixToCCAffineTransform(rotMatrix);

            CCVector2.Transform(verts, ref rotAffineTransform, verts);
            // Translate end shape
            var endAffineTransform = MatrixToCCAffineTransform(endMatrix);

            CCVector2.Transform(verts, ref endAffineTransform, verts);

            // Draw arrow end shape
            DrawSolidPolygon(verts, 3, color, false);

            if (drawStartIndicator)
            {
                // Create translation matrix for start
                Matrix startMatrix = Matrix.CreateTranslation(start.X, start.Y, 0);
                // Setup arrow start shape
                CCVector2[] baseVerts = new CCVector2[4];
                baseVerts[0] = new CCVector2(-halfWidth, length / 4);
                baseVerts[1] = new CCVector2(halfWidth, length / 4);
                baseVerts[2] = new CCVector2(halfWidth, 0);
                baseVerts[3] = new CCVector2(-halfWidth, 0);

                // Rotate start shape
                CCVector2.Transform(baseVerts, ref rotAffineTransform, baseVerts);
                // Translate start shape
                var startAffineTransform = MatrixToCCAffineTransform(startMatrix);
                CCVector2.Transform(baseVerts, ref startAffineTransform, baseVerts);
                // Draw start shape
                DrawSolidPolygon(baseVerts, 4, color, false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an open or closed <see cref="GraphicsPath"/> from a transformed copy of the path with a given <see cref="Pen"/>.
        /// </summary>
        /// <param name="pen">The pen to stroke the path with.</param>
        /// <param name="transform">The transform matrix to apply to all of the points in the path.</param>
        /// <param name="pathType">Whether the path is open or closed.</param>
        /// <returns>A computed <see cref="GraphicsPath"/>.</returns>
        public GraphicsPath Stroke(Pen pen, CCAffineTransform transform, PathType pathType)
        {
            CCVector2[] buffer = new CCVector2[_geometryIndex];
            for (int i = 0; i < _geometryIndex; i++)
            {
                buffer[i] = CCVector2.Transform(_geometryBuffer[i], transform);
            }

            return(new GraphicsPath(pen, buffer, _lengthBuffer, pathType, 0, _geometryIndex));
        }