Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3. import random
  4. import math
  5. import clr
  6. import numpy as np
  7. import os
  8.  
  9. root = Tk()
  10. root.geometry('800x600')
  11. c = Canvas(root, width=800, height=600, bg='white')
  12. c.pack()
  13.  
  14. x0 = int()
  15. y0 = int()
  16. x1 = int()
  17. y1 = int()
  18.  
  19. x0_label = Label(text="x0: ")
  20. y0_label = Label(text="y0: ")
  21. x1_label = Label(text="x1: ")
  22. y1_label = Label(text="y1: ")
  23. max_rand = 0
  24.  
  25. line_array = [[random.randint(200, 500), random.randint(200, 500)], [random.randint(400, 600), random.randint(200, 600)]]
  26.  
  27.  
  28. def draw_line(self, x1=0, y1=0, x2=0, y2=0):
  29. dx = x2 - x1
  30. dy = y2 - y1
  31.  
  32. sign_x = 1 if dx > 0 else -1 if dx < 0 else 0
  33. sign_y = 1 if dy > 0 else -1 if dy < 0 else 0
  34.  
  35. if dx < 0: dx = -dx
  36. if dy < 0: dy = -dy
  37.  
  38. if dx > dy:
  39. pdx, pdy = sign_x, 0
  40. es, el = dy, dx
  41. else:
  42. pdx, pdy = 0, sign_y
  43. es, el = dx, dy
  44.  
  45. x, y = x1, y1
  46.  
  47. error, t = el / 2, 0
  48.  
  49. c.create_line(x, y, x + 1, y)
  50. while t < el:
  51. error -= es
  52. if error < 0:
  53. error += el
  54. x += sign_x
  55. y += sign_y
  56. else:
  57. x += pdx
  58. y += pdy
  59. t += 1
  60. c.create_line(x, y, x + 1, y)
  61.  
  62.  
  63. def check_x():
  64. if line_array[0][0] < line_array[1][0]:
  65. global max_rand
  66. max_rand = line_array[0][0]
  67. else:
  68. max_rand = line_array[1][0]
  69. return
  70.  
  71.  
  72. def slope():
  73. dy = float(line_array[1][1] - line_array[0][1])
  74. dx = float(line_array[1][0] - line_array[0][0])
  75. k = float(math.atan(dy / dx))
  76. return k
  77.  
  78.  
  79. def change_line_coord():
  80. line_array[0][0] = input()
  81. line_array[0][1] = input()
  82. line_array[1][0] = input()
  83. line_array[1][1] = input()
  84.  
  85. def start():
  86. os.system('CLS')
  87. draw_line(c, line_array[0][0], line_array[0][1], line_array[1][0], line_array[1][1])
  88.  
  89. check_x()
  90.  
  91. triangle_array = [[random.randint(100, max_rand), random.randint(150, 500)],
  92. [random.randint(150, max_rand), random.randint(150, 500)],
  93. [random.randint(150, max_rand), random.randint(150, 500)]]
  94.  
  95. draw_line(c, triangle_array[0][0], triangle_array[0][1], triangle_array[1][0], triangle_array[1][1])
  96. draw_line(c, triangle_array[1][0], triangle_array[1][1], triangle_array[2][0], triangle_array[2][1])
  97. draw_line(c, triangle_array[2][0], triangle_array[2][1], triangle_array[0][0], triangle_array[0][1])
  98.  
  99. result1 = np.matmul([[triangle_array[0][0], triangle_array[0][1], 1],
  100. [triangle_array[1][0], triangle_array[1][1], 1],
  101. [triangle_array[2][0], triangle_array[2][1], 1]],
  102. [[1, 0, 0], [0, 1, 0], [-line_array[0][0], -line_array[0][1], 1]])
  103.  
  104. result2 = np.matmul(result1, [[math.cos(-slope()), (math.sin(-slope())), 0],
  105. [-math.sin(-slope()), (math.cos(-slope())), 0], [0, 0, 1]])
  106.  
  107. result3 = np.matmul(result2, [[1, 0, 0], [0, -1, 0], [0, 0, 1]])
  108.  
  109. result4 = np.matmul(result3, [[math.cos(slope()), (math.sin(slope())), 0],
  110. [-math.sin(slope()), (math.cos(slope())), 0], [0, 0, 1]])
  111.  
  112. result5 = np.matmul(result4, [[1, 0, 0], [0, 1, 0], [line_array[0][0], line_array[0][1], 1]])
  113.  
  114. draw_line(c, result5[0][0], result5[0][1], result5[1][0], result5[1][1])
  115. draw_line(c, result5[1][0], result5[1][1], result5[2][0], result5[2][1])
  116. draw_line(c, result5[2][0], result5[2][1], result5[0][0], result5[0][1])
  117. return
  118.  
  119. try:
  120. start()
  121. btn = Button(root,command = change_line_coord, text="Hello")
  122. btn.pack()
  123. except:
  124. pass
  125. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement