Pages: [1]
  Print  
Author Topic: The formula for finding point_distance.  (Read 5461 times)
Offline (Unknown gender) MrJackSparrow2
Posted on: May 29, 2008, 04:58:02 PM
Member
Joined: Apr 2008
Posts: 35

View Profile Email
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)) );
}
Logged
Offline (Male) Austin
Reply #1 Posted on: May 29, 2008, 07:55:02 PM

Member
Location: Florida
Joined: Apr 2008
Posts: 32
MSN Messenger - austin6641@hotmail.com
View Profile Email
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));
}
Logged
Offline (Male) Josh @ Dreamland
Reply #2 Posted on: September 04, 2008, 06:45:33 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2953

View Profile Email
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.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Post made October 22, 2008, 10:32:26 AM was deleted at the author's request.
Offline (Unknown gender) score_under
Reply #4 Posted on: October 22, 2008, 01:14:57 PM

Member
Joined: Aug 2008
Posts: 308

View Profile
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?
Logged
Offline (Male) sprintf()
Reply #5 Posted on: October 22, 2008, 02:56:03 PM

"Past Contributor"
Location: S. Wales
Joined: Apr 2008
Posts: 72
MSN Messenger - dmgoron@gmail.com
View Profile Email
All square numbers are positive.
Logged
Offline (Unknown gender) score_under
Reply #6 Posted on: October 22, 2008, 03:15:39 PM

Member
Joined: Aug 2008
Posts: 308

View Profile
All square numbers are positive.
Of course I was... testing.
Logged
Post made November 15, 2008, 02:38:24 PM was deleted at the author's request.
Offline (Male) RetroX
Reply #8 Posted on: November 15, 2008, 09:57:23 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
As far as I know, C++ doesn't have an i constant.  Would be cool, though.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) score_under
Reply #9 Posted on: November 16, 2008, 06:47:57 AM

Member
Joined: Aug 2008
Posts: 308

View Profile
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++)
Logged
Offline (Male) Josh @ Dreamland
Reply #10 Posted on: November 16, 2008, 08:59:04 AM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2953

View Profile Email
XD @ i constant

Nah. C++ would just kill your game for you. ^_^
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #11 Posted on: November 17, 2008, 07:16:57 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Not actually an "i" variable, but support for imaginary numbers.  You could always just do sqrt(-1). =P
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) ludamad
Reply #12 Posted on: November 22, 2008, 03:53:22 PM
Developer
Joined: May 2008
Posts: 1256

View Profile Email
RetroX: #include <Complex>
Logged
Offline (Male) RetroX
Reply #13 Posted on: November 23, 2008, 10:40:34 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Wait, C++ has a library for that? O_O

EDIT: Holy shit.  That is awesome. :D
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Pages: [1]
  Print