Easy Draw Module
Search…
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.
1
import easy_draw
2
​
3
easy_draw.load_canvas()
4
​
5
easy_draw.set_canvas_color("#87CEEB")
6
​
7
trunk = easy_draw.Rectangle(
8
xy = (250, 300),
9
width = 100,
10
height = 310,
11
color = (160, 82, 45)
12
)
13
​
14
top = easy_draw.RegPolygon(
15
nsides = 3,
16
center_xy = (300, 175),
17
radius = 125,
18
color = (0, 255, 0)
19
)
20
top.rotate(30)
21
​
22
middle = easy_draw.RegPolygon(
23
nsides = 3,
24
center_xy = (300, 275),
25
radius = 175,
26
color = (0, 255, 0)
27
)
28
middle.rotate(30)
29
​
30
bottom = easy_draw.RegPolygon(
31
nsides = 3,
32
center_xy = (300, 400),
33
radius = 250,
34
color = (0, 255, 0)
35
)
36
bottom.rotate(30)
37
​
38
easy_draw.end()
Copied!

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.
1
import easy_draw
2
import random
3
​
4
easy_draw.load_canvas()
5
​
6
colors = ["blue", "red", "yellow", "green", "orange", "purple"]
7
​
8
square = easy_draw.Rectangle(
9
xy = (100, 100),
10
width = 200,
11
height = 200
12
)
13
​
14
def click_square(event):
15
global square
16
random_color = random.choice(colors)
17
square.set_property(color = random_color)
18
square.rotate(10)
19
​
20
square.event_setup("<Button-1>", click_square)
21
​
22
octagon = easy_draw.RegPolygon(
23
center_xy = (400, 400),
24
radius = 100,
25
nsides = 8
26
)
27
​
28
def click_octagon(event):
29
global octagon
30
random_color = random.choice(colors)
31
octagon.set_property(color = random_color)
32
octagon.rotate(10)
33
​
34
octagon.event_setup("<Button-1>", click_octagon)
35
​
36
easy_draw.end()
Copied!
​

Example 3: Decagon Islamic Design

Provided by Jackie Lin from Taiwan CSHS (Chung-shan Senior High School)
1
import easy_draw
2
import math
3
from shapely import affinity
4
from shapely.geometry import LineString, Point, Polygon
5
​
6
def out_n_polygon_of_circle(line_1, line_2, n):
7
its_0 = line_1.intersection(line_2)
8
its_0_xy = (its_0.x, its_0.y)
9
poly_xy = [its_0_xy]
10
for i in range(1, n+1, 1):
11
its_i = affinity.rotate(its_0, i*360/n, (0, 0))
12
its_i_xy = (its_i.x, its_i.y)
13
poly_xy.append(its_i_xy)
14
return poly_xy
15
​
16
factor = 37
17
cxy = (0, 0)
18
r0 = 8*factor
19
r1 = r0/2
20
​
21
p0_xy = (r1*math.cos(0*math.pi/10), r1*math.sin(0*math.pi/10))
22
p2_xy = (r1*math.cos(2*math.pi/10), r1*math.sin(2*math.pi/10))
23
p3_xy = (r1*math.cos(3*math.pi/10), r1*math.sin(3*math.pi/10))
24
p4_xy = (r1*math.cos(4*math.pi/10), r1*math.sin(4*math.pi/10))
25
p6_xy = (r1*math.cos(6*math.pi/10), r1*math.sin(6*math.pi/10))
26
p8_xy = (r1*math.cos(8*math.pi/10), r1*math.sin(8*math.pi/10))
27
p10_xy = (r1*math.cos(10*math.pi/10), r1*math.sin(10*math.pi/10))
28
p18_xy = (r1*math.cos(18*math.pi/10), r1*math.sin(18*math.pi/10))
29
​
30
line_p2_p8 = LineString([p2_xy, p8_xy])
31
line_p2_00 = LineString([p2_xy, (0, 0)])
32
line_p3_00 = LineString([p3_xy, (0, 0)])
33
line_p4_00 = LineString([p4_xy, (0, 0)])
34
​
35
its_21 = line_p3_00.intersection(line_p2_p8)
36
its_22 = affinity.rotate(its_21, 36, (0, 0))
37
its_23 = affinity.rotate(its_21, 72, (0, 0))
38
its_21_xy = (its_21.x, its_21.y)
39
its_22_xy = (its_22.x, its_22.y)
40
its_23_xy = (its_23.x, its_23.y)
41
​
42
star_edge_xy = [p2_xy, its_21_xy, p4_xy]
43
star_edge = LineString(star_edge_xy)
44
​
45
its_41 = line_p4_00.intersection(line_p2_p8)
46
its_41_xy = (its_41.x, its_41.y)
47
star_edge_xy = [its_21_xy, its_41_xy, its_22_xy]
48
star_edge = LineString(star_edge_xy)
49
​
50
H_line = LineString([(0, 0), (8*factor, 0)])
51
r_H_line = affinity.rotate(H_line, 18, cxy)
52
V_Rline = LineString([(r1, 0), (r1, 8*factor)])
53
returned_xy_list = out_n_polygon_of_circle(r_H_line, V_Rline, 10)
54
decagon_xy = returned_xy_list
55
decagon = LineString(decagon_xy)
56
​
57
line_p0_p6 = LineString([p0_xy, p6_xy])
58
line_p4_p18 = LineString([p4_xy, p18_xy])
59
​
60
its_1 = r_H_line.intersection(V_Rline)
61
its_2 = r_H_line.intersection(line_p0_p6)
62
its_3 = H_line.intersection(line_p4_p18)
63
its_4 = line_p0_p6.intersection(line_p4_p18)
64
its_1_xy = (its_1.x, its_1.y)
65
its_2_xy = (its_2.x, its_2.y)
66
its_3_xy = (its_3.x, its_3.y)
67
its_4_xy = (its_4.x, its_4.y)
68
​
69
its_5_xy = (its_1.x, -its_1.y)
70
its_5 = Point(its_5_xy)
71
its_6 = affinity.rotate(its_1, -108, its_5_xy)
72
its_6_xy = (its_6.x, its_6.y)
73
its_7 = affinity.rotate(its_5, -108, its_6_xy)
74
its_7_xy = (its_7.x, its_7.y)
75
its_8 = affinity.rotate(its_6, -108, its_7_xy)
76
its_8_xy = (its_8.x, its_8.y)
77
​
78
pentagon_line = LineString([its_1_xy, its_5_xy, its_6_xy, its_7_xy, its_8_xy])
79
​
80
pentagon = Polygon([its_1_xy, its_5_xy, its_6_xy, its_7_xy, its_8_xy, its_1_xy])
81
​
82
pentagon_cen = pentagon.centroid
83
pentagon_cen_xy = (pentagon_cen.x, pentagon_cen.y)
84
​
85
pen_line_5_to_6 = LineString([its_5_xy, its_6_xy])
86
mid = pen_line_5_to_6.interpolate(0.5, normalized=True)
87
mid_xy = (mid.x, mid.y)
88
pen_line_1 = LineString([p0_xy, pentagon_cen_xy])
89
pen_line_2 = LineString([mid_xy, pentagon_cen_xy])
90
​
91
dart_line_1_extend = affinity.rotate(pen_line_1, -36, p0_xy)
92
dart_line_2_extend = affinity.rotate(pen_line_2, 36, mid_xy)
93
​
94
its_dart = dart_line_1_extend.intersection(dart_line_2_extend)
95
its_dart_xy = (its_dart.x, its_dart.y)
96
dart_xy = [p0_xy, its_dart_xy, mid_xy, pentagon_cen_xy, p0_xy]
97
dart = LineString(dart_xy)
98
dart_edge = LineString([p0_xy, its_dart_xy, mid_xy])
99
​
100
dart_edge_1 = LineString([p0_xy, its_dart_xy, mid_xy])
101
dart_edge_2 = affinity.rotate(dart_edge_1, 72, pentagon_cen_xy)
102
dart_edge_3 = affinity.rotate(dart_edge_1, 144, pentagon_cen_xy)
103
dart_edge_4 = affinity.rotate(dart_edge_1, 216, pentagon_cen_xy)
104
dart_edge_5 = affinity.rotate(dart_edge_1, 288, pentagon_cen_xy)
105
​
106
star_p01 = dart_edge_1.coords[:][0]
107
star_p02 = dart_edge_1.coords[:][1]
108
star_p03 = dart_edge_1.coords[:][2]
109
star_p04 = dart_edge_2.coords[:][1]
110
star_p05 = dart_edge_2.coords[:][2]
111
star_p06 = dart_edge_3.coords[:][1]
112
star_p07 = dart_edge_3.coords[:][2]
113
star_p08 = dart_edge_4.coords[:][1]
114
star_p09 = dart_edge_4.coords[:][2]
115
star_p10 = dart_edge_5.coords[:][1]
116
star_p11 = dart_edge_5.coords[:][2]
117
​
118
star_xy = [star_p01, star_p02, star_p03, star_p04, star_p05, star_p06, star_p07, star_p08, star_p09, star_p10, star_p11]
119
star = LineString(star_xy)
120
​
121
pentagon = LineString([its_1_xy, its_5_xy, its_6_xy, its_7_xy, its_8_xy])
122
​
123
easy_draw.load_canvas()
124
​
125
colors = ["black", "blue", "red", "yellow", "green", "orange", "purple"]
126
​
127
cir_r1 = easy_draw.Circle(
128
center_xy=(300, 300),
129
radius=r1,
130
color=(100, 149, 237)
131
)
132
​
133
r2 = its_2.distance(Point(0, 0))
134
s01_xy = (r2*math.cos(1*math.pi/10), r2*math.sin(1*math.pi/10))
135
kite_xy = [p0_xy, its_1_xy, p2_xy, s01_xy]
136
kite = LineString(kite_xy)
137
​
138
for degree in (0, 36, 72, 108, 144, 180, 216, 252, 288, 324):
139
r_kite = affinity.rotate(kite, degree, (0, 0))
140
t_r_kite = affinity.translate(r_kite, 300, 300, 0)
141
xy_list = []
142
for xy in t_r_kite.coords[:]:
143
xy_list.append(xy[0])
144
xy_list.append(xy[1])
145
poly_kite = easy_draw.Polygon(points_list=xy_list, color=(153, 50, 204), border_color=colors[0], border_width=2)
146
​
147
out_star_edge_xy = [p2_xy, its_21_xy, p4_xy]
148
out_star_edge = LineString(out_star_edge_xy)
149
for i in range(1, 10+1, 1):
150
r_star_edge = affinity.rotate(out_star_edge, i*36, (0, 0))
151
t_star_edge = affinity.translate(r_star_edge, 300, 300, 0)
152
xy_list = []
153
for xy in t_star_edge.coords[:]:
154
xy_list.append(xy[0])
155
xy_list.append(xy[1])
156
edge_line = easy_draw.Line(points_list=xy_list, thickness=2, color=colors[0], style="cut")
157
​
158
in_star_edge_xy = [its_21_xy, its_41_xy, its_22_xy]
159
in_star_edge = LineString(in_star_edge_xy)
160
for i in range(1, 10+1, 1):
161
r_star_edge = affinity.rotate(in_star_edge, i*36, (0, 0))
162
t_star_edge = affinity.translate(r_star_edge, 300, 300, 0)
163
xy_list = []
164
for xy in t_star_edge.coords[:]:
165
xy_list.append(xy[0])
166
xy_list.append(xy[1])
167
edge_line = easy_draw.Line(points_list=xy_list, thickness=2, color=colors[0], style="cut")
168
​
169
for degree in (0, 36, 72, 108, 144, 180, 216, 252, 288, 324):
170
r_pentagon_line = affinity.rotate(pentagon_line, degree, (0, 0))
171
t_r_pentagon_line = affinity.translate(r_pentagon_line, 300, 300, 0)
172
xy_list = []
173
for xy in t_r_pentagon_line.coords[:]:
174
xy_list.append(xy[0])
175
xy_list.append(xy[1])
176
poly_petagon = easy_draw.Polygon(points_list=xy_list, color=(173, 216, 230), border_color=colors[0], border_width=2)
177
r_star = affinity.rotate(star, degree, (0, 0))
178
t_r_star = affinity.translate(r_star, 300, 300, 0)
179
xy_list = []
180
for xy in t_r_star.coords[:]:
181
xy_list.append(xy[0])
182
xy_list.append(xy[1])
183
poly_star = easy_draw.Polygon(points_list=xy_list, color=(255, 215, 0), border_color=colors[0], border_width=2)
184
​
185
dy = 2 * its_1.y
186
up_tmp = affinity.rotate(its_1, 72, (0, 0))
187
pentagon_right_tip = affinity.rotate(Point(its_5_xy), 144, pentagon_cen_xy)
188
up_point_1 = affinity.rotate(pentagon_right_tip, 72, (0, 0))
189
up_point_1_xy = (up_point_1.x, up_point_1.y)
190
up_point_2_xy = (-up_point_1.x, up_point_1.y)
191
up_point_3_xy = (up_tmp.x, up_tmp.y + dy)
192
​
193
up_tri = LineString([up_point_1_xy, up_point_2_xy, up_point_3_xy])
194
for degree in (0, 36, 72, 108, 144, 180, 216, 252, 288, 324):
195
r_up_tri = affinity.rotate(up_tri, degree, (0, 0))
196
t_r_up_tri = affinity.translate(r_up_tri, 300, 300, 0)
197
xy_list = []
198
for xy in t_r_up_tri.coords[:]:
199
xy_list.append(xy[0])
200
xy_list.append(xy[1])
201
poly_tri = easy_draw.Polygon(points_list=xy_list, color=(143, 188, 143), border_color=colors[0], border_width=2)
202
​
203
easy_draw.end()
Copied!
More examples coming soon!
​
​
Last modified 9mo ago