|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I'm writing a C program and I don't know how to measure the distance between two points. It's obviously pretty easy if they are along the x axis or y axis but what about at any arbitrary angle?
Thanks in advance for your help guys. |
| Sponsored Links |
|
|
|
|||
|
Programmatically:
Code:
#include <math.h> // point A(x1, y1) // point B(x2, y2) double distance = sqrt((x2-x1)^2 + (y2-y1)^2); Last edited by mholt; 05-04-2008 at 01:48 AM. |
|
|||
|
^ does not do exponentiation in C, it does an XOR.
Code:
#include <stdio.h>
#include <math.h>
double distance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2-x1,2) + pow(y2-y1,2));
}
int main(void)
{
double dist = distance(0,0,3,4);
printf("dist = %g\n", dist);
return 0;
}
/* my output
dist = 5
*/
|
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| VB6.0:Tutorial, Command Line | TcM | VB Tutorials | 9 | 08-25-2008 01:19 PM |
| Moving a line of cin.getline down to the next available line (C++ console)? | mholt | C and C++ | 5 | 04-20-2008 03:12 PM |
| Lot's of errors | Jaan | PHP Forum | 11 | 03-08-2008 01:46 PM |
| HTML Basic Formatting | clookid | Tutorials | 14 | 03-06-2007 04:10 PM |
| Next Line and line lengths | zee | Java Help | 3 | 02-25-2007 02:31 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |