Thread: Distance Formula Implecations: Urgent!

  1. #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

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  3. #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

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    How did you fix it?

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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);

  6. #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

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yes you can approximate distance via a power series, but it has an error of like 3 or 4 percent.

Popular pages Recent additions subscribe to a feed

Similar Threads

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