我想检测用什么 Canvas 对象 block
。我们有 block、circle 和 triangle
Canvas 对象。
我知道 canvas.find_overlapping(x1,y1,x2,y2): 方法中有 if block ,但没有显示
block
与什么对象重叠。它只是显示 block
是否与任何其他 Canvas 对象接触。
overlapping_object=canvas.find_overlapping(block)
,overlapping_object
可以是一个列表,显示与 block
接触的对象的标签。
如何制作 overlapping_object=canvas.find_overlapping(block)
但它是正确的。我在这里输入的这个就是它的样子。
感谢您的帮助!
我正在使用 tkinter 制作 2D Minecraft,这是真正可以加快我的进程的东西。
最佳答案
如果您不愿意使用其他库而只使用 tkinter,我认为 tkinter 中没有允许这样做的内置函数。我也不确定为什么一切都必须在 tkinter 中完成,因为程序使用多个库并不罕见。就个人而言,考虑到 tikinter 的局限性,我会使用 pygame 来跟踪多边形及其交点,但永远不会绘制它们。除了使用第三个库(tikinter、python default 等)之外,还有另一种方法。我的意思是这确实是唯一的方法。
很好的老式数学。 https://algs4.cs.princeton.edu/93intersection/ 这里有一些关于如何去做的文档。祝你好运。
编辑: 我想我在学习其他一些东西的时候不小心用数学找到了你问题的答案。在 tkinter 中仍然没有办法,但这里有一个数学解释。
circle with center (x,y) and radius r
Polygon with z number of sides with x*2-1 number of points(x,y)
如果您迭代多边形的线并通过以下数学计算
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
现在我们有一个三角形
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.
现在你有直线 P 和直线 P 与直线 K 的 1/2 角
现在求那条中角线的截距
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
现在采用线 J 的斜率截距公式并将其设置为等于新线的斜率截距
line J (mx + b) = y = line P (mx + b)
Solve for y
然后将 y 代入斜率截距并求解 x。
完成此操作后,您将获得 4 分。三角形的三点,由J线与圆心成垂线的点。
如果这些点中的任何 3 个点到圆心的距离小于 r,则它们重叠。如果它们都是 > r 那么它们就不会重叠。
关于python - 查找与其他 Canvas 对象重叠的对象 - Python tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73184836/