Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-03-2008, 04:08 PM
yellowhamster yellowhamster is offline
Newbie
 
Join Date: May 2008
Posts: 1
Rep Power: 0
yellowhamster is on a distinguished road
Default Calculating Line Length

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-03-2008, 05:13 PM
soku11 soku11 is offline
Newbie
 
Join Date: May 2008
Location: Warsaw, Poland
Posts: 12
Rep Power: 0
soku11 is on a distinguished road
Default Re: Calculating Line Length

The formula is something like this:
Code:
d=Sqrt[(x_a-x_b)^2+(y_a-y_b)^2]
Its coming from the pythagoras theory Is it what you wanted??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-04-2008, 01:43 AM
mholt mholt is offline
Newbie
 
Join Date: Jan 2008
Posts: 27
Rep Power: 4
mholt is on a distinguished road
Default Re: Calculating Line Length

Programmatically:


Code:
#include <math.h>
// point A(x1, y1)
// point B(x2, y2)

double distance = sqrt((x2-x1)^2 + (y2-y1)^2);
This works for any point anywhere on the X-Y coordinate plane.

Last edited by mholt; 05-04-2008 at 01:48 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-04-2008, 10:57 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,897
Last Blog:
Web slideshow in JavaS...
Rep Power: 78
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Calculating Line Length

This brings us back to the discussion of how maths is vital to programming. Yes, if you work out the triangle using Pythagoras, you can calculate the length. Just make sure the program runs the calculations in the order you want it to, though - remember, the exponentiation will be performed before the addition.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-05-2008, 11:07 AM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: Calculating Line Length

^ 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
*/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-05-2008, 04:36 PM
mholt mholt is offline
Newbie
 
Join Date: Jan 2008
Posts: 27
Rep Power: 4
mholt is on a distinguished road
Default Re: Calculating Line Length

Oops. That you are right. Eh, I just got out of my computer science class (Java) - wrong mindset.

Thanks for the correction.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-06-2008, 07:52 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,897
Last Blog:
Web slideshow in JavaS...
Rep Power: 78
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Calculating Line Length

The math.h file ensures you can use the pow() function. It might be worth looking around the DOM in case there are any preset functions in cases like these.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 11:32 AM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads