Advertisement
Guest User

lalalab1

a guest
May 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. from Tkinter import *
  2. import random
  3.  
  4. pressed = False
  5. rect_coord = [[0, 0], [0, 0]]
  6. red_points_ids = set()
  7. red_points = set()
  8. blue_points_ids = dict()
  9. rect_id = None
  10.  
  11. SIZE = 30
  12.  
  13. pt_lists = [[list() for j in xrange((1000 + SIZE - 1) / SIZE)] for i in xrange((1500 + SIZE - 1) / SIZE)]
  14.  
  15. def circle(canvas, xy, r, color):
  16.     x, y = xy
  17.     id = canvas.create_oval(x-r,y-r,x+r,y+r, fill=color)
  18.     return id
  19.  
  20. def add_point(canvas, x, y):
  21.     pt_lists[x / SIZE][y / SIZE].append((x, y, circle(canvas, (x,y), 2, 'blue')))
  22.  
  23. def read_points(canvas):
  24.     for i in xrange(100000):
  25.         add_point(canvas, random.randint(10, 1490), random.randint(10, 990))
  26.  
  27. def main():
  28.  
  29.     root = Tk()
  30.     drawing_area = Canvas(root, width=1500, height=1000)
  31.     drawing_area.config(bg='white')
  32.     drawing_area.pack()
  33.     drawing_area.bind("<Motion>", motion)
  34.     drawing_area.bind("<ButtonPress-1>", b1down)
  35.     #drawing_area.bind("<ButtonRelease-1>", b1up)
  36.     read_points(drawing_area)
  37.     root.mainloop()
  38.  
  39.  
  40. def init_points(canvas):
  41.     for row in pt_lists:
  42.         for l in row:
  43.             for xy in l:
  44.                 circle(canvas, xy, 2, 'blue')
  45.  
  46.  
  47. def redraw(widget):
  48.     global rect_id
  49.     if rect_id is not None:
  50.         widget.delete(rect_id)
  51.     rect_id = widget.create_rectangle(rect_coord[0][0], rect_coord[0][1], rect_coord[1][0], rect_coord[1][1])
  52.     global red_points, red_points_ids
  53.     for red_id in red_points_ids:
  54.         widget.itemconfig(red_id, fill='blue')
  55.  
  56.     red_points_ids = set()
  57.  
  58.  
  59.     global pt_lists
  60.     for u in xrange(min(rect_coord[0][0], rect_coord[1][0]) / SIZE, max(rect_coord[0][0], rect_coord[1][0]) / SIZE + 1):
  61.         for v in xrange(min(rect_coord[0][1], rect_coord[1][1]) / SIZE, max(rect_coord[0][1], rect_coord[1][1]) / SIZE + 1):
  62.             for point in pt_lists[u][v]:
  63.                 red = True
  64.                 for i in xrange(2):
  65.                     if not abs(abs(point[i] - rect_coord[0][i]) + abs(point[i] - rect_coord[1][i]) - abs(rect_coord[0][i] - rect_coord[1][i])) < 0.001:
  66.                         red = False
  67.                         break
  68.                 if red:
  69.                     red_points_ids.add(point[2])
  70.                     widget.itemconfig(point[2], fill='red')
  71.  
  72. def b1down(event):
  73.     global pressed
  74.     global rect_coord
  75.     rect_coord[int(pressed)] = [event.x, event.y]
  76.     pressed = not pressed
  77.     if not pressed:
  78.         redraw(event.widget)
  79.  
  80. def motion(event):
  81.     global pressed
  82.     global rect_coord
  83.     rect_coord[int(pressed)] = [event.x, event.y]
  84.     if not pressed:
  85.         redraw(event.widget)
  86.  
  87.  
  88.  
  89. if __name__ == "__main__":
  90.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement