views:

47

answers:

1

I am having a total mind blank on this at the moment.

I have a cube (voxel) that is defined by a centre point and a width, height and depth of 1. If I have a vector with an origin from the centre of the box (E.g. (-0.7,-0.7, -0.7)) how do I calculate the point at which the vector intersects the outside of the box?

EDIT: The box is always aligned to the axis. The problem is in 3d.

Thanks

+2  A: 

Any point on the surface of your box will have at least one coordinate equal to 0.5 or -0.5, and all others will be -0.5 <= c <= 0.5.

So, find the coordinate with the largest absolute value, and then scale the vector to make that coordinate equal to +/- 0.5.

Something like this might work:

if (fabs(x) > fabs(y) && fabs(x) > fabs(z))
    y *= 0.5 / fabs(x)
    z *= 0.5 / fabs(x)
    x *= 0.5 / fabs(x)
else if (fabs(y) > fabs(z))
    x *= 0.5 / fabs(y)
    z *= 0.5 / fabs(y)
    y *= 0.5 / fabs(y)
else
    x *= 0.5 / fabs(z)
    y *= 0.5 / fabs(z)
    z *= 0.5 / fabs(z)
Saxon Druce
Does this assume that the vector is normalised?
paintstripper
No it doesn't need to be normalised
Saxon Druce
Ok, this seems to work. Nice and simple - Thank you, this is exactly what I needed.
paintstripper