Example #1
0
        /// <summary>
        /// Draws a line with rounded ends.
        /// </summary>
        /// <param name="x1">The X position of the first point.</param>
        /// <param name="y1">The Y position of the first point.</param>
        /// <param name="x2">The X position of the second point.</param>
        /// <param name="y2">The Y position of the second point.</param>
        /// <param name="color">The color of the line.</param>
        /// <param name="thickness">The thickness of the line.</param>
        static public void RoundedLine(float x1, float y1, float x2, float y2, Color color, float thickness) {
            VertexArray vertices = new VertexArray(PrimitiveType.TrianglesFan);

            int rotationSteps = 10;

            var line = new Vector2(x2 - x1, y2 - y1);
            var normalUp = new Vector2(y1 - y2, x2 - x1);
            var normalDown = new Vector2(y2 - y1, x1 - x2);

            normalUp.Normalize(thickness * 0.5f);
            normalDown.Normalize(thickness * 0.5f);

            var nextPoint = new Vector2();

            float vx, vy;

            vx = x1;
            vy = y1;

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x1 + normalUp.X);
            vy = (float)(y1 + normalUp.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            nextPoint.X = normalUp.X;
            nextPoint.Y = normalUp.Y;

            for (int i = 0; i < rotationSteps; i++) {
                nextPoint = Util.Rotate(nextPoint, -180 / rotationSteps);

                vx = (float)(x1 + nextPoint.X);
                vy = (float)(y1 + nextPoint.Y);

                vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));
            }

            vx = (float)(x1 + normalDown.X);
            vy = (float)(y1 + normalDown.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x2 + normalDown.X);
            vy = (float)(y2 + normalDown.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            for (int i = 0; i < rotationSteps; i++) {
                nextPoint = Util.Rotate(nextPoint, -180 / rotationSteps);

                vx = (float)(x2 + nextPoint.X);
                vy = (float)(y2 + nextPoint.Y);

                vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));
            }

            vx = (float)(x2 + normalUp.X);
            vy = (float)(y2 + normalUp.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x1 + normalUp.X);
            vy = (float)(y1 + normalUp.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            Drawable(vertices, RenderStates.Default);
        }
Example #2
0
        /// <summary>
        /// Draws a line with a thickness using a quad.
        /// </summary>
        /// <param name="x1">The X position of the first point.</param>
        /// <param name="y1">The Y position of the first point.</param>
        /// <param name="x2">The X position of the second point.</param>
        /// <param name="y2">The Y position of the second point.</param>
        /// <param name="color">The color of the line.</param>
        /// <param name="thickness">The thickness of the line.</param>
        static public void Line(float x1, float y1, float x2, float y2, Color color, float thickness) {
            VertexArray vertices = new VertexArray(PrimitiveType.Quads);

            var line = new Vector2(x2 - x1, y2 - y1);
            var normalUp = new Vector2(y1 - y2, x2 - x1);
            var normalDown = new Vector2(y2 - y1, x1 - x2);

            normalUp.Normalize(thickness * 0.5f);
            normalDown.Normalize(thickness * 0.5f);

            float vx, vy;

            vx = (float)(x1 + normalUp.X);
            vy = (float)(y1 + normalUp.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x1 + normalDown.X);
            vy = (float)(y1 + normalDown.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x2 + normalDown.X);
            vy = (float)(y2 + normalDown.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x2 + normalUp.X);
            vy = (float)(y2 + normalUp.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            Drawable(vertices, RenderStates.Default);
        }