Skip to content

Instantly share code, notes, and snippets.

@SpaceVoyager
Created May 31, 2015 14:40
Show Gist options
  • Save SpaceVoyager/190c1f2cc76d8bc42478 to your computer and use it in GitHub Desktop.
Save SpaceVoyager/190c1f2cc76d8bc42478 to your computer and use it in GitHub Desktop.
forest.py
# draws a bunch of Christmas trees
# writen by Yuhang Wang for A2PyKids
import canvas
from math import sin, cos, pi
# draws a star
def draw_star(x, y, inner_r, outer_r, jags):
canvas.set_fill_color(1,1,0.2)
canvas.move_to(x, y + outer_r)
for i in xrange(jags * 2):
angle = (2 * pi / jags) * i / 2
if i % 2 == 0:
r = outer_r
else:
r = inner_r
a = x + r * sin(angle)
b = y + r * cos(angle)
canvas.add_line(a, b)
canvas.close_path()
canvas.set_line_width(1)
canvas.fill_path()
# draws a tree rooted at point (x, y) with a star on top
def draw_tree(x, y, width, height):
# draws the tree leaves
canvas.set_fill_color(0,0.6,0, 0.6)
trunk_height = height/6.5
canvas.begin_path()
canvas.move_to(x - width/2, y + trunk_height)
canvas.add_line(x + width/2, y + trunk_height)
canvas.add_line(x, y + height)
canvas.close_path()
canvas.fill_path()
# draws the tree trunk
canvas.set_line_width(width/5)
canvas.set_stroke_color(0.5, 0.4, 0.32)
canvas.draw_line(x, y, x, y + trunk_height)
draw_star(x, y + height + 9, 15, 30, 5)
canvas.set_size(1000, 600)
# draws 2 rows of trees with 10 trees in each row
for x in range(10):
draw_tree(x*100 + 50, 300, 90, 230)
draw_tree(x*100 + 50, 40, 90, 230)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment