, , , .
Here is an example in python with some tests. Note that it is common to N dimensions, and this is the same algorithm for crossing boxes:
def are_intervals_intersecting(a0, a1, b0, b1):
'''
@param a0: float
@param a1: float
@param b0: float
@param b1: float
'''
if (a1 < a0):
a1, a0 = a0, a1
if (b1 < b0):
b1, b0 = b0, b1
if b0 < a0:
return a0 < b1
else:
return b0 < a1
def is_segment_intersecting_box(P0, P1, B0, B1):
'''
@param P0: tuple(float)
@param P1: tuple(float)
@param B0: tuple(float)
@param B1: tuple(float)
'''
for i in xrange(len(P0)):
if not are_intervals_intersecting(P0[i], P1[i], B0[i], B1[i]):
return False
return True
if __name__ == '__main__':
assert not is_segment_intersecting_box(
(0.0, 0.0, 0.0), (1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0))
assert not is_segment_intersecting_box(
(0.0, 0.0, 0.0), (4.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0))
assert not is_segment_intersecting_box(
(1.5, 1.5, 0.0), (4.0, 2.5, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0))
assert is_segment_intersecting_box(
(1.5, 1.5, 0.0), (4.0, 2.5, 2.5), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0))
assert is_segment_intersecting_box(
(1.5, 1.5, 1.5), (2.5, 2.5, 2.5), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0))
assert is_segment_intersecting_box(
(2.5, 2.5, 2.5), (2.6, 2.6, 2.6), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0))
assert is_segment_intersecting_box(
(2.5, 2.5), (2.5, 3.5), (2.0, 2.0), (3.0, 3.0))
print 'ok'