Skip to main content
gamedev.net
🔒 Locked

math question (distance)

Started by Luke1 5 replies 450+ views
Original Post
Luke1
Luke1
I know the distance formula for 2d but im not too sure on the 3d part would some like this work:
    
int GetDist(int x1, int y1, int z1, int x2, int y2, int z2)
{
return sqr(((x1-x2)^2)+((y1-y2)^2)+((z1-z2)^2))
}
    
is that right or is there another way ? [edited by - Luke1 on March 11, 2003 4:18:26 PM]
Punty50
Punty50
Thats not right, because as AP said, ^ is the binary exclusive or operator. Use * because you know that you are squaring, and that way things will be nicely optimized and everything.

int GetDist(int x1, int y1, int z1, int x2, int y2, int z2){return sqr(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2))+((z1-z2)*(z1-z2)))}

Brendan
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
Jesper T
Jesper T
And if you use c++ its sqrt(..) or sqrtf(..), not sqr(..).


[edited by - grhodes_at_work on March 12, 2003 12:33:37 PM]
Thunder_Hawk
Thunder_Hawk
To figure out how to apply the distance formula to 3D (which you have correct BTW), imagine an arbitrary rectangular box and assign the letters x, y, and z to the axes of the box. Then use pythagoras to find the length of a diagonal through the middle of the box (hint: it is the hypotenuse of a triangle formed by the diagonal of one side of the box and a connected edge). This will give you the equivalent of the pythagorean theorem in 3D. From there it''s child''s play to derive the 3D distance formula.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.