boris-vlasenko

проверка попадания блока в блок

Feb 9th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange as rnd, choice
  3. from PIL import ImageTk, Image
  4. import os, sys
  5. import time
  6.  
  7. root = Tk()
  8. width = 900
  9. hight = 600
  10.  
  11. root.geometry(str(width)+'x'+str(hight)+'+100+100')
  12. canv = Canvas(bg='lightblue')
  13. canv.pack(fill=BOTH,expand=1)
  14.  
  15.  
  16.  
  17. def pointInSegment(x,x1,x2):
  18.     return x1 <= x <= x2
  19.  
  20. def segmentInSegment(x1,x2,xx1,xx2):
  21.     return pointInSegment(x1,xx1,xx2) or pointInSegment(x2,xx1,xx2) or \
  22.     pointInSegment(xx1,x1,x2) or pointInSegment(xx2,x1,x2)
  23.  
  24. def rectInRect(x1,y1,x2,y2,xx1,yy1,xx2,yy2):
  25.     if segmentInSegment(x1,x2,xx1,xx2) and segmentInSegment(y1,y2,yy1,yy2):
  26.         return True
  27.     if segmentInSegment(xx1,xx2,x1,x2) and segmentInSegment(yy1,yy2,y1,y2):
  28.         return True
  29.     return False
  30.  
  31. xx1,yy1,xx2,yy2 = 250,200,350,300
  32.  
  33. canv.create_rectangle(xx1,yy1,xx2,yy2)
  34.  
  35.  
  36. x1,y1,x2,y2 = 0,0,0,0
  37. px1,py1,px2,py2 = 0,0,0,0
  38.  
  39. sq = canv.create_rectangle(x1,y1,x2,y2)
  40.  
  41. def m(event):
  42.     global x1,x2,y1,y2
  43.     px1,py1,px2,py2 = x1,y1,x2,y2
  44.     x1 = event.x
  45.     x2 = event.x + 150
  46.     y1 = event.y
  47.     y2 = event.y + 150
  48.     color = 'black'
  49.     if rectInRect(x1,y1,x2,y2,xx1,yy1,xx2,yy2):
  50.         color = 'yellow'
  51.         if segmentInSegment(px1,px2,xx1,xx2):
  52.             # ~ print('up-down')
  53.             if py2 < yy1:
  54.                 color = 'blue'
  55.                 print('up')
  56.             else:
  57.                 color = 'green'
  58.                 print('down')
  59.         else:
  60.             # ~ print('left-right')
  61.             if px2 < xx1:
  62.                 color = 'orange'
  63.                 print('left')
  64.             else:
  65.                 color ='red'
  66.                 print('right')
  67.         x1,y1,x2,y2 = px1,py1,px2,py2
  68.     canv.coords(sq,x1,y1,x2,y2)
  69.        
  70.     canv.itemconfig(sq,fill=color)
  71.  
  72. root.bind('<Motion>',m)
  73.  
  74. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment