Skip to content

Polygons and paths ​

KeyMakes
polya polygon from its vertices, a regular polygon, or a regular star
pathany shape, written as the data of an SVG path

poly: a polygon ​

p=poly A,B,C,D
p=poly A,4,5
p=poly A,B,5
p=poly A,4,5,2
p=poly A,B,5,2

The key is always poly, and the values say how the polygon is made: only points, a point then numbers, or two points then a number.

ValuesThe polygon
A,B,C,...the vertices, in order. Three points at least
A,radius,na regular polygon with n sides: center A and radius radius. The radius is a number (in units of the x axis) or B:C, the distance between two points. The first vertex is above the center, the others follow counter-clockwise
A,B,na regular polygon with n sides, of which A and B are two consecutive vertices. The polygon is on the left of A toward B, as seen on the screen: for the other side, write B,A,n
...,n,kthe regular star {n/k}, see below

n is an integer from 3 to 1000.

pidraw
A(1,1)->label
B(6,1)->label
C(8,4)->label
D(3,5)->label
p=poly A,B,C,D->fill=yellow/0.3
pidraw
O(0,0)
p=poly O,3,6->fill=teal/0.3
q=poly O,1.5,3->red
pidraw
A(1,1)->label
B(4,1)->label
p=poly A,B,5->fill=orange/0.3
q=poly B,A,4->fill=blue/0.2

Stars ​

With a last number k, the polygon is the regular star {n/k}: the vertices are joined k by k. 5,2 is the pentagram. k is an integer from 1 (the convex polygon) to less than n/2. When n and k have a common divisor, the figure is made of several polygons: 6,2 gives two triangles. With A,B,n,k, [AB] is one of the strokes of the star: two vertices that it joins.

pidraw
O(0,0)
P(7,0)
s=poly O,3,5,2->red
h=poly P,3,6,2->blue
pidraw
A(1,1)
B(5,1)
s=poly A,B,5,2->fill=gold/0.4

A polygon with a vertex that has no position (a point that is not drawn, A and B the same point) is not drawn. The label is at the center of the vertices. A position that you give (label=p/tl) is kept.

Mistakes ​

pidraw
A(1,1)
B(4,2)
a=poly A,B
b=poly A,3,2
c=poly A,3,2000
d=poly A,3,5,3
LineWhat is wrong
3two points are not a polygon
4a polygon has 3 sides at least
5and 1000 at most
6k is less than n/2: 5,3 is the same star as 5,2

path: an SVG path ​

p=path M 0,0 L 2,1 Q 3,3 4,0 Z

The data of an SVG path (d), written in units of the graph and not in pixels: M 1,2 is the point (1; 2), the y axis goes up. The commas of the line split the values, which are put end to end: write the data as in an SVG file, with commas or spaces between the numbers.

CommandDescription
M x,ythe start of a piece (the first command is always M)
L x,y, H x, V ysegments
C x1,y1 x2,y2 x,y, S x2,y2 x,ycubic Bézier curves
Q x1,y1 x,y, T x,yquadratic Bézier curves
A rx,ry rotation large sweep x,yan arc of an ellipse
Zcloses the piece

Capital letters are positions, small letters are moves from the current point. A path can have several pieces (one M per piece). It is read as segments and cubic Bézier curves: an arc is drawn by a few curves that follow it.

pidraw
p=path M 0,0 L 2,1 L 3,0->red
q=path M 0,3 C 1,5 3,5 4,3 Z->fill=green/0.3
r=path M 1,1 A 1,1 0 0 1 2,2->blue,w=2
s=path M 4,0 L 5,0 L 5,1 L 4,1 Z M 4.3,0.3 L 4.7,0.3 L 4.7,0.7 L 4.3,0.7 Z->fill=orange/0.4

Data that are not the ones of a path (they do not start with M, an unknown command, a missing number) are an error.

pidraw
p=path L 2,1
q=path M 0,0 L 2

The label is at the middle of the longest piece, on the left of the way the path goes. A position that you give is kept. A point can follow a path: t=follow p, see curves.

Released under the MIT License.