Pages: 1
Author Topic: The formula for finding point_distance.  (104,570 Views)
Offline (Unknown gender) MrJackSparrow2

Member
Joined: Apr 2008
Posts: 35
View profile
Posted on: May 29, 2008, 09:58:02 PM
I'm sure you already have this, but i'm trying to help. :)

#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)) );
}
Offline (Unknown gender) Austin

Member
Joined: Apr 2008
Posts: 32
View profile
Reply #1 Posted on: May 30, 2008, 12:55:02 AM
And for two 3D points (I didn't define the types because I am not sure what you use):
d3d_point_distance(x1,y1,z1,x2,y2,z2) {
    return sqrt(sqr(x1-x2)+sqr(y1-y2)+sqr(z1-z2));
}
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Reply #2 Posted on: September 04, 2008, 11: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.
Post made October 22, 2008, 03:32:26 PM was deleted at the author's request.
Offline (Unknown gender) score_under

Member
Joined: Aug 2008
Posts: 308
View profile
Reply #4 Posted on: October 22, 2008, 06: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?
Offline (Unknown gender) sprintf()

Past Contributor
Joined: Apr 2008
Posts: 72
View profile
Reply #5 Posted on: October 22, 2008, 07:56:03 PM
All square numbers are positive.
Offline (Unknown gender) score_under

Member
Joined: Aug 2008
Posts: 308
View profile
Reply #6 Posted on: October 22, 2008, 08:15:39 PM
Quote from: bigmoron on October 22, 2008, 07:56:03 PM
All square numbers are positive.
Of course I was... testing.
Post made November 15, 2008, 07:38:24 PM was deleted at the author's request.
Offline (Unknown gender) RetroX

Master of all things Linux
Contributor
Joined: Apr 2008
Posts: 1,055
View profile
Reply #8 Posted on: November 16, 2008, 02:57:23 AM
As far as I know, C++ doesn't have an i constant.  Would be cool, though.
Offline (Unknown gender) score_under

Member
Joined: Aug 2008
Posts: 308
View profile
Reply #9 Posted on: November 16, 2008, 11: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:
for(i=0;i<size;i++)
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Reply #10 Posted on: November 16, 2008, 01:59:04 PM
XD @ i constant

Nah. C++ would just kill your game for you. ^_^
Offline (Unknown gender) RetroX

Master of all things Linux
Contributor
Joined: Apr 2008
Posts: 1,055
View profile
Reply #11 Posted on: November 18, 2008, 12:16:57 AM
Not actually an "i" variable, but support for imaginary numbers.  You could always just do sqrt(-1). =P
Offline (Unknown gender) ludamad

Developer
Joined: May 2008
Posts: 1,256
View profile
Reply #12 Posted on: November 22, 2008, 08:53:22 PM
RetroX: #include <Complex>
Offline (Unknown gender) RetroX

Master of all things Linux
Contributor
Joined: Apr 2008
Posts: 1,055
View profile
Reply #13 Posted on: November 23, 2008, 03:40:34 PM
Wait, C++ has a library for that? O_O

EDIT: Holy shit.  That is awesome. :D
Pages: 1