-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
275 lines (223 loc) · 9.63 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import random
import sys
import pygame
import numpy as np
from dnq.delaunay import delaunay as dnqdel, step, genRandom
from Button import Button
from incremental.delaunay2D import Delaunay2D
SCREEN_HEIGHT = 768
SCREEN_WIDTH = 1280
# Displaying all points and edges
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
clock = pygame.time.Clock()
zoom = 2
def mesh_inc(surface, numSeeds):
radius = 730
seeds = radius * np.random.random((numSeeds, 2)) + 5
center = np.mean(seeds, axis=0)
dt = Delaunay2D(center, 50 * radius)
print("Adding points...")
print(seeds)
for s in seeds:
dt.addPoint(s)
pygame.draw.circle(surface, "#CC00CC", s, 5)
for t in dt.exportTriangles():
print(t)
pygame.draw.polygon(surface=surface, color="Green", points=[seeds[t[0]], seeds[t[1]], seeds[t[2]]], width=1)
def add_inc(surface):
surface.fill((160, 160, 160))
s = (random.randint(5, 735), random.randint(5, 735))
seeds.append(s)
center = np.mean(seeds, axis=0)
dt = Delaunay2D(center, 50 * radius)
print(seeds)
for s in seeds:
dt.addPoint(s)
pygame.draw.circle(surface, "#CC00CC", s, 5)
for t in dt.exportTriangles():
print(t)
pygame.draw.polygon(surface=surface, color="Green", points=[seeds[t[0]], seeds[t[1]], seeds[t[2]]], width=1)
def mesh_dq(surface, numSeeds):
seeds = genRandom(760, 760, numSeeds)
edges = dnqdel(seeds)
for s in seeds:
pygame.draw.circle(surface, "#CC00CC", s, 5)
for e in edges:
pygame.draw.line(surface=surface, color="Green", start_pos=e.org, end_pos=e.dest, width=1)
def add_dq(screen, surface, pic, numSeeds):
clear(pic)
seeds = genRandom(760, 760, numSeeds)
step(seeds, screen, surface, pic)
for s in seeds:
pygame.draw.circle(surface, "#CC00CC", s, 5)
def clear(surface):
surface.fill((160, 160, 160))
global seeds
seeds = []
def get_font(size): # Returns Press-Start-2P in the desired size
return pygame.font.Font("assets/font.ttf", size)
def blitRotateCenter(surf, image, topleft, angle):
rotated_image = pygame.transform.rotate(image, angle)
new_rect = rotated_image.get_rect(center=image.get_rect(topleft = topleft).center)
surf.blit(rotated_image, new_rect)
NOP_Info = get_font(16).render("No of Points:", True, "Black")
NOP_RECT = pygame.Rect(1150, 260, 140, 32)
radius = 730
center = (380, 380)
seeds = []
def inc_screen():
pygame.display.set_caption("Incremental Algorithm")
fake_screen = screen.copy()
pic = pygame.surface.Surface((760, 760))
pic.fill((160, 160, 160))
zoom_size = (round(760 / zoom), round(760 / zoom))
no_of_points = ""
active = False
zoomActive = False
while True:
mouse = pygame.mouse.get_pos()
fake_screen.fill((160, 160, 160))
fake_screen.blit(pic, (1, 1))
screen.blit(pygame.transform.scale(fake_screen, screen.get_rect().size), (0, 0))
if zoomActive:
zoom_area = pygame.Rect(0, 0, *zoom_size)
zoom_area.center = (mouse[0], mouse[1])
zoom_surf = pygame.Surface(zoom_area.size)
zoom_surf.blit(screen, (0, 0), zoom_area)
zoom_surf = pygame.transform.scale(zoom_surf, (760, 760))
screen.blit(zoom_surf, (0, 0))
NOP_TEXT = get_font(16).render(no_of_points, True, "Black")
pygame.draw.rect(screen, "Black", NOP_RECT, 2)
screen.blit(NOP_Info, (NOP_RECT.x - 225, NOP_RECT.y + 5))
screen.blit(NOP_TEXT, (NOP_RECT.x + 5, NOP_RECT.y + 5))
NOP_RECT.w = max(100, NOP_TEXT.get_width() + 10)
MESH = Button(image=None, pos=(1020, 340), text_input="MESH",
font=get_font(30), base_color="White", hovering_color="Green")
STEP = Button(image=None, pos=(1080, 420), text_input="STEP-BY-STEP",
font=get_font(30), base_color="White", hovering_color="Green")
CLEAR = Button(image=None, pos=(1190, 340), text_input="CLEAR",
font=get_font(30), base_color="White", hovering_color="Green")
BACK = Button(image=None, pos=(1175, 700), text_input="BACK",
font=get_font(30), base_color="White", hovering_color="Green")
for button in [MESH, STEP, CLEAR, BACK]:
button.changeColor(mouse)
button.update(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if BACK.checkForInput(mouse):
main_menu()
elif MESH.checkForInput(mouse):
no = int(no_of_points)
mesh_inc(pic, no)
elif CLEAR.checkForInput(mouse):
clear(pic)
elif STEP.checkForInput(mouse):
add_inc(pic)
if NOP_RECT.collidepoint(event.pos):
active = True
else:
active = False
if mouse <= (760, 760):
zoomActive = not zoomActive
elif event.type == pygame.KEYDOWN and active:
if event.key == pygame.K_BACKSPACE:
no_of_points = no_of_points[:-1]
else:
no_of_points += event.unicode
pygame.display.flip()
clock.tick(90)
def dq_screen():
pygame.display.set_caption("Divide & Conquer Algorithm")
fake_screen = screen.copy()
pic = pygame.surface.Surface((760, 760))
pic.fill((160, 160, 160))
zoom_size = (round(760 / zoom), round(760 / zoom))
no_of_points = ""
active = False
zoomActive = False
while True:
mouse = pygame.mouse.get_pos()
fake_screen.fill((160, 160, 160))
fake_screen.blit(pic, (1, 1))
screen.blit(pygame.transform.scale(fake_screen, screen.get_rect().size), (0, 0))
if zoomActive:
zoom_area = pygame.Rect(0, 0, *zoom_size)
zoom_area.center = (mouse[0], mouse[1])
zoom_surf = pygame.Surface(zoom_area.size)
zoom_surf.blit(screen, (0, 0), zoom_area)
zoom_surf = pygame.transform.scale(zoom_surf, (760, 760))
screen.blit(zoom_surf, (0, 0))
NOP_TEXT = get_font(16).render(no_of_points, True, "Black")
pygame.draw.rect(screen, "Black", NOP_RECT, 2)
screen.blit(NOP_Info, (NOP_RECT.x - 225, NOP_RECT.y + 5))
screen.blit(NOP_TEXT, (NOP_RECT.x + 5, NOP_RECT.y + 5))
NOP_RECT.w = max(100, NOP_TEXT.get_width() + 10)
MESH = Button(image=None, pos=(1020, 340), text_input="MESH",
font=get_font(30), base_color="White", hovering_color="Green")
STEP = Button(image=None, pos=(1080, 420), text_input="STEP-BY-STEP",
font=get_font(30), base_color="White", hovering_color="Green")
CLEAR = Button(image=None, pos=(1190, 340), text_input="CLEAR",
font=get_font(30), base_color="White", hovering_color="Green")
BACK = Button(image=None, pos=(1175, 700), text_input="BACK",
font=get_font(30), base_color="White", hovering_color="Green")
for button in [MESH, STEP, CLEAR, BACK]:
button.changeColor(mouse)
button.update(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if BACK.checkForInput(mouse):
main_menu()
elif MESH.checkForInput(mouse):
no = int(no_of_points)
mesh_dq(pic, no)
elif CLEAR.checkForInput(mouse):
clear(pic)
elif STEP.checkForInput(mouse):
no = int(no_of_points)
add_dq(screen, fake_screen, pic, no)
if NOP_RECT.collidepoint(event.pos):
active = True
else:
active = False
if mouse <= (760, 760):
zoomActive = not zoomActive
elif event.type == pygame.KEYDOWN and active:
if event.key == pygame.K_BACKSPACE:
no_of_points = no_of_points[:-1]
else:
no_of_points += event.unicode
pygame.display.flip()
clock.tick(90)
def main_menu():
pygame.display.set_caption("Delaunay Triangulation Main Menu")
screen.fill((160, 160, 160))
while True:
mouse = pygame.mouse.get_pos()
menu_text = get_font(100).render("Main Menu", True, "#CC00CC")
menu_rect = menu_text.get_rect(center=(640, 100))
screen.blit(menu_text, menu_rect)
INC_BUTTON = Button(image=None, pos=(640, 350), text_input="Incremental Algorithm",
font=get_font(40), base_color="White", hovering_color="Green")
DQ_BUTTON = Button(image=None, pos=(640, 500), text_input="Divide & Conquer Algorithm",
font=get_font(40), base_color="White", hovering_color="Green")
for button in [INC_BUTTON, DQ_BUTTON]:
button.changeColor(mouse)
button.update(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if INC_BUTTON.checkForInput(mouse):
inc_screen()
if DQ_BUTTON.checkForInput(mouse):
dq_screen()
pygame.display.update()
main_menu()