# ENIGMA Forums

## Contributing to ENIGMA => Function Peer Review => Topic started by: MrJackSparrow2 on May 29, 2008, 04:58:02 PM

Title: The formula for finding point_distance.
Post by: MrJackSparrow2 on May 29, 2008, 04:58:02 PM
I'm sure you already have this, but i'm trying to help. :)

Code: [Select]
`#include <math.h>double point_distance(double x1,double y1,double x2,double y2){       return (double) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) );}`
Title: Re: The formula for finding point_distance.
Post by: Austin on May 29, 2008, 07:55:02 PM
And for two 3D points (I didn't define the types because I am not sure what you use):
Code: [Select]
`d3d_point_distance(x1,y1,z1,x2,y2,z2) {    return sqrt(sqr(x1-x2)+sqr(y1-y2)+sqr(z1-z2));}`
Title: Re: The formula for finding point_distance.
Post by: Josh @ Dreamland on September 04, 2008, 06:45:33 PM
The former has existed since R1, but the latter doesn't exist in 'other programs,' and I prolly won't add it until I actually do some 3D.
Title: Re: The formula for finding point_distance.
Post by: Fede-lasse on October 22, 2008, 10:32:26 AM
I'm sure you already have this, but i'm trying to help. :)

Code: [Select]
`#include <math.h>double point_distance(double x1,double y1,double x2,double y2){       return (double) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) );}`
That would be uneffecient. Why not:
Code: [Select]
`#include <math.h>double point_distance(double x1,double y1,double x2,double y2){  int x_diff,y_diff;  x_diff = x2-x1;  y_diff = y2-y1;  return (double)sqrt((x_diff*x_diff)+(y_diff*y_diff));}`Not sure how old this topic is, though xD

EDIT:
Sorry, didn't notice Josh's post.
Title: Re: The formula for finding point_distance.
Post by: score_under on October 22, 2008, 01:14:57 PM
Oh dear, imaginary numbers.

Quote
#include <math.h>

double point_distance(double x1,double y1,double x2,double y2)
{
int x_diff,y_diff;
x_diff = x2-x1;
y_diff = y2-y1;
return (double)sqrt((x_diff*x_diff)+(y_diff*y_diff));
}
If I were to measure the distance from point (4,4) to point (0,0) without any checking on my part, this would cause a huge error as the numbers would suddenly mysteriously appear in the complex plane  ;)

Off the top of my head,
Quote
#include <math.h>

double point_distance(double x1,double y1,double x2,double y2)
{
int x_diff,y_diff;
x_diff = abs(x2-x1);
y_diff = abs(y2-y1);
return (double)sqrt((x_diff*x_diff)+(y_diff*y_diff));
}
That should work better, right?
Title: Re: The formula for finding point_distance.
Post by: sprintf() on October 22, 2008, 02:56:03 PM
All square numbers are positive.
Title: Re: The formula for finding point_distance.
Post by: score_under on October 22, 2008, 03:15:39 PM
All square numbers are positive.
Of course I was... testing.
Title: Re: The formula for finding point_distance.
Post by: Fede-lasse on November 15, 2008, 02:38:24 PM
All square numbers are positive.
Of course I was... testing.

:D
Title: Re: The formula for finding point_distance.
Post by: RetroX on November 15, 2008, 09:57:23 PM
As far as I know, C++ doesn't have an i constant.  Would be cool, though.
Title: Re: The formula for finding point_distance.
Post by: score_under on November 16, 2008, 06:47:57 AM
If it had an "i" constant, then 99% of c++ programs would be broken.

It's hard to find a C++ prog that does NOT contain something similar to:
Code: [Select]
`for(i=0;i<size;i++)`
Title: Re: The formula for finding point_distance.
Post by: Josh @ Dreamland on November 16, 2008, 08:59:04 AM
XD @ i constant

Nah. C++ would just kill your game for you. ^_^
Title: Re: The formula for finding point_distance.
Post by: RetroX on November 17, 2008, 07:16:57 PM
Not actually an "i" variable, but support for imaginary numbers.  You could always just do sqrt(-1). =P
Title: Re: The formula for finding point_distance.
Post by: ludamad on November 22, 2008, 03:53:22 PM
RetroX: #include <Complex>
Title: Re: The formula for finding point_distance.
Post by: RetroX on November 23, 2008, 10:40:34 AM
Wait, C++ has a library for that? O_O

EDIT: Holy shit.  That is awesome. :D