Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- import random
- pressed = False
- rect_coord = [[0, 0], [0, 0]]
- red_points_ids = set()
- red_points = set()
- blue_points_ids = dict()
- rect_id = None
- SIZE = 30
- pt_lists = [[list() for j in xrange((1000 + SIZE - 1) / SIZE)] for i in xrange((1500 + SIZE - 1) / SIZE)]
- def circle(canvas, xy, r, color):
- x, y = xy
- id = canvas.create_oval(x-r,y-r,x+r,y+r, fill=color)
- return id
- def add_point(canvas, x, y):
- pt_lists[x / SIZE][y / SIZE].append((x, y, circle(canvas, (x,y), 2, 'blue')))
- def read_points(canvas):
- for i in xrange(100000):
- add_point(canvas, random.randint(10, 1490), random.randint(10, 990))
- def main():
- root = Tk()
- drawing_area = Canvas(root, width=1500, height=1000)
- drawing_area.config(bg='white')
- drawing_area.pack()
- drawing_area.bind("<Motion>", motion)
- drawing_area.bind("<ButtonPress-1>", b1down)
- #drawing_area.bind("<ButtonRelease-1>", b1up)
- read_points(drawing_area)
- root.mainloop()
- def init_points(canvas):
- for row in pt_lists:
- for l in row:
- for xy in l:
- circle(canvas, xy, 2, 'blue')
- def redraw(widget):
- global rect_id
- if rect_id is not None:
- widget.delete(rect_id)
- rect_id = widget.create_rectangle(rect_coord[0][0], rect_coord[0][1], rect_coord[1][0], rect_coord[1][1])
- global red_points, red_points_ids
- for red_id in red_points_ids:
- widget.itemconfig(red_id, fill='blue')
- red_points_ids = set()
- global pt_lists
- 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):
- 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):
- for point in pt_lists[u][v]:
- red = True
- for i in xrange(2):
- 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:
- red = False
- break
- if red:
- red_points_ids.add(point[2])
- widget.itemconfig(point[2], fill='red')
- def b1down(event):
- global pressed
- global rect_coord
- rect_coord[int(pressed)] = [event.x, event.y]
- pressed = not pressed
- if not pressed:
- redraw(event.widget)
- def motion(event):
- global pressed
- global rect_coord
- rect_coord[int(pressed)] = [event.x, event.y]
- if not pressed:
- redraw(event.widget)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement