-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometry.py
62 lines (56 loc) · 2.05 KB
/
Geometry.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
from tkinter import *
from random import randint
from Logger import Logger
from time import strftime
from Colors import *
SHAPE_SCALE = 30
logName = "Logs\\" + strftime("%d%m%Y%H%M%S") + ".log"
log = Logger(logName)
def check_collision(canvas, shape1, shape2):
# Returns True if shape2 is touching shape1
# Returns False otherwise.
s1 = canvas.coords(shape1)
s2 = canvas.coords(shape2)
log.debug(s1, s2)
return (s1[0] <= s2[0] <= s1[2] or s1[0] <= s2[2] <= s1[2])\
and (s1[1] <= s2[1] <= s1[3] or s1[1] <= s2[3] <= s1[3])
def make_accepting(canvas, state, is_accepting, is_entry):
if is_accepting:
if not is_entry:
canvas.itemconfig(state.shape, fill=ACCEPTING)
else:
canvas.itemconfig(state.shape, fill=BOTH)
else:
if not is_entry:
canvas.itemconfig(state.shape, fill=DEFAULT)
else:
canvas.itemconfig(state.shape, fill=ENTRY)
def add_state(canvas, state, states):
log.debug(states)
width = canvas.width
height = canvas.height
# TODO: temporary Condition; find a better one.
tests_pass = False
new_shape = None
while not tests_pass:
chosen_width = randint(0, width-30)
chosen_height = randint(0, height-30)
new_shape = canvas.create_oval(chosen_width,
chosen_height,
chosen_width+30,
chosen_height+30,
fill=DEFAULT)
log.debug("Shape = ", canvas.coords(new_shape))
for s in states:
log.debug("s = ", s)
if s == state:
continue
collision = check_collision(canvas, s.shape, new_shape)
if collision:
log.debug("Collision")
canvas.delete(new_shape)
break
else:
tests_pass = True
state.shape = new_shape
canvas.create_text(chosen_width+SHAPE_SCALE/2, chosen_height+SHAPE_SCALE/2, text=state.name)