C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-18-2004, 07:34 PM   #1
Registered User
 
Join Date: Aug 2003
Posts: 42
Distance Formula Implecations: Urgent!

Hello. As we all know, the distance formula is sqrt(sq(x2-x1) + sq(y2 - y1)). sq = square. Now, for some reason, this isn't at all working. Here is my code:

Code:
float distance(float x2, float y2)
{
  float distance = 0;
  static float x1 = 7, y1 = 2;
  distance = sq(x2 - x1) + sq(y2 - y1);
  distance = 4 * sq(distance) + 0.5;
  x1 = x2;
  y1 = y2;
  return distance;
}
Now I don't have the math.h libraries available (working with a special embedded system, using a special compiler) therefore I have no sqrt() availible, so i just squared it a second time. Assume all numbers are correct, because they are. But when I do something like: sqrt(sq(6-7) + sq(1-2)) which in my calculator comes out to 1.41421... but in my program comes out to -46.500000. Also I'm using printf("%f\n", distance) to print. I just don't understance what's wrong, any help would be much appreciated.
__________________
Sigh, nothing ever works the first try.

Register Linux User #314127
KneeLess is offline   Reply With Quote
Old 03-18-2004, 07:40 PM   #2
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
Posts: 2,718
...and what's your sq( ) function?
__________________
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
XSquared is offline   Reply With Quote
Old 03-18-2004, 07:42 PM   #3
Registered User
 
Join Date: Aug 2003
Posts: 42
Nevermind, fixed. Don't you hate that? Worked on it for 6 hours only to fix it in three minutes.
__________________
Sigh, nothing ever works the first try.

Register Linux User #314127
KneeLess is offline   Reply With Quote
Old 03-18-2004, 07:49 PM   #4
Registered User
 
Join Date: Feb 2004
Posts: 72
How did you fix it?
major_blagger is offline   Reply With Quote
Old 03-19-2004, 03:25 AM   #5
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,817
You can use a power series to estimate distance or if you know the angle you are looking you can find the distance by

Code:
if (distx>disty)
{
   totaldist=distx/cos(angle);
} else totaldist=disty/sin(angle);
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 03-20-2004, 08:00 PM   #6
Registered User
 
joed's Avatar
 
Join Date: Mar 2004
Posts: 59
distance

A little off the subject, but here's a neat way to approximate integer distance, if you don't care about precision. Saw this somewhere and simplified the shifting for my needs:

Code:
int fastdist(int x1, int y1, int x2, int y2)
{
  int dx = abs(x2 - x1);
  int dy = abs(y2 - y1);

  if(dy > dx) {        // XOR swap function
    dx ^= dy;
    dy ^= dx;
    dx ^= dy;
  } 

  return ((dx << 4) + (dx << 2) +
          (dy << 4) - (dy << 3)) >> 4;
}
Making the shifting more complex adds precision, without costing too much, but this was precise enough for the short pixel distances I was measuring. Haven't tried converting to fixed-point.

-Joe
joed is offline   Reply With Quote
Old 03-20-2004, 10:52 PM   #7
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,817
Yes you can approximate distance via a power series, but it has an error of like 3 or 4 percent.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segfaulting Distance Program radiohead C Programming 2 01-09-2006 08:48 PM
Distance Formula in my program..... I need help fast!!! Mackology101 C Programming 3 09-23-2004 10:10 PM
shortest path problems talz13 C++ Programming 7 05-08-2004 06:13 AM
Fuzzy Logic ygfperson A Brief History of Cprogramming.com 10 10-13-2002 04:58 PM
Formula string conversion Gr3g C++ Programming 2 04-12-2002 08:28 PM


All times are GMT -6. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22