-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlottingTriangle.py
279 lines (237 loc) · 10.7 KB
/
PlottingTriangle.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
276
277
278
279
from asyncio import events
from tkinter import *
import math
import sys
import os
myTk = Tk()
myTk.title("Triangle Testing")
myCanvas = Canvas(myTk, bd=4, bg="skyblue", cursor="circle", height=600, width=600)
pointList = []
hull = []
destroyList = []
finishedHull = False
def pythagMyBro(point1, point2):
return math.sqrt(pow(point2[1] - point1[1], 2) + pow(point2[0] - point1[0]))
def insideCircle(circle, point):
center = circle[0]
distance = circle[1]
if pythagMyBro(center, point) < distance:
return True
else:
return False
def findIntersection(point1, point2, point3, point4):
try:
firstLineSlope = (point2[1] - point1[1])/(point2[0]-point1[0])*-1
except ZeroDivisionError:
firstLineSlope = float('inf')
firstLinePoint = point1
try:
secondLineSlope = ((point4[1] - point3[1])/(point4[0] - point3[0]))*-1
except ZeroDivisionError:
secondLineSlope = float('inf')
secondLinePoint = point3
a1 = firstLineSlope
b1 = 1
c1 = firstLineSlope*firstLinePoint[0]+firstLinePoint[1]
#print("First Slope is: " + str(a1))
a2 = secondLineSlope
b2 = 1
c2 = secondLineSlope*secondLinePoint[0]+secondLinePoint[1]
#print("Second Slope is: " + str(a2))
#print(str(a1*b2-a2*b1))
if a1 == a2:
return [float('inf'), float('inf')]
'''if firstLineSlope == float('inf'):
print("firstlineSlope")
print(point1)
print(point2)
print(point3)
print(point4)
if secondLineSlope == float('inf'):
print("secondLineSlope")
print(point1)
print(point2)
print(point3)
print(point4)'''
if firstLineSlope == float('inf'):
intersection = [
firstLinePoint[0],
-1*secondLineSlope*firstLinePoint[0]+c2]
#x_coord = intersection[0]
#y_coord = intersection[1]
#dot_circle = myCanvas.create_oval(x_coord-5,y_coord-5,x_coord+5,y_coord+5,outline="black",fill="black",width=0)
#print("First Inf")
#print(intersection)
elif secondLineSlope == float('inf'):
intersection = [
secondLinePoint[0],
-1*firstLineSlope*secondLinePoint[0]+c1]
#x_coord = intersection[0]
#y_coord = intersection[1]
#dot_circle = myCanvas.create_oval(x_coord-5,y_coord-5,x_coord+5,y_coord+5,outline="black",fill="black",width=0)
#print("Second Inf")
#print(intersection)
elif firstLineSlope != float('inf') and secondLineSlope != float('inf'):
intersection = [
-1*(b1*c2-b2*c1)/(a1*b2-a2*b1),
-1*(a2*c1-a1*c2)/(a1*b2-a2*b1),
]
else:
print("Failure :(")
#print(intersection)
'''if (intersection[0] + 0.000005 > round(intersection[0])):
intersection[0] = round(intersection[0])
if (intersection[0] - 0.000005 < round(intersection[0])):
intersection[0] = round(intersection[0])
if (intersection[1] + 0.000005 > round(intersection[1])):
intersection[1] = round(intersection[1])
if (intersection[1] - 0.000005 < round(intersection[1])):
intersection[1] = round(intersection[1])'''
return intersection
def findCircle(point1, point2, point3):
circleCenter = findIntersection(point1, point2, point2, point3)
radius = abs(pythagMyBro(circleCenter, point1))
return [circleCenter, radius]
def leftOf(a, b, c):
area = (b[0]-a[0])*(c[1]-a[1])-(b[1]-a[1])*(c[0]-a[0])
if area > 0:
return False
else:
return True
def checkForCross():
counter = len(hull)-1
print("counter is " + str(counter))
print("hull is ")
print(hull)
print("\n")
newPoint = counter
counter -= 4
while counter > 2:
point4 = [hull[counter-3], hull[counter-2]]
point3 = [hull[counter-1], hull[counter]]
point2 = [hull[newPoint-3], hull[newPoint-2]]
point1 = [hull[newPoint-1], hull[newPoint]]
'''print("\n")
print(point1)
print(point2)
print(point3)
print(point4)
print("\n")'''
#print("firstLineSLope: " + str(firstLineSlope))
try:
secondLineSlope = (point4[1] - point3[1])/(point4[0]-point3[0])
except ZeroDivisionError:
secondLineSlope = float('inf')
#print("secondLineSlope: " + str(secondLineSlope))
intersection = findIntersection(point1, point2, point3, point4)
#print("\nIntersection Testing! Points: " + str((counter/2)+0.5) + " " + str((counter/2)-0.5))
#print(str(point1[0]) + " " + str(point2[0]) + " " + str(intersection[0]) + " " + str(min([point1[0], point2[0], intersection[0]])))
#print(str(point1[1]) + " " + str(point2[1]) + " " + str(intersection[1]) + " " + str(min([point1[1], point2[1], intersection[1]])))
#print(str(point3[0]) + " " + str(point4[0]) + " " + str(intersection[0]) + " " + str(min([point3[0], point4[0], intersection[0]])))
#print(str(point3[1]) + " " + str(point4[1]) + " " + str(intersection[1]) + " " + str(min([point3[1], point4[1], intersection[1]])))
#print("\n")
if (intersection != [float('inf'), float('inf')]):
print("First Passed")
if (min([point1[0], point2[0], intersection[0]]) != intersection[0] and max([point1[0], point2[0], intersection[0]]) != intersection[0]) or (intersection[0] == point1[0] and intersection[0] == point2[0]):# and (intersection[1] != point1[1] and intersection[1] != point2[1])):
print("Second Passed")
if (min([point1[1], point2[1], intersection[1]]) != intersection[1] and max([point1[1], point2[1], intersection[1]]) != intersection[1]) or (intersection[1] == point1[1] and intersection[1] == point2[1]):# and (intersection[0] != point1[0] and intersection[0] != point2[0])):
print("Third Passed")
if (min([point3[0], point4[0], intersection[0]]) != intersection[0] and max([point3[0], point4[0], intersection[0]]) != intersection[0]) or (intersection[0] == point3[0] and intersection[0] == point4[0]):# and (intersection[1] != point3[1] and intersection[1] != point4[1])):
print("Fourth Passed")
if (min([point3[1], point4[1], intersection[1]]) != intersection[1] and max([point3[1], point4[1], intersection[1]]) != intersection[1]) or (intersection[1] == point3[1] and intersection[1] == point4[1]):# and (intersection[0] != point3[0] and intersection[0] != point4[0])):
print("\nIntersection Testing! Points: " + str((counter/2)+0.5) + " " + str((counter/2)-0.5))
print(point1)
print(point2)
print(point3)
print(point4)
print(intersection)
return True
'''if (intersection != [float('inf'), float('inf')] and
((intersection[1] == point1[1] or intersection[1] == point2[1]) and
(min([point1[0], point2[0], intersection[0]]) != intersection[0] and max([point1[0], point2[0], intersection[0]]) != intersection[0])) or
((intersection[0] == point1[0] or intersection[0] == point2[0]))
)'''
counter -= 2
return False
def colorInPolygon():
i = 0
while i > len(hull):
i = i + 2
pass
def findEdgeDistance(point1, point2):
return math.sqrt((point2[0] - point1[0])**2 + (point2[1] - point1[0])**2)
def findMidPoint(point1, point2):
list = []
list.append((point1[0] + point2[0]) / 2)
list.append((point1[1] + point2[1]) / 2)
return list
def findSlope(point1, point2):
return (point2[1] - point1[1]) / (point2[0] - point1[0])
"""def createCircle(point1, point2, point3):
e1 = findEdgeDistance(point1, point2)
e2 = findEdgeDistance(point2, point3)
e3 = findEdgeDistance(point1, point3)
intersecting_point = findIntersection(point1, point2, point1, point2)
mid_pt = findMidPoint(point1, point2)
radius = findEdgeDistance(mid_pt, intersecting_point)"""
def draw_dots(event):
global finishedHull
if not finishedHull:
x_coord = event.x
y_coord = event.y
pointList.append(x_coord)
pointList.append(y_coord)
hull.append(x_coord)
hull.append(y_coord)
#print(len(pointList))
if len(destroyList) > 0:
counter = len(destroyList)-1
while counter >= 0:
myCanvas.delete(destroyList[counter])
counter -= 1
destroyList.clear()
if len(hull) > 4 and (math.sqrt(pow((y_coord-hull[1]), 2) + pow((x_coord-hull[0]), 2))) < 5:
#print(len(pointList))
anything = myCanvas.create_line(pointList[0], pointList[1], hull[0], hull[1], fill="blue")
anything2 = myCanvas.create_polygon
finishedHull = True
else:
if len(pointList) == 2:
dot_circle = myCanvas.create_oval(x_coord-5,y_coord-5,x_coord+5,y_coord+5,outline="black",fill="green",width=0)
elif len(pointList) == 4 and not checkForCross():
counter = len(hull)-1
#if counter > 5:
#print("CheckForCross: " + str(checkForCross()))
dot_circle = myCanvas.create_oval(x_coord-5,y_coord-5,x_coord+5,y_coord+5,outline="black",fill="green",width=0)
#print("We Did IT!")
anything = myCanvas.create_line(pointList[0], pointList[1], pointList[2], pointList[3], fill="blue")
pointList.pop(0)
pointList.pop(0)
else:
circleBreak = myCanvas.create_oval(x_coord-5,y_coord-5,x_coord+5,y_coord+5,outline="black",fill="red",width=0, tags="DestroyCircle")
lineBreak = myCanvas.create_line(pointList[0], pointList[1], pointList[2], pointList[3], fill="black", tags="DestroyLine")
destroyList.append(circleBreak)
destroyList.append(lineBreak)
hull.pop()
hull.pop()
pointList.pop()
pointList.pop()
def restart_program():
python = sys.executable
os.execl(python, python, * sys.argv)
def FindTriangulation():
pass
myTk.bind("<Button-1>", draw_dots)
quit_button = Button(myTk, text="Exit")
quit_button.pack(side="bottom")
quit_button["command"] = myTk.destroy
restart_button = Button(myTk, text="Restart")
restart_button.pack(side="bottom")
restart_button["command"] = restart_program
myCanvas.pack()
myTk.mainloop()
'''point1 = [300, 300]
point2 = [310, 400]
point3 = [200, 350]
point4 = [400, 350]
print(findIntersection(point1, point2, point3, point4))'''