-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMondrian Art Color.py
134 lines (121 loc) · 3.56 KB
/
Mondrian Art Color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Piotr Smietana
# CS1210
# Homework 12
# Draw random "art" in a Mondrian style
import turtle
import random
# Height and width of the turtle window
WIDTH = 1024
HEIGHT = 768
# Draw a rectange with the lower Right corner at (x, y)
def drawSquare(x, y, w, h, color,width, tur):
tur.up()
tur.goto(x,y)
tur.setheading(90)
tur.down()
tur.width(int(width))
tur.begin_fill()
tur.fillcolor(color)
for I in range(2):
tur.forward(h)
tur.right(90)
tur.forward(w)
tur.right(90)
tur.end_fill()
tur.up()
# Select a color randomly
def randomColor(y):
# Define color lists
redsList = ["red","#FF6A6A","#CD2626"]
greensList = ["green","#00C957","#7FFF00"]
bluesList = ["blue","skyblue","#00CED1"]
brownsList = ["#9C661F","#D2691E","#D2B48C","#CDC8B1","#F8F8F8"]
yellowsList = ["yellow","#FFD700","#FFB90F"]
# Colors based on height of square
if y < 120:
return random.choice(yellowsList)
elif y < 300:
return random.choice(brownsList)
elif y < 450:
return random.choice(bluesList)
elif y < 640:
return random.choice(greensList)
else:
return random.choice(redsList)
# Select pen size randomly
def randomPenSize():
penSize = random.randint(1,3)
return penSize
# Split horizontally
def splitHoriz(x, y, w, h, tur):
splitPoint = random.randint(33, 67) / 100
leftWidth = splitPoint * w
rightWidth = w - leftWidth
art(x, y, leftWidth, h, tur)
art(x + leftWidth, y, rightWidth, h, tur)
# Split vertically
def splitVert(x, y, w, h, tur):
splitPoint = random.randint(33, 67) / 100
topHeight = splitPoint * h
bottomHeight = h - topHeight
art(x, y, w, topHeight, tur)
art(x, y + topHeight, w, bottomHeight, tur)
# Split both vertically and horizontally
def splitBoth(x, y, w, h, tur):
splitPointH = random.randint(33, 67) / 100
splitPointV = random.randint(33, 67) / 100
leftWidth = splitPointH * w
rightWidth = w - leftWidth
topHeight = splitPointV * h
bottomHeight = h - topHeight
art(x, y, leftWidth, topHeight, tur)
art(x + leftWidth, y, rightWidth, topHeight, tur)
art(x, y + topHeight, leftWidth, bottomHeight, tur)
art(x + leftWidth, y + topHeight, rightWidth, bottomHeight, tur)
# Decide at random whether or not to split
def doSplit(dim):
r = random.randint(120, max(round(1.5 * dim), 120))
if r < dim:
return True
else:
return False
# Use recursion to draw "art" in a Mondrian style
def art(x, y, w, h, tur):
# Conditions to split
if w > WIDTH / 32 and h > HEIGHT / 32:
splitBoth(x, y, w, h, tur)
elif w > WIDTH / 32:
splitHoriz(x, y, w, h, tur)
elif h > HEIGHT / 32:
splitVert(x, y, w, h, tur)
else:
# Random split
if doSplit(w) and doSplit(h):
splitBoth(x, y, w, h, tur)
elif doSplit(w):
splitHoriz(x, y, w, h, tur)
elif doSplit(h):
splitVert(x, y, w, h, tur)
# No split
else:
drawSquare(x, y, w, h, randomColor(y),randomPenSize(), tur)
# Set up turtle
def makeTurtle():
# Set up the turtle
turtle.setworldcoordinates(-10, -15, WIDTH+15, HEIGHT+10)
turtle.setup()
ourWindow = turtle.Screen()
ourWindow.title('Recursive Art')
tur = turtle.Turtle()
tur.hideturtle()
ourWindow.tracer(0)
tur.pencolor("black")
return tur
def main():
# Create the turtle
tur = makeTurtle()
# Draw the art
art(0, 0, WIDTH, HEIGHT, tur)
# Done
turtle.done()
main()