Find an object that other canvas object is overlapping with – Python tkinter

Question:

I want to detect with what canvas object block. We have block, circle and triangle canvas objects.

I know there is if block in canvas.find_overlapping(x1,y1,x2,y2): method but doesn’t shows with what object is block overlapping. It just shows if block is touching with any other canvas object.

overlapping_object=canvas.find_overlapping(block), overlapping_object could be a list that shows tags of objects that is block touching with.

How to make overlapping_object=canvas.find_overlapping(block) but it’s correct. This one I typed here is just how could it look.

Thanks for any help!

I’m making 2D minecraft in tkinter and this is the thing that can really speed up my process.

Asked By: GamerTronky

||

Answers:

If you’re not willing to use other libraries and only tkinter, I don’t think there’s a built in function in tkinter that will allow it. I am also not sure why everything must be done in tkinter as it’s not at all unusual for programs to use multiple libraries. Personally, given tikinter’s limitations, I would use pygame to track polygons and their intersection but would never draw them. Short of using a third library (tikinter, python default, and other), there is one other approach. I mean it’s really the only appraoch.

good ole fashioned math.
https://algs4.cs.princeton.edu/93intersection/
Here’s some documentation on how to go about doing that. I wish you luck.

EDIT:
I think I accidentally found the answer to your question using math while studying for some other stuff. Still no way IN tkinter but here’s a math explanation.

circle with center (x,y) and radius r
Polygon with z number of sides with x*2-1 number of points(x,y)

If you take iterate the lines of the polygon and put them through the following maths

Line J (x1,y1)(x2,y2)
m of Line J = (y1-y2)/(x1-x2)

create line P from circle center to P1 of LineJ
create line O from circle center to P2 of LineJ

Now we have a triangle

take the inverse cosine and length of P and O to get angle of the triangle you just made.

Make a right triangle by bisecting the triangle with line K starting at circle center and going out at the angle you just calculated.

Now you have line P and 1/2 angle of line P to line K

Now to find the intercept of that mid angle line

Tan(1/2 angle) = slope or m of the new line
using the x,y of the circle center calculate the slope intercept formula y=mx+b and get b

Now take the slope intercept formula for line J and set it equal to the slope intercept of the new line

line J (mx + b) = y = line P (mx + b)
Solve for y

Then plug y in the slope intercept for either and solve for x.

Once you’ve done this you have 4 points. The three points of the triangle, the point that makes a perpendicular line to center of circle from line J.

If any 3 of those points to the center of the circle in distance is smaller than r, they overlap. If they are are all > r then they don’t overlap.

Answered By: Ash
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.