logo mwo g2d, examples of programs
Themes: Herman grid illusion | Equilateral triangle, frame and shadow | Basic vectors | A simple polygon | The GO sign | Drunk spider | Pythagoras' theorem | Flying plane | Angles and segments | Intersection of sets | Thales' theorem | Text on a snake | Thales' theorem | Rotations, intersections | Using arrays and loops | Construction of x3, y3 | Starry night | Text in circle | Axes, millimeter paper | Marking congruent line segments | Simple Olympic rings | Stars and polygons | Marking bisected angles | Using subroutines | Euler's circle, the circle of the 9 points
Tip: before running an example, set the half box size to 2 and the box center to 0,0.
Herman grid illusion
background(black)
tickpenwidth(15)
tickcolor(white)
millimeter(0.5)
Equilateral triangle, frame and shadow
ss = square(O,3.5)
fillcolor(white)
shadow(ss)
draw(ss)

penwidth(20)
pencolor(orange * 3/2)
fillcolor((blue + 3*green)/2)
filldraw(triangle(O, 1))
Basic vectors
textsize(24)

A0 = (-1.5, 0.8)
A1 = (-0.5, 0.4)
A2 = (-1.3, -0.3)
A3 = (-1.7, -1.5)
u = (1.6, 0.3)

penwidth(1/4)
pencolor(gray)
line(A0, A0+u)
line(A1, A1+u)
line(A2, A2+u)
line(A3, A3+u)

penwidth(2)
pencolor(darker(cyan))
tickcolor(darker(cyan))
segment(A0,A0+u)
arrow(A0,A0+u)
name(A0+u/2, latex, "[tl]$\vec{u}$")

segment(A1,A1+u)
arrow(A1,A1+u)
name(A1+u/2, latex, "[tl]$\vec{u}$")

pencolor(darker(magenta))
tickcolor(darker(magenta))
segment(A2,A2+u)
arrow(A2+u,A2)
name(A2+u/2, latex, "[tl]$-\vec{u}$")

pencolor(gray)
tickcolor(gray)
segment(A3,A3+2*u)
arrow(A3,A3+2*u)
name(A3+u, latex, "[tl]$2.\vec{u}$")
A simple polygon
x1 = -1.5
x2 = 0.7
w = 0.5
z = 0.5

filldraw(polygon((x1,w), (x2,w), (x2,w+z), (x2+w+z,0), (x2,-w-z), (x2,-w), (x1,-w)))

textcolor(white)
textfont("Arial")
textstyle("bold")
textsize(40)
name(O,"[h]I am with him")
The GO sign
P = octagon(O, 1.8)
green = darker(green)

penwidth(19)
pencolor(green)
draw(P)

penwidth(17)
pencolor(white)
fillcolor(green)
drawfill(P)

textfont("Arial")
textsize(120)
textstyle("bold")
textcolor(white)
name(O, "[h]GO")
Drunk spider
a0 = -30°
r0 = 0.25/2
r1 = 0.4/2
n1 = 12

da = 360° / n1
dr = r1 / n1
n = trunc((2 - r0) / dr)

P[n]
Rnd = array(n, -0.1, 0.1, random)
for i=1 to n
a = a0 + i * da
r = r0 + i* dr + Rnd[i]
P[i] = polar(r,a)
end

penwidth(2)
brokenline(P)

penwidth(1/2)
for i=n-n1+1 to n do segment(O,P[i])
Pythagoras' theorem
a = (-ux-uy, polar(1,50°)-uy, ux-uy)
sq[3]
T=polygon(a)

for i = 1 to 3
    p1 = a[i+1]
    p4 = a[i+2]
    p2 = rotate(p4, p1, -90°)
    p3 = rotate(p1, p4, 90°)
    sq[i] = polygon(p1, p2, p3, p4)
end

sq[1] = translate(sq[1], 2*uy)
sq[3]=symmetric(sq[3], line(a[1], a[2]))

fillcolor((1,0,0,0.08))
fill(sq[1])
fillcolor((0,0,1,0.08))
fill(sq[3])

T1=translate(T, 2*uy)
fillcolor((1,1,0,1/4))
fill(T1)
fillcolor((1,1,0,1))
fill(T)

T2=rotate(T, -ux-uy, 90°)
T3=translate(T2, 2*ux)
fillcolor((0.5,1,0.5,1/4))
fill(T2)
fillcolor((0.5,1,0.5,1))
fill(T3)

draw(sq)
pencolor(blue/3)
penwidth(2)
draw(T)
Flying plane
sky(40)
pencolor(white)
nose=(-0.5,1.5)
tail=(-0.4,1.5)
polygon(nose, tail, tail+0.01*(ux+uy), tail+0.01*ux)
pencolor((1,1,1,0.1))
penwidth(2)
segment((-0.3,1.5),(2.5,1.5))
Angles and segments
textsize(20)
textfont("Times")
textstyle("italic")
r=1.5
tickpenwidth(1.5)

penwidth(1/2)
circle(O,r)

A=polar(r,30°)
B=polar(r,90°)

penwidth(2)
segment(A,B)

pencolor(blue)
brokenline(A,O,B)
mark(A,O,B,2)

pencolor(red)
M=(polar(r,-60°), polar(r,190°), polar(r,140°))
for i=1 to 3
    brokenline(A,M[i],B)
    mark(A,M[i],B,1)
end

smartname(A,B,"A")
smartname(M,"M_1")
name(O,"[bl]O")

textfont("script")
textsize(30)
name(polar(r,55°),"C")
Intersection of sets
A = potato((-1.2,1.4), (0.2,1.6), (0.2,0.4), (-1.2,0.6))
B = potato((-1.5,1), (0.5,0.8), (0.5,-0.8), (-1.5,-1))

fillcolor(lighter(cyan))
fill(A)
fillcolor(lighter(magenta))
fill(B)
fillcolor(red*0.8)
fill(intersect(A,B))

pencolor(yellow)
penwidth(8)
draw(A,B)

textsize(40)
textstyle("italic")
textfont("Times")

name((0.5,1.8),"[h]A")
name(ux,"[h]B")

textcolor(white)
name((-0.5,0.8),"[h]A \cap B")
Thales' theorem
Ctr = -uy/2
r = 1.5
A = Ctr + polar(r, 180°)
B = Ctr + polar(r, 0°)
C = Ctr + polar(r, 57°)

arc(A,C,B)
segment(A,B)

penwidth(2)
textsize(24)
ticksize(2)

brokenline(A,C,B)
mark(A,C,B)
smartname(A,B,C,"A")

r = rectangle(Ctr - uy/2, 2, 0.9)
fillcolor(white)
ticksize(1)
shadow(r)
draw(r)

textjustification(centered)
write(Ctr - uy/2,"An inscribed angle
in a semicircle
is a right angle.")
Text on a snake
textsize(22)
textfont("Courier")
textstyle("bold")

s = "this is the plot of the cosine function"
le = letters(s)
n = size(le)
per = pi/2

for i=1 to n
x = 4*(i - (n+1)/2)/n
c = cos(2*pi*x/per)
s = sin(2*pi*x/per)
y = 0.2*c
if (c < 0) then textcolor(0)
if (c > 0) then textcolor(1)
a = -0.2*2*pi/per*s
texttilt(arctan(a))
name((x,y),"[h]" & le[i])
end

background(0.7)
Thales' theorem
A = (1.8, 1.9)
B = (1.7, 1)
C = (1.5, 1.6)
L1 = line(A,B)
L2 = line(A,C)

draw(L1, L2)

P1 = translate(Ox, uy * 0.7)
P2 = Ox
P3 = translate(Ox, - uy * 1.2)

pencolor(cyan/2)
penwidth(2)
draw(P1, P2, P3)

P1L1 = intersect(P1, L1)
P1L2 = intersect(P1, L2)
P2L1 = intersect(P2, L1)
P2L2 = intersect(P2, L2)
P3L1 = intersect(P3, L1)
P3L2 = intersect(P3, L2)
L1L2 = intersect(L1, L2)

ticksize(2)
tickpenwidth(1)

tickcolor(orange)
mark(segment(L1L2, P1L1),1)
mark(segment(P1L1, P2L1),2)
mark(segment(P2L1, P3L1),3)

tickcolor(blue*0.8)
mark(segment(L1L2, P1L2),1)
mark(segment(P1L2, P2L2),2)
mark(segment(P2L2, P3L2),3)
Rotations, intersections
C=circle(0.7*uy,1)
Y=rotate(C,O,120°)
M=rotate(Y,O,120°)

fillcolor(cyan)
fill(C)
fillcolor(yellow)
fill(Y)
fillcolor(magenta)
fill(M)

fillcolor(blue)
fill(intersect(C,M))
fillcolor(red)
fill(intersect(Y,M))
fillcolor(green)
fill(intersect(C,Y))

fillcolor(black)
fill(intersect(C,Y,M))
Using arrays and loops
c=array(3)
c[0]=circle(0.7*uy,1)
for i=1 to 2 do c[i]=rotate(c[i-1],O,120°)

for i=1 to 3
col=(0,0,0)
col[i]=1
fillcolor(col)
fill(c[i])
end

for i=1 to 3
col=(1,1,1)
col[i+2]=0
fillcolor(col)
fill(intersect(c[i],c[i+1]))
end

fillcolor(white)
fill(intersect(c[0],c[1],c[2]))
Construction of x3, y3
textsize(16)

C=circle(O,1)
penwidth(1/2)
draw(C)

M=polar(1,35°)
mark(M,C)
smartname(M,"M = (x, y)")

P=project(M,Ox)
Q=project(M,Oy)
draw(Ox,Oy)
penwidth(1/4)
line(M,Q)
line(M,P)
mark(Q,Oy)
mark(P,Ox)
name(P,Q,"[br]P","[tr]Q")

Mprime=project(M,line(P,Q))
mark(M,Mprime,Q)
penwidth(1)
segment(P,Q)
segment(M,Mprime)
smartname(Mprime,"M' = (x^3, y^3)")
Starry night
global r

function star5(c)
    V[5]
    for i = 1 to 5 do V[i] = c + polar(r, 90° + 4*pi * i/5)
    fill(polygon(V))
end

background(blue / 3)
fillcolor(yellow)
r = 0.1
for i = 1 to 20 do star5(array(2, -2,2, random))
Text in circle
textsize(30)
textfont("Courier")
textstyle("bold")

s = "What a wonderful world!"
le = letters(s)
n = size(le)
r = 1.5
a0 = 315°/(n-1)
for i=1 to n
    textcolor((1 - i / n, 1 / 2, i / n))
    a = ( i - (n+1)/2 ) * a0
    p = polar(r, 90° - a)
    texttilt(-a)
    name(p,"[h]" & le[i])
end
Axes, millimeter paper
textfont("Times")
textstyle("italic")

Yformula = "2 * cos(3*x) * sin(9*10*x^2)"
P=array(400,("2*x",Yformula))
pencolor(brown)
brokenline(P)
penwidth(1/2)
draw(Ox,Oy)

mark(ux,Ox)
mark(uy,Oy)

name(ux,"[b]1")
name(O,"[lb]O")
name(uy,"[l]1")

arrow(2*ux,0)
arrow(2*uy,90°)

name(2*ux,2*uy,"[tl]x","[rb]y")

tickcolor(yellow)
millimeter(1,0.2,0.04)
Marking congruent line segments
textsize(20)
textfont("Times")
textstyle("italic")

A=((-1.8,-1),(1.8,-1),(0.8,1.2))
polygon(A)
smartname(A,"A")

Bisectors=array(3)
AH=array(3)
HNames=("[rb]A_H","[l]B_H","[bl]C_H")

pencolor(blue)
for i=1 to 3
    Bisectors[i]=ray(bisector(A[i-1],A[i+1]))
    AH[i]=center(A[i-1],A[i+1])
end

draw(Bisectors)

name(AH,HNames)
X=intersect(Bisectors[1],Bisectors[2])

pencolor(green)
for i=1 to 3
    S=segment(A[i],X)
    draw(S)
    mark(S,1)
end

for i=1 to 3 do mark(X,AH[i],A[i-1])
Simple Olympic rings
r=0.5
dx=r*1.2
dy=r
cblack=(0,1)
cblue=cblack-2*(dx,0)
cred=cblack+2*(dx,0)
cyellow=cblack-(dx,dy)
cgreen=cblack+(dx,-dy)

penwidth(10)

pencolor(blue)
circle(cblue,r)

pencolor(yellow)
circle(cyellow,r)
pencolor(blue)
arc(cblue,r,-30°,30°)

pencolor(black)
circle(cblack,r)
pencolor(yellow)
arc(cyellow,r,60°,90°)

pencolor(green)
circle(cgreen,r)
pencolor(black)
arc(cblack,r,-30°,30°)

pencolor(red)
circle(cred,r)
pencolor(green)
arc(cgreen,r,60°,90°)
Stars and polygons
n=5
r=1.5

v[n]
for i=0 to n-1
v[i]=r*(sin(2*i*360°/n),cos(2*i*360°/n))
end

penwidth(10)
pencolor(yellow)
fillcolor(magenta)
drawfill(polygon(v))
Marking bisected angles
textfont("Times")
textsize(24)
textstyle("italic")

A=((-1.7,-1),(1.7,-1),(0.8,1.2))
polygon(A)
smartname(A,"A")

R=array(3)

for i=1 to 3
    e1=ray(A[i],A[i+1])
    e2=ray(A[i],A[i-1])
    R[i]=bisector(e1,e2)
end

pencolor(red)
draw(R)

X=intersect(R[1],R[2])

tickcolor(black)
for i=1 to 3 do mark(A[i+1],A[i],X,i)

ticksize(1.5)
for i=1 to 3 do mark(X,A[i],A[i-1],i)
Using subroutines
r=0.5
dx=r*1.2
dy=r
cblack=(0,1)
cblue=cblack-2*(dx,0)
cred=cblack+2*(dx,0)
cyellow=cblack-(dx,dy)
cgreen=cblack+(dx,-dy)

function Specialcircle(c,r,col)
penwidth(18)
pencolor(white)
circle(c,r)
penwidth(12)
pencolor(col)
circle(c,r)
end

function Specialarc(c,r,a1,a2,col)
penwidth(18)
pencolor(white)
arc(c,r,a1,a2)
penwidth(12)
pencolor(col)
arc(c,r,a1,a2)
end

Specialcircle(cblue,r,blue)
Specialcircle(cyellow,r,yellow)
Specialarc(cblue,r,-30°,30°,blue)

Specialcircle(cblack,r,black)
Specialarc(cyellow,r,60°,90°,yellow)

Specialcircle(cgreen,r,green)
Specialarc(cblack,r,-30°,30°,black)

Specialcircle(cred,r,red)
Specialarc(cgreen,r,60°,90°,green)
Euler's circle, the circle of the 9 points
textfont("Times")
textsize(14)
textstyle("italic")

a = (-1.8,-0.5)
b = (1.8,-0.5)
c = (0.8,1.7)
cc = circle(a,b,c)
ce = center(cc)

smartnamecenter(center(a,b,c))

ah = project(a,line(b,c))
am = center(b,c)
bh = project(b,line(a,c))
bm = center(a,c)
ch = project(c,line(b,a))
cm = center(b,a)

smartname(a,b,c,"A")
smartname(ah,bh,ch,"A_H")
smartname(am,bm,cm,"A'")

c9 = circle(ah, bh, ch)
name(center(c9), "[l]O_{Euler}",ce,"[l]O_m")

draw(polygon(a,b,c),cc,c9)

penwidth(0.5)
pencolor("red")

segment(a,ah)
segment(b,bh)
segment(c,ch)
mark(a,ah,b)
mark(b,bh,c)
mark(c,ch,a)

pencolor("blue")
segment(a,am)
segment(b,bm)
segment(c,cm)

penwidth(1)
pencolor("black")

Oh = intersect(line(a,ah),line(b,bh))
LEuler = line(ce,center(c9))
draw(LEuler)
mark(ce,LEuler)
mark(Oh,LEuler)
mark(center(c9),LEuler)
name(Oh,"[l]O_H")

a2 = center(a,Oh)
b2 = center(b,Oh)
c2 = center(c,Oh)
mark(a2,line(a,Oh))
mark(b2,line(b,Oh))
mark(c2,line(c,Oh))