8 Replies - 18630 Views - Last Post: 01 January 2011 - 01:03 PM Rate Topic: -----

#1 suad1111   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 10

the slope and the distance beween 2 points

Posted 01 January 2011 - 09:28 AM

how to right a simple program to calculate the slope and the distance between 2 points (x1,y1) and (x2,y2)
Is This A Good Question/Topic? 0
  • +

Replies To: the slope and the distance beween 2 points

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: the slope and the distance beween 2 points

Posted 01 January 2011 - 09:35 AM

View Postsuad1111, on 01 January 2011 - 03:28 PM, said:

how to right a simple program to calculate the slope and the distance between 2 points (x1,y1) and (x2,y2)

first determine the algorithm for the calculation?
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: the slope and the distance beween 2 points

Posted 01 January 2011 - 10:13 AM

Slope is the change in Y over the change in X. If the change in X is 0, the slope is undefined.

Distance is the square root of the sum of the squares of the change in X and Y.

Now show us your code if you still have problems.
Was This Post Helpful? 1
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: the slope and the distance beween 2 points

Posted 01 January 2011 - 10:36 AM

It is really simple. You can look up the formulas in just about any high school algebra text or google "slope and distance formulas":

delta_x = x2 - x1
delta_y = y2 - y1

slope = delta_y / delta_x

distance = sqrt(delta_x * delta_x + delta_y * delta_y)

writing these in C/C++ is pretty easy.
Was This Post Helpful? 1
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: the slope and the distance beween 2 points

Posted 01 January 2011 - 12:08 PM

Its just the Pythagorean theorem and its not too hard to prove/show (if you don't feel like memorizing an another mindless high school equation).

Posted Image

Posted Image

Posted Image

Posted Image

This post has been edited by ImaSexy: 01 January 2011 - 12:09 PM

Was This Post Helpful? 2
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: the slope and the distance beween 2 points

Posted 01 January 2011 - 12:29 PM

View PostImaSexy, on 01 January 2011 - 02:08 PM, said:

(if you don't feel like memorizing an another mindless high school equation).

Heh, I recently taught it to a 5th grader. I knew he got it when I could give him points from two lines and he could tell me if they intersected! Math isn't hard when you're too young to have been taught to be afraid of it. ;)
Was This Post Helpful? 0
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: the slope and the distance beween 2 points

Posted 01 January 2011 - 12:56 PM

Ive just experienced math teachers that just throw equations at you and they don't have the slightest bit of interest in whether you understand where they came from or not. I think being open minded and having interest in why you do the things you do in mathematics can help you go a long way.
Was This Post Helpful? 0
  • +
  • -

#8 suad1111   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 10

Re: the slope and the distance beween 2 points

Posted 01 January 2011 - 12:59 PM

#include <stdio.h>
#include <math.h>
int slope(float m);
int distance(float len);

int main( void)
{
float x1,x2 ,y1 , y2 ;
printf (“ Enter the two points scale on the two axis X and Y : “) ;
scanf (“%d%d%d%d, & x1, & x2 , & y1 , & y2 ) ;
printf (“The slope for the straight line is : “, slope(m));
printf (“The length between the two points is : “, distance(len));
}
{
int slope (float m)
return m = (x2 – x1 ) / (y2 – y1 ) ;
}
{
Int distance (float len)
Return len = sqrt ((x2 – x1 )* (x2 – x1 )/ (y2 – y1 )* (y2 – y1 ))
}
I do not know where is the problem in this first program
Its just the Pythagorean theorem and its not too hard to prove/show (if you don't feel like memorizing an another mindless high school equation).

Posted Image

Posted Image

Posted Image

Posted Image
[/quote]

This post has been edited by suad1111: 01 January 2011 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: the slope and the distance beween 2 points

Posted 01 January 2011 - 01:03 PM

this is not how you define a function
{
	int slope (float m)
		return m = (x2 – x1 ) / (y2 – y1 ) ;
}



look here
http://www.cplusplus...rial/functions/

This post has been edited by ImaSexy: 01 January 2011 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1