![]() |
| | #1 |
| Registered User Join Date: Aug 2003
Posts: 42
| Distance Formula Implecations: Urgent! 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;
}
|
| KneeLess is offline | |
| | #2 |
| C++ Developer 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 | |
| | #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. |
| KneeLess is offline | |
| | #4 |
| Registered User Join Date: Feb 2004
Posts: 72
| How did you fix it? |
| major_blagger is offline | |
| | #5 |
| Super Moderator 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 | |
| | #6 |
| Registered User 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;
}
-Joe |
| joed is offline | |
| | #7 |
| Super Moderator 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |