Example Code

Example 1: A Tree!

Here is a simple example drawing a tree with a rectangle and three triangles. The background is set to sky blue with a hex color value, and the triangles are rotated 30 degrees.

import easy_draw
easy_draw.load_canvas()
easy_draw.set_canvas_color("#87CEEB")
trunk = easy_draw.Rectangle(
xy = (250, 300),
width = 100,
height = 310,
color = (160, 82, 45)
)
top = easy_draw.RegPolygon(
nsides = 3,
center_xy = (300, 175),
radius = 125,
color = (0, 255, 0)
)
top.rotate(30)
middle = easy_draw.RegPolygon(
nsides = 3,
center_xy = (300, 275),
radius = 175,
color = (0, 255, 0)
)
middle.rotate(30)
bottom = easy_draw.RegPolygon(
nsides = 3,
center_xy = (300, 400),
radius = 250,
color = (0, 255, 0)
)
bottom.rotate(30)
easy_draw.end()

Example 2: Property Change and Event Handling

Here is an example of drawing a square and an octagon, then animating them when clicked.

When the square is clicked, it changes color to a new random value and rotates by 10 degrees.

When the octagon is clicked, it changes color to a new random value and rotates by 10 degrees.

import easy_draw
import random
easy_draw.load_canvas()
colors = ["blue", "red", "yellow", "green", "orange", "purple"]
square = easy_draw.Rectangle(
xy = (100, 100),
width = 200,
height = 200
)
def click_square(event):
global square
random_color = random.choice(colors)
square.set_property(color = random_color)
square.rotate(10)
square.event_setup("<Button-1>", click_square)
octagon = easy_draw.RegPolygon(
center_xy = (400, 400),
radius = 100,
nsides = 8
)
def click_octagon(event):
global octagon
random_color = random.choice(colors)
octagon.set_property(color = random_color)
octagon.rotate(10)
octagon.event_setup("<Button-1>", click_octagon)
easy_draw.end()

Example 3: Decagon Islamic Design

Provided by Jackie Lin from Taiwan CSHS (Chung-shan Senior High School)

import easy_draw
import math
from shapely import affinity
from shapely.geometry import LineString, Point, Polygon
def out_n_polygon_of_circle(line_1, line_2, n):
its_0 = line_1.intersection(line_2)
its_0_xy = (its_0.x, its_0.y)
poly_xy = [its_0_xy]
for i in range(1, n+1, 1):
its_i = affinity.rotate(its_0, i*360/n, (0, 0))
its_i_xy = (its_i.x, its_i.y)
poly_xy.append(its_i_xy)
return poly_xy
factor = 37
cxy = (0, 0)
r0 = 8*factor
r1 = r0/2
p0_xy = (r1*math.cos(0*math.pi/10), r1*math.sin(0*math.pi/10))
p2_xy = (r1*math.cos(2*math.pi/10), r1*math.sin(2*math.pi/10))
p3_xy = (r1*math.cos(3*math.pi/10), r1*math.sin(3*math.pi/10))
p4_xy = (r1*math.cos(4*math.pi/10), r1*math.sin(4*math.pi/10))
p6_xy = (r1*math.cos(6*math.pi/10), r1*math.sin(6*math.pi/10))
p8_xy = (r1*math.cos(8*math.pi/10), r1*math.sin(8*math.pi/10))
p10_xy = (r1*math.cos(10*math.pi/10), r1*math.sin(10*math.pi/10))
p18_xy = (r1*math.cos(18*math.pi/10), r1*math.sin(18*math.pi/10))
line_p2_p8 = LineString([p2_xy, p8_xy])
line_p2_00 = LineString([p2_xy, (0, 0)])
line_p3_00 = LineString([p3_xy, (0, 0)])
line_p4_00 = LineString([p4_xy, (0, 0)])
its_21 = line_p3_00.intersection(line_p2_p8)
its_22 = affinity.rotate(its_21, 36, (0, 0))
its_23 = affinity.rotate(its_21, 72, (0, 0))
its_21_xy = (its_21.x, its_21.y)
its_22_xy = (its_22.x, its_22.y)
its_23_xy = (its_23.x, its_23.y)
star_edge_xy = [p2_xy, its_21_xy, p4_xy]
star_edge = LineString(star_edge_xy)
its_41 = line_p4_00.intersection(line_p2_p8)
its_41_xy = (its_41.x, its_41.y)
star_edge_xy = [its_21_xy, its_41_xy, its_22_xy]
star_edge = LineString(star_edge_xy)
H_line = LineString([(0, 0), (8*factor, 0)])
r_H_line = affinity.rotate(H_line, 18, cxy)
V_Rline = LineString([(r1, 0), (r1, 8*factor)])
returned_xy_list = out_n_polygon_of_circle(r_H_line, V_Rline, 10)
decagon_xy = returned_xy_list
decagon = LineString(decagon_xy)
line_p0_p6 = LineString([p0_xy, p6_xy])
line_p4_p18 = LineString([p4_xy, p18_xy])
its_1 = r_H_line.intersection(V_Rline)
its_2 = r_H_line.intersection(line_p0_p6)
its_3 = H_line.intersection(line_p4_p18)
its_4 = line_p0_p6.intersection(line_p4_p18)
its_1_xy = (its_1.x, its_1.y)
its_2_xy = (its_2.x, its_2.y)
its_3_xy = (its_3.x, its_3.y)
its_4_xy = (its_4.x, its_4.y)
its_5_xy = (its_1.x, -its_1.y)
its_5 = Point(its_5_xy)
its_6 = affinity.rotate(its_1, -108, its_5_xy)
its_6_xy = (its_6.x, its_6.y)
its_7 = affinity.rotate(its_5, -108, its_6_xy)
its_7_xy = (its_7.x, its_7.y)
its_8 = affinity.rotate(its_6, -108, its_7_xy)
its_8_xy = (its_8.x, its_8.y)
pentagon_line = LineString([its_1_xy, its_5_xy, its_6_xy, its_7_xy, its_8_xy])
pentagon = Polygon([its_1_xy, its_5_xy, its_6_xy, its_7_xy, its_8_xy, its_1_xy])
pentagon_cen = pentagon.centroid
pentagon_cen_xy = (pentagon_cen.x, pentagon_cen.y)
pen_line_5_to_6 = LineString([its_5_xy, its_6_xy])
mid = pen_line_5_to_6.interpolate(0.5, normalized=True)
mid_xy = (mid.x, mid.y)
pen_line_1 = LineString([p0_xy, pentagon_cen_xy])
pen_line_2 = LineString([mid_xy, pentagon_cen_xy])
dart_line_1_extend = affinity.rotate(pen_line_1, -36, p0_xy)
dart_line_2_extend = affinity.rotate(pen_line_2, 36, mid_xy)
its_dart = dart_line_1_extend.intersection(dart_line_2_extend)
its_dart_xy = (its_dart.x, its_dart.y)
dart_xy = [p0_xy, its_dart_xy, mid_xy, pentagon_cen_xy, p0_xy]
dart = LineString(dart_xy)
dart_edge = LineString([p0_xy, its_dart_xy, mid_xy])
dart_edge_1 = LineString([p0_xy, its_dart_xy, mid_xy])
dart_edge_2 = affinity.rotate(dart_edge_1, 72, pentagon_cen_xy)
dart_edge_3 = affinity.rotate(dart_edge_1, 144, pentagon_cen_xy)
dart_edge_4 = affinity.rotate(dart_edge_1, 216, pentagon_cen_xy)
dart_edge_5 = affinity.rotate(dart_edge_1, 288, pentagon_cen_xy)
star_p01 = dart_edge_1.coords[:][0]
star_p02 = dart_edge_1.coords[:][1]
star_p03 = dart_edge_1.coords[:][2]
star_p04 = dart_edge_2.coords[:][1]
star_p05 = dart_edge_2.coords[:][2]
star_p06 = dart_edge_3.coords[:][1]
star_p07 = dart_edge_3.coords[:][2]
star_p08 = dart_edge_4.coords[:][1]
star_p09 = dart_edge_4.coords[:][2]
star_p10 = dart_edge_5.coords[:][1]
star_p11 = dart_edge_5.coords[:][2]
star_xy = [star_p01, star_p02, star_p03, star_p04, star_p05, star_p06, star_p07, star_p08, star_p09, star_p10, star_p11]
star = LineString(star_xy)
pentagon = LineString([its_1_xy, its_5_xy, its_6_xy, its_7_xy, its_8_xy])
easy_draw.load_canvas()
colors = ["black", "blue", "red", "yellow", "green", "orange", "purple"]
cir_r1 = easy_draw.Circle(
center_xy=(300, 300),
radius=r1,
color=(100, 149, 237)
)
r2 = its_2.distance(Point(0, 0))
s01_xy = (r2*math.cos(1*math.pi/10), r2*math.sin(1*math.pi/10))
kite_xy = [p0_xy, its_1_xy, p2_xy, s01_xy]
kite = LineString(kite_xy)
for degree in (0, 36, 72, 108, 144, 180, 216, 252, 288, 324):
r_kite = affinity.rotate(kite, degree, (0, 0))
t_r_kite = affinity.translate(r_kite, 300, 300, 0)
xy_list = []
for xy in t_r_kite.coords[:]:
xy_list.append(xy[0])
xy_list.append(xy[1])
poly_kite = easy_draw.Polygon(points_list=xy_list, color=(153, 50, 204), border_color=colors[0], border_width=2)
out_star_edge_xy = [p2_xy, its_21_xy, p4_xy]
out_star_edge = LineString(out_star_edge_xy)
for i in range(1, 10+1, 1):
r_star_edge = affinity.rotate(out_star_edge, i*36, (0, 0))
t_star_edge = affinity.translate(r_star_edge, 300, 300, 0)
xy_list = []
for xy in t_star_edge.coords[:]:
xy_list.append(xy[0])
xy_list.append(xy[1])
edge_line = easy_draw.Line(points_list=xy_list, thickness=2, color=colors[0], style="cut")
in_star_edge_xy = [its_21_xy, its_41_xy, its_22_xy]
in_star_edge = LineString(in_star_edge_xy)
for i in range(1, 10+1, 1):
r_star_edge = affinity.rotate(in_star_edge, i*36, (0, 0))
t_star_edge = affinity.translate(r_star_edge, 300, 300, 0)
xy_list = []
for xy in t_star_edge.coords[:]:
xy_list.append(xy[0])
xy_list.append(xy[1])
edge_line = easy_draw.Line(points_list=xy_list, thickness=2, color=colors[0], style="cut")
for degree in (0, 36, 72, 108, 144, 180, 216, 252, 288, 324):
r_pentagon_line = affinity.rotate(pentagon_line, degree, (0, 0))
t_r_pentagon_line = affinity.translate(r_pentagon_line, 300, 300, 0)
xy_list = []
for xy in t_r_pentagon_line.coords[:]:
xy_list.append(xy[0])
xy_list.append(xy[1])
poly_petagon = easy_draw.Polygon(points_list=xy_list, color=(173, 216, 230), border_color=colors[0], border_width=2)
r_star = affinity.rotate(star, degree, (0, 0))
t_r_star = affinity.translate(r_star, 300, 300, 0)
xy_list = []
for xy in t_r_star.coords[:]:
xy_list.append(xy[0])
xy_list.append(xy[1])
poly_star = easy_draw.Polygon(points_list=xy_list, color=(255, 215, 0), border_color=colors[0], border_width=2)
dy = 2 * its_1.y
up_tmp = affinity.rotate(its_1, 72, (0, 0))
pentagon_right_tip = affinity.rotate(Point(its_5_xy), 144, pentagon_cen_xy)
up_point_1 = affinity.rotate(pentagon_right_tip, 72, (0, 0))
up_point_1_xy = (up_point_1.x, up_point_1.y)
up_point_2_xy = (-up_point_1.x, up_point_1.y)
up_point_3_xy = (up_tmp.x, up_tmp.y + dy)
up_tri = LineString([up_point_1_xy, up_point_2_xy, up_point_3_xy])
for degree in (0, 36, 72, 108, 144, 180, 216, 252, 288, 324):
r_up_tri = affinity.rotate(up_tri, degree, (0, 0))
t_r_up_tri = affinity.translate(r_up_tri, 300, 300, 0)
xy_list = []
for xy in t_r_up_tri.coords[:]:
xy_list.append(xy[0])
xy_list.append(xy[1])
poly_tri = easy_draw.Polygon(points_list=xy_list, color=(143, 188, 143), border_color=colors[0], border_width=2)
easy_draw.end()

More examples coming soon!