2D-drawing

Scripting Languages
pygame drawing with recursion:
–
coordinates conversion
–
line & polygon drawing
–
recursion
–
exercises
Coordinates conversion
user coordinates
0,0
pygame window: win
xb,yb
s2
xa,ya
s1
translate + flip vert.
centerx, height
0,0
x, y → centerx + x, height − y
def
def drawline(xa,ya,xb,yb,
drawline(xa,ya,xb,yb, width):
width):
s1,s2
s1,s2 == (win.centerx+xa,win.height-ya),
(win.centerx+xa,win.height-ya), (win.centerx+xb,win.height-yb)
(win.centerx+xb,win.height-yb)
pygame.draw.line(scr,
black,
s1,
s2
,
width)
pygame.draw.line(scr, black, s1, s2 , width)
pygame.display.flip()
pygame.display.flip()
...
...
drawline(x1,y1,x2,y2,n)
drawline(x1,y1,x2,y2,n)
Pythagorian Tree
●
●
●
Draw the trunk
At the end of the trunk, split by some angle
and draw two branches
Repeat at the end of each branch until
a sufficient level of branching is reached
n=2
n=1
n=12
n=3
branches → 2n+1 − 1
Creating branches
branch
x4,y4
φ2
φ1
x2,y2
x1,y1
n
tru
k
branch
x3,y3
Coordinates rotation
x4,y4
branch
φ2
x,y
φ1
x2,y2
k
x3,y3
sin(φ) and cos(φ) are constant
and may be computed only once,
outside the drawing function
dx
dx
dx == (x2-x1)*scale
(x2-x1)*scale
dy
dy == (y2-y1)*scale
(y2-y1)*scale
= x cos(ϕ)− y sin(ϕ)
= x sin (ϕ)+ y cos(ϕ)
dy
x1,y1
n
tru
branch
x'
y'
## *scaling
*scaling factor
factor
x3
x3 == x2
x2 ++ dx*math.cos(phi1)
dx*math.cos(phi1) -- dy*math.sin(phi1)
dy*math.sin(phi1)
y3
=
y2
+
dx*math.sin(phi1)
+
dy*math.cos(phi1)
y3 = y2 + dx*math.sin(phi1) + dy*math.cos(phi1)
x4
x4 == x2
x2 ++ dx*math.cos(phi2)
dx*math.cos(phi2) -- dy*math.sin(phi2)
dy*math.sin(phi2)
y4
y4 == y2
y2 ++ dx*math.sin(phi2)
dx*math.sin(phi2) ++ dy*math.cos(phi2)
dy*math.cos(phi2)
Program structure
initialization
initialization
initialization
initialization
import
import pygame,
pygame, sys,
sys, math
math
black
=
(0,
0,
0);
white
black = (0, 0, 0); white == (255,
(255, 255,
255, 255)
255)
def
def drawtree(x1,y1,x2,y2,n):
drawtree(x1,y1,x2,y2,n):
drawline(x1,y1,x2,y2,n)
drawline(x1,y1,x2,y2,n)
pygame.init()
pygame.init()
scr
scr == pygame.display.set_mode((800,
pygame.display.set_mode((800, 550))
550))
scr.fill(white)
scr.fill(white)
win
win == scr.get_rect()
scr.get_rect()
here
heregoes
goesyour
yourcode
codeto
tocompute
computex3,y3,x4,y4
x3,y3,x4,y4
if
if nn >> 1:
1:
drawtree(x2,y2,x3,y3,
drawtree(x2,y2,x3,y3, n-1)
n-1)
drawtree(x2,y2,x4,y4,
n-1)
drawtree(x2,y2,x4,y4, n-1)
#pygame.display.flip()
#pygame.display.flip()
trunk=180
trunk=180
scale
scale == 0.7
0.7
phi1
=
math.radians(
phi1 = math.radians( 60)
60)
phi2
=
math.radians(-30)
phi2 = math.radians(-30)
drawtree(0,0,
drawtree(0,0, 0,
0, trunk,
trunk, 12)
12)
#pygame.display.flip()
#pygame.display.flip()
pygame
pygameloop
loop(empty)
(empty)
def
def drawline(xa,ya,xb,yb,
drawline(xa,ya,xb,yb, width):
width):
...
...
#pygame.display.flip()
#pygame.display.flip()
pygame
pygameloop
loop(empty)
(empty)
while
while True:
True:
for
for event
event in
in pygame.event.get():
pygame.event.get():
if
event.type
if event.type ==
== pygame.QUIT:
pygame.QUIT:
sys.exit()
sys.exit()
if
if event.type
event.type ==
== pygame.KEYDOWN:
pygame.KEYDOWN:
if
event.key
==
if event.key == pygame.K_RETURN:
pygame.K_RETURN:
sys.exit()
sys.exit()
Exercise 1
Polygon Pythagorian Tree
●
●
●
Draw the trunk-rectangle and hat-triangle
At both short sides of hat-triangle repeat the procedure
Repeat until a sufficient level of branching is reached
Exercise 2
●
use non-equilateral triangles
●
alternate triangle orientation
●
use depth-dependent coloring