我正在制作一个一维间隔,我必须检查自身是否与另一个对象相交。
这是我到目前为止所拥有的:
def __init__(self, lbound, rbound):
"""
Constructs a new interval given its lower and upper bounds.
"""
self._lbound = lbound
self._rbound = rbound
这是我的职责:
def intersects(self, other):
"""
Returns True if self intersects other and False othewise.
"""
s1 = set([self._lbound, self._rbound])
s2 = set([other._lbound, other._rbound])
if s1.intersection(s2) and s2.intersection(s1):
return True
问题是这个函数只给出了我想要的一半答案,什么是 检查自身是否与其他相交的更好方法?
最佳答案
您可能想要这样的东西:
def intersects(self, other):
return (other._lbound < self._lbound < other._rbound or
other._lbound < self._rbound < other._rbound or
self._lbound < other._lbound < self._rbound or
self._lbound < other._rbound < self._rbound)
关于python - 如何创建交集函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43713781/