【发布时间】:2017-04-09 07:22:04
【问题描述】:
class Point:
def __init__(self, xcoord=0, ycoord=0):
self.x = xcoord
self.y = ycoord
class Rectangle:
def __init__(self, bottom_left, top_right, colour):
self.bottom_left = bottom_left
self.top_right = top_right
self.colour = colour
def intersects(self, other):
我正在尝试根据右上角和左下角查看两个矩形是否相交但是当我创建函数时:
def intersects(self, other):
return self.top_right.x>=other.top_right.x>=self.bottom_left.x and self.top_right.x>=other.bottom_left.x>=self.bottom_left.x and self.top_right.y>=other.top_right.y>=self.bottom_left.y and self.top_right.x>=other.bottom_left.x>=self.bottom_left.x
输入时函数会返回false:
r1=Rectangle(Point(1,1), Point(2,2), 'blue')
r3=Rectangle(Point(1.5,0), Point(1.7,3), 'red')
r1.intersects(r3)
进入外壳。
【问题讨论】:
-
“我想看看两个三角形是否相交”——你的意思是“矩形”吗?
-
如果您对一个矩形使用 4 个点,或者为每个矩形计算剩余的 2 个点,这将非常容易。然后,每当矩形 a 的一个点包含在矩形 b 中时,它们就会重叠。 ;)
标签: python python-3.x