Advertisement
Void-voiD

Untitled

May 7th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. from pyglet.gl import *
  2. from pyglet.window import key, mouse
  3. from shapely.geometry import LineString
  4.  
  5.  
  6. Width = 1200
  7. Height = 1200
  8. window = pyglet.window.Window(Width, Height, resizable=True)
  9. window.set_minimum_size(100, 100)
  10. glClearColor(1, 1, 1, 1)
  11. state = -1
  12. victim = []
  13. cutter = []
  14. current_circuit = []
  15. result = []
  16.  
  17.  
  18. class Point:
  19. x = 0
  20. y = 0
  21. type = -1
  22. visited = False
  23. linker = []
  24. index = 0
  25.  
  26. def __init__(self, xx, yy):
  27. self.x = xx
  28. self.y = yy
  29.  
  30. def set_index(self, ind):
  31. self.index = ind
  32.  
  33. def set_visited(self):
  34. self.visited = True
  35. if len(self.linker) == 2:
  36. self.linker[0].visited = True
  37. self.linker[1].visited = True
  38.  
  39.  
  40. class Segment:
  41. start = Point(0, 0)
  42. stop = Point(0, 0)
  43.  
  44. def __init__(self, one, two):
  45. self.start = one
  46. self.stop = two
  47.  
  48.  
  49. def draw_circuit(circuit, color):
  50. glBegin(GL_LINE_LOOP)
  51. glColor3f(*color)
  52. for point in circuit:
  53. glVertex2i(point.x, point.y)
  54. glEnd()
  55.  
  56.  
  57. def fill_vertices_list(lists, segments, source):
  58. for counter in source:
  59. lists.append(counter)
  60. help = len(lists) - 1
  61. reference_list = lists[help]
  62. for point in reference_list:
  63. point.type = -1
  64. point.index = help
  65. i = 0
  66. j = 1
  67. length = len(reference_list)
  68. while i != length:
  69. segments.append(Segment(reference_list[i], reference_list[j]))
  70. i += 1
  71. j += 1
  72. if j == length:
  73. j = 0
  74. return lists, segments
  75.  
  76.  
  77. def insert_point(lis, start, stop, pointer):
  78. # print(start.x, start.y, "---------", stop.x, stop.y, "----pointer----", pointer.x, pointer.y)
  79. # for l in lis:
  80. # print(l.x, l.y)
  81. dx = abs(pointer.x - start.x)
  82. dy = abs(pointer.y - start.y)
  83. is_delta_x = dx >= dy
  84. delta_origin = 0
  85. if is_delta_x:
  86. delta_origin = dx
  87. else:
  88. delta_origin = dy
  89.  
  90. start_pos = stop_pos = 0
  91. length = len(lis)
  92. for i in range(length):
  93. cur = lis[i]
  94. if start.x == cur.x and start.y == cur.y:
  95. start_pos = i
  96. if stop.x == cur.x and stop.y == cur.y:
  97. stop_pos = i
  98. # print(start_pos, stop_pos, length)
  99. it = start_pos
  100. it += 1
  101. delta = 0
  102. if stop_pos < start_pos:
  103. stop_pos += length
  104. if is_delta_x:
  105. if it < length:
  106. delta = abs(lis[it].x - start.x)
  107. else:
  108. delta = abs(lis[it - length].x - start.x)
  109. else:
  110. if it < length:
  111. delta = abs(lis[it].y - start.y)
  112. else:
  113. delta = abs(lis[it - length].y - start.y)
  114. while delta < delta_origin:
  115. if it >= stop_pos:
  116. break
  117. it += 1
  118. if is_delta_x:
  119. if it < length:
  120. delta = abs(lis[it].x - start.x)
  121. else:
  122. delta = abs(lis[it - length].x - start.x)
  123. else:
  124. if it < length:
  125. delta = abs(lis[it].y - start.y)
  126. else:
  127. delta = abs(lis[it - length].y - start.y)
  128. lis.insert(it, pointer)
  129. return lis, pointer
  130.  
  131.  
  132. def check_inside(pointer, segments):
  133. counter = 0
  134. for segment in segments:
  135. pointer1 = segment.start
  136. pointer2 = segment.stop
  137. if pointer1.y == pointer2.y or (pointer1.x < pointer.x and pointer2.x < pointer.x) or ((pointer1.y > pointer.y) == (pointer2.y > pointer.y)):
  138. continue
  139. elif pointer1.x >= pointer.x >= pointer2.x:
  140. t = float(pointer.y - pointer1.y) / float(pointer2.y - pointer1.y)
  141. x = pointer1.x + t * (pointer2.x - pointer1.x)
  142. if x <= pointer.x + 0.001:
  143. continue
  144. counter += 1
  145. return counter % 2 != 0
  146.  
  147.  
  148. def show_bounds():
  149. global victim, cutter, state, current_circuit
  150. for i in victim:
  151. draw_circuit(i, (0, 0, 0))
  152. for j in cutter:
  153. draw_circuit(j, (1, 0, 0))
  154. if state != 1:
  155. draw_circuit(current_circuit, (0, 0, 1))
  156.  
  157.  
  158. def add_circuit(destination):
  159. global current_circuit
  160. if len(current_circuit) > 2:
  161. destination.append(current_circuit)
  162. current_circuit = []
  163. return destination
  164.  
  165.  
  166. def draw():
  167. global victim, cutter, result
  168.  
  169. exits = []
  170. d_segments = []
  171. r_segments = []
  172. d_list = []
  173. r_list = []
  174. #free_list = []
  175. d_alone_counter = [False] * len(victim)
  176. r_alone_counter = [False] * len(cutter)
  177. d_list, d_segments = fill_vertices_list(d_list, d_segments, victim)
  178. r_list, r_segments = fill_vertices_list(r_list, r_segments, cutter)
  179.  
  180. for d_seg in d_segments:
  181. for r_seg in r_segments:
  182. d = LineString(((d_seg.start.x, d_seg.start.y), (d_seg.stop.x, d_seg.stop.y)))
  183. r = LineString(((r_seg.start.x, r_seg.start.y), (r_seg.stop.x, r_seg.stop.y)))
  184. res = r.intersection(d)
  185. if not res.is_empty:
  186. cross = Point(int(res.x), int(res.y))
  187. d_alone_counter[d_seg.start.index] = True
  188. r_alone_counter[r_seg.start.index] = True
  189.  
  190. point1 = r_seg.start
  191. point2 = r_seg.stop
  192. point3 = d_seg.start
  193. if (point3.x - point1.x) * (point2.y - point1.y) - (point3.y - point1.y) * (point2.x - point1.x) < 0:
  194. cross.type = 0
  195. else:
  196. cross.type = 1
  197.  
  198. d_list[d_seg.start.index], lis1 = insert_point(d_list[d_seg.start.index], d_seg.start, d_seg.stop, cross)
  199. r_list[r_seg.start.index], lis2 = insert_point(r_list[r_seg.start.index], r_seg.start, r_seg.stop, cross)
  200. # lis1.index = d_seg.start.index
  201. # lis2.index = r_seg.start.index
  202. link = [lis1, lis2]
  203. # free_list.append(link)
  204. lis1.linker = link
  205. lis2.linker = link
  206. if cross.type == 1:
  207. exits.append(lis1)
  208.  
  209. for i in range(len(d_list)):
  210. for p in d_list[i]:
  211. p.set_index(i)
  212.  
  213. for i in range(len(r_list)):
  214. for p in r_list[i]:
  215. p.set_index(i)
  216.  
  217. print("\n")
  218. for d in d_list:
  219. for point in d:
  220. print(point.index, point.x, point.y, point.type, len(point.linker))
  221. print("-------------")
  222. print("\n\n")
  223. for d in r_list:
  224. for point in d:
  225. print(point.index, point.x, point.y, point.type, len(point.linker))
  226. print("-------------")
  227. print("\n\n")
  228.  
  229. while len(exits) > 0:
  230. j = 0
  231. if exits[-1].visited:
  232. exits.pop()
  233. continue
  234. current_result = []
  235. current_point = exits[-1]
  236.  
  237. j += 1
  238. if j >= 30:
  239. return
  240.  
  241. start_point = current_point
  242. print(current_point.x, current_point.y, "victim-start", current_point.index)
  243.  
  244. while True:
  245. i = 0
  246. index = current_point.index
  247. while i < len(d_list[index]):
  248. if d_list[index][i].x == current_point.x and d_list[index][i].y == current_point.y:
  249. break
  250. i += 1
  251.  
  252. while current_point.type != 0:
  253. current_point.set_visited()
  254. current_result.append(current_point)
  255. i += 1
  256. if i >= len(d_list[index]):
  257. i = 0
  258. current_point = d_list[index][i]
  259.  
  260. j += 1
  261. if j >= 30:
  262. return
  263.  
  264. print(i, current_point.x, current_point.y, "victim", current_point.index)
  265.  
  266. current_point = current_point.linker[1]
  267.  
  268. j += 1
  269. if j >= 30:
  270. return
  271.  
  272. index = current_point.index
  273. print(i, current_point.x, current_point.y, "victim->cutter", current_point.index)
  274. i = 0
  275. while True:
  276. if r_list[index][i].x == current_point.x and r_list[index][i].y == current_point.y:
  277. break
  278. i += 1
  279.  
  280. while current_point.type != 1:
  281. current_point.set_visited()
  282. current_result.append(current_point)
  283. i -= 1
  284. if i <= 0:
  285. i = len(r_list[index]) - 1
  286. current_point = r_list[index][i]
  287.  
  288. j += 1
  289. if j >= 30:
  290. return
  291.  
  292. print(i, current_point.x, current_point.y, "cutter", current_point.index)
  293.  
  294. current_point = current_point.linker[0]
  295.  
  296. j += 1
  297. if j >= 30:
  298. return
  299.  
  300. print(i, current_point.x, current_point.y, "cutter->victim", current_point.index)
  301. if current_point.x == start_point.x and current_point.y == start_point.y:
  302. break
  303.  
  304. result.append(current_result)
  305.  
  306. for i in range(len(d_alone_counter)):
  307. if not d_alone_counter[i]:
  308. if check_inside(d_list[i][0], r_segments):
  309. result.append(d_list[i])
  310.  
  311. for i in range(len(r_alone_counter)):
  312. if not r_alone_counter[i]:
  313. if check_inside(r_list[i][0], d_segments):
  314. result.append(r_list[i])
  315.  
  316.  
  317. @window.event
  318. def on_draw():
  319. global result
  320. window.clear()
  321.  
  322. show_bounds()
  323.  
  324. for res in result:
  325. draw_circuit(res, (0, 1, 0))
  326.  
  327.  
  328. @window.event
  329. def on_mouse_press(x, y, button, modifiers):
  330. global current_circuit
  331. if button == mouse.LEFT:
  332. if state != 1:
  333. current_circuit.append(Point(int(x), int(y)))
  334.  
  335.  
  336. @window.event
  337. def on_key_press(symbol, modifiers):
  338. global state, current_circuit, victim, cutter
  339. if symbol == key.ENTER:
  340. if state == -1:
  341. victim = add_circuit(victim)
  342. elif state == 0:
  343. cutter = add_circuit(cutter)
  344. elif symbol == key.SPACE:
  345. if state == -1:
  346. state = 0
  347. victim = add_circuit(victim)
  348. elif state == 0:
  349. state = 1
  350. cutter = add_circuit(cutter)
  351. draw()
  352.  
  353.  
  354. @window.event
  355. def on_resize(width, height):
  356. global Width, Height
  357. Width = width
  358. Height = height
  359.  
  360.  
  361. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement