I need a very rough pythag alteranitve
I need a very rough pythag alteranitve to test the distance between 2 points.
I know
dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
But it's required for handling some particles so I am hoping for something with less operations and preferably without the sqrt.
It doesn't need to be very accurate it's just so I know when I can recycle the particles.
Thanks.
You can avoid the sqrt by storing the square of the distance you are checking against. Not sure how you'd cut it down past [(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)] though.
--my site
Ah that helps.
dist*dist = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
It's always obvious when you know how isn't it.
dist*dist = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
It's always obvious when you know how isn't it.
What the fuck? :P
Btw an alternative to using pythag/square of distance is to use the "Manhattan" distance, which is just the "block" distance between two points. E.g. left 5, up 4 = 9 blocks travelled.
Obviously it's less accurate than pythag, but for something simple it'll be ok. (It's commonly used in pathfinding etc. where heuristics/"best guesses" are what you're after, rather than accurate measurements).
Btw an alternative to using pythag/square of distance is to use the "Manhattan" distance, which is just the "block" distance between two points. E.g. left 5, up 4 = 9 blocks travelled.
Obviously it's less accurate than pythag, but for something simple it'll be ok. (It's commonly used in pathfinding etc. where heuristics/"best guesses" are what you're after, rather than accurate measurements).
---PS3dev
April 18, 2005 07:41 AM
So something like dist = fabs(x1-x2)+fabs(y1+y2); ?
Does anyone know of any speed issues with fabs I'm guessing it just does this
return (parameter<0.0f)?parameter*-1.0f:parameter;
You guys agree?
freegdev have too many blue smarties or something?
Does anyone know of any speed issues with fabs I'm guessing it just does this
return (parameter<0.0f)?parameter*-1.0f:parameter;
You guys agree?
freegdev have too many blue smarties or something?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement