Docs menu: Paths, polylines and polygons Documentation home
On this page Polyline and polygon Path Path commands Building paths from parameters

Paths, polylines and polygons

Path data, point lists and closed shapes.

Three elements describe outlines that are not a single primitive shape. polyline and polygon connect a list of points with straight segments. path accepts the full path data language of SVG.

path_demo rendered
examples/path_demo.msx · 14 lines · open in playground · all samples
Source
@CONFIG( version -> "1.0.0" )
@DATA(
  scene = { width = 500, height = 500, background = #0d1117 }
  elements::
    { type = "path", d = "M 250 50 L 450 430 L 50 430 Z",
      style = { fill = #3498db, stroke = #2980b9, stroke_width = 3.0, opacity = 0.85 } }
    { type = "path",
      d = "M 250 120 C 340 60 440 140 420 240 C 400 340 300 420 210 390 C 120 360 80 260 100 180 C 120 100 160 180 250 120 Z",
      style = { fill = "none", stroke = #e74c3c, stroke_width = 4.0, opacity = 1.0 } }
    { type = "path", d = "M 80 350 A 130 130 0 0 1 420 350",
      style = { fill = "none", stroke = #f5a623, stroke_width = 5.0, opacity = 1.0 } }
    { type = "circle", cx = 250, cy = 250, r = 8,
      style = { fill = #ffffff, stroke = "none", stroke_width = 0, opacity = 1.0 } }
)

Polyline and polygon

{ type = "polyline", points = [[20, 120], [60, 40], [100, 120], [140, 40]],
  style = { fill = "none", stroke = #f5a623, stroke_width = 3 } }

{ type = "polygon", points = [[200, 40], [260, 40], [230, 110]],
  style = { fill = #a78bfa } }

points is an array of [x, y] pairs. A polygon closes the shape: the last point connects back to the first in the fill and in the stroke. A polyline stays open, so its stroke does not return to the start. A polyline with a stroke and no fill is the usual way to draw a zigzag.

Path

{ type = "path", d = "M 250 50 L 450 430 L 50 430 Z",
  style = { fill = #3498db, stroke = #2980b9, stroke_width = 3 } }

d is a string of path data. It is parsed when the file loads, so a syntax error in d stops the parse with the element's path in the message, for example elements[1].d: ....

Path commands

Each command exists in an absolute form (capital letter) and a relative form (lowercase letter). A relative command measures from the current point.

Command Arguments Draws
M x y Move to a new start point
L x y Line
H x Horizontal line
V y Vertical line
C x1 y1 x2 y2 x y Cubic Bezier curve
S x2 y2 x y Smooth cubic, reflecting the previous control point
Q x1 y1 x y Quadratic Bezier curve
T x y Smooth quadratic, reflecting the previous control point
A rx ry rotation large sweep x y Elliptical arc
Z none Close the path

A command can be followed by several argument sets, which repeat the command. The large and sweep arguments of an arc are flags, 0 or 1. In path_demo, M 80 350 A 130 130 0 0 1 420 350 draws an arc from (80, 350) to (420, 350) with no axis rotation, large set to 0 and sweep set to 1, which turns clockwise on screen.

Building paths from parameters

For shapes that depend on values, an interpolated string computes the path data.

d = $"M {x} {y} L {x + w} {y} L {x + w / 2} {y + h} Z"