is there any easy way to find the objects ids that are overlapped? Here's an example of the code:
import tkinter as tk
import random as rand
class GUI:
def __init__(self, master, width, height):
self.master = master
self.w = width
self.h = height
self.canvas = tk.Canvas(master, width=width, height=height)
self.canvas.pack()
self.create_objects()
def create_objects(self):
r = 5
for i in range(100):
x = rand.uniform(0,1)*width
y = rand.uniform(0,1)*height
self.canvas.create_oval(x-r,y-r,x+r,y+r, fill="red")
def find_overlaps(self):
pass
width = 800
height = 600
root = tk.Tk()
app = GUI(root, width, height)
root.mainloop()
I would like for the find_overlaps function to give me the pairs of the object ids that are overlapped (or tripplets if such case happen). Is there any easy/efficient way to do it?
Here are steps that is possible for you to do here:
canvas
. You can do it using canvas.find_all()
method.canvas.coords(id)
.I have checked out standard find_overlapping
method of canvas
. It helps to determine which object are overlapped with specific rectangle only and I guess you need to solve problem you mentioned using some mathematics with a help of this method. Although, I have found a nice alternative, not based on find_overlapping
:
def find_overlaps(self):
r = 5
X = []
tags = self.canvas.find_all() #finds tags of all the object created
for tag in tags:
x0, y0, x1, y1 = self.canvas.coords(tag) # corresponding coordinates
center = [(x0+x1)/2, (y0+y1)/2] #centers of objects
X.append(center)
tree = cKDTree(X)
print(tree.query_pairs(2*r))
This is a set of couples of tags:
{(2, 63), (10, 93), (70, 82), (8, 45)}
from scipy.spatial import cKDTree
is required
Thanks a lot!, this is what i needed. Which of the two methods do you think is best?
find_overlapping
doesn't find result straightforwardly, you'll need to do the checkings for each rectangle on yourself. Another difference is that cKDTrees is precise only if objects are circles.Thank you very much, that was really helpfull, since my objects are circles i will use the cKDTrees approach then.