I'm trying to determine if a line given by two endpoints start = (x, y, z, ...), end = (x, y, z, ...)
intersects an n-dimensional cube or rectangle, which I am storing as opposite endpoints bottom_most = (x, y, z, ...), top_most= (x, y, z, ...)
.
I've found solutions in 3D, but not one that generalizes for n dimensions.
My current solution is:
dimensions = 3
def check_intersects(start, end, cube):
num_intersections = 0
for dim in range(dimensions):
if cube[dim] <= start[dim] <= cube[self.dimensions + dim] or start[dim] <= cube[dim] <= end[dim]:
num_intersections += 1
if num_intersections >= dimensions:
return True
return False
start, end = (0, 0, 0), (30, 30, 30)
cube = (10, 10, 10, 20, 20, 20)
print(check_intersects(start, end, cube))
start, end = (0, 0, 0), (0, 10, 20)
print(check_intersects(start, end, cube))
but this appears to break down in some cases where the lines come in from certain angles.
Are there any better solutions, or libraries that can do this for me?