Advertisement
Guest User

Untitled

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