# Remove an axis from coordinate distances

This topic is 4257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I have the length between two coordinates, is there a quick method to remove an axis from that distance, if given the direction vector? For example, say I have position pa and pb. dir_3d is the normalized direction vector from pa to pb. And distance_3d is the distance between them. Can I flatten pa and pb to exist on the same Y space (pa.y = pb.y), and then perform a calculation between dir_3d and distance_3d to obtain a new distance_2d between them? To put it simply, I want this result: distance_2d = distance_3d * sqrtf( (dir_3d.x * dir_3d.x) + (dir_3d.z * dir_3d.z) ); But I don't want to have to calculate with sqrtf again. I want to use dir_3d to change distance_3d into distance_2d. Is this possible? Thanks for any help. [Edited by - Kest on June 26, 2006 8:38:13 PM]

##### Share on other sites
You could simply take the sqrt of everything at once. If you simplify everything your doing you get:

distance_2d = distance_3d/sqrt(distance_3d.x^2+distance_3d.y^2+distance_3d.z^2) * sqrt(distance_3d.x^2+distance_3d.z^2);

that is normalizing distance_3d and then multiplying it by the length of distance_2d. You could combine the two sqrt functions by doing:

distance_2d = distance_3d*sqrt((distance_3d.x^2+distance_3d.z^2)/(distance_3d.x^2+distance_3d.y^2+distance_3d.z^2));

##### Share on other sites
I'm sending a 3D ray to an intersect routine. The intersect routine uses a grid system to optimize the number of objects checked with the ray. The optimizing grid is 2D. So I need to find the distance that ray covers on the flattened xz spaces. I'm not computing the 3D length and 2D length in the same place.

It's not a big deal. I'm not that worried about extreme optmization here. I just thought that there may be a quick way to accomplish this.

One way I was considering.. If x=.7,y=.7,z=0. Y would be 50% of the vector, right? So 50% of the 3D distance needs removed? So if the length was 300 in 3D, the ray now runs only 150 units straight down the x axis. Is that correct? Is there a fast way to find the percentage of Y in the vector? Or would calculating the percentage be just as time consuming as calculating the distance again?

##### Share on other sites
idk I think sqrt is a rly slow function. You might be able to write some speed optimized version with lower accuaracy. I did that for all the trig functions. I dont think that percentage would work. if sqrt(.7^2+.7^2+0^2) == 300 then sqrt(.7^2 + 0^2) == 300/sqrt(2) which isnt 150. unless you can find a way to combine them I think you need 2 sqrt function calls.

##### Share on other sites
Quote:
 I'm not sure about the percentage way, I'd have to think about that some more. There are other ways to compute it though.Yours:distance_2d = distance_3d * sqrtf( (dir_3d.x * dir_3d.x) + (dir_3d.z * dir_3d.z) );A little less math:distance_2d = sqrtf(distance_3d * distance_3d - dir_3d.y * dir_3d.y);Trig:distance_2d = distance_3d * cos(asin(dir_3d.y / distance_3d));Not sure which is more optimal though.

EDIT: Oops! Sorry, not sure what I was thinking. I forgot dir_3d was a unit vector I guess. So ignore all that.

Here's the corrected "less math":
distance_2d = distance_3d * sqrtf(1 - dir_3d.y * dir_3d.y);

Here's the corrected Trig:
distance_2d = distance_3d * cos(asin(dir_3d.y));

I don't think the percentage will work. BTW, what you are looking for is called the projection of the 3D vector into a plane.

[Edited by - reana1 on June 27, 2006 4:52:33 PM]