Advertisement
here2share

# Tk_rotate_oval.py

Dec 5th, 2019 (edited)
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. # Tk_rotate_oval.py
  2.  
  3. from Tkinter import *
  4.  
  5. root = Tk()
  6. root.title("Tk Rotate Oval")
  7. ### root.withdraw() # vs root.deiconify()')
  8. xm,ym=600,600
  9.  
  10. canvas = Canvas(root, width=xm, height=ym)
  11. canvas.grid()
  12.  
  13. from PIL import ImageDraw, ImageTk, Image, ImageGrab
  14. from random import randrange
  15. import time
  16.  
  17. class Cv(): pass
  18. cv=Cv()
  19.  
  20. 0
  21. RGB_BLACK=(0,0,0)
  22. RGB_WHITE=(255,255,255)
  23. RGB_GRAY=(180,180,180)
  24.  
  25. RGB_GRAYLIGHT=(236,236,236)
  26. RGB_GRAYDARK=(169,169,169)
  27.  
  28. RGB_RED=(255,0,0)
  29. RGB_ORANGE=(255,165,0)
  30. RGB_YELLOW=(255,255,0)
  31. RGB_GREEN=(0,128,0)
  32. RGB_BLUE=(0,0,255)
  33. RGB_CYAN=(0,255,255)
  34. RGB_PURPLE=(128,0,128)
  35.  
  36. RGB_DARKBLUE=(0,0,139)
  37. RGB_DARKGREEN=(0,100,0)
  38. RGB_DEEPPINK=(255,20,147)
  39. RGB_INDIGO=(75,0,130)
  40. RGB_LIGHTPURPLE=(204,153,255)
  41. RGB_LIGHTBLUE=(173,216,230)
  42. RGB_LIGHTGREEN=(178,255,102)
  43. RGB_LIGHTYELLOW=(255,255,102)
  44. RGB_LIME=(0,255,0)
  45. RGB_OLIVE=(107,142,35)
  46.  
  47. RGB_BROWN=(139,69,19)
  48. RGB_GOLD=(255,215,0)
  49. RGB_SILVER=(192,192,192)
  50.  
  51. RGB_ROYALBLUE=(65,105,225)
  52. 0
  53. img = Image.new('RGB', (xm,ym))
  54. # random.seed(1)
  55.  
  56.  
  57. from math import sin, cos, pi
  58.  
  59. def rotate_oval( x1, y1, x2, y2, rotate=0, vertex_count=-1):
  60.     rotate=rotate%180
  61.     if vertex_count == -1:
  62.         vertex_count = int((abs(x1 - x2) + abs(y1 - y2)) * 0.07)
  63.         vertex_count = max(18,vertex_count)
  64.     vertex_count = max(3,vertex_count)
  65.     (x1, x2) = (min(x1, x2), max(x1, x2))
  66.     (y1, y2) = (min(y1, y2), max(y1, y2))
  67.     a = (x2 - x1) / 2
  68.     b = (y2 - y1) / 2
  69.     VERTEX = [ (a * cos(i * 2 * pi / vertex_count), \
  70.                 b * sin(i * 2 * pi / vertex_count)) \
  71.                     for i in range(vertex_count) ]
  72.     ## rotation
  73.     VERTEX = [( x*cos(pi/180*rotate) + y*sin(pi/180*rotate), y*cos(pi/180*rotate) - x*sin(pi/180*rotate) )
  74.               for (x,y) in VERTEX  ]
  75.  
  76.     ## move center
  77.     VERTEX = [( x + ( x1 + x2 ) / 2, y + ( y1 + y2 ) / 2 )
  78.               for (x,y) in VERTEX  ]
  79.     return VERTEX
  80.  
  81.  
  82. if __name__ == '__main__':
  83.     min_x = 20
  84.     min_y = 20
  85.     max_x = 580
  86.     max_y = 580
  87.     for i in range(1000):
  88.         canvas.delete('all')
  89.         '''
  90.         (x1, y1) = (randrange(start=min_x, stop=max_x),
  91.                     randrange(start=min_y, stop=max_y))
  92.         (x2, y2) = (randrange(start=min_x, stop=max_x),
  93.                     randrange(start=min_y, stop=max_y))
  94.         '''
  95.         x1, y1, x2, y2 = 200, 0, 400, 600
  96.         roto = rotate_oval(x1, y1, x2, y2, i)
  97.         canvas.create_polygon(roto)
  98.         root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement