Line Functions (Slope & Direction)

Hello! -

My task is to:

Write a program with modular structure that asks the user for two points on a line, calculates the slope of the line, and determines whether the line is horizontal, vertical, falling from left to right, or rising left to right. My program should consist of two functions: one for calculating slope (in which I already have created) and another that determines the line direction.

I have created the function for calculating the slope of the line, however, I am not sure as to how I should go about creating a function that will determine the direction of the line itself. Also, I must check if the slope is undefined. (Knowing that if the denominator = 0, the slope will be undefined)

Any help is appreciated,

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream.h>
#include <math.h>

   int lineDirection (int);
           
   void main()
           
   {
      float slope;
      float x1, y1, x2, y2;
      float dx, dy;
      char ans;
      ans='y';
   
      while (ans=='y' || ans=='Y')
      {
         cout << "\nEnter X1: ";
         cin >> x1;
      
         cout << "\nEnter Y1: ";
         cin >> y1;
      
         cout << "\nEnter X2: ";
         cin >> x2;
      
         cout << "\nEnter Y2: ";
         cin >> y2;
      
         dy = y2 - y1;
         dx = x2 - x1;
         slope = dy / dx;
      
         cout<<"\nThe slope is: "<< slope << "\n";
      
         cout<<"\nDo you wish to continue? (y/n): ";
         cin>>ans;
      
      }
   }
	
	int lineDirection (int slope);  //Point of confusion 
Last edited on
Make slope double value. That way vertical lines (when slope denominator is 0) will be represented by infinity special value you can check with isinf() function: http://en.cppreference.com/w/cpp/numeric/math/isinf

example:
1
2
3
double s = 1/0.0;
if(std::isinf(s))
    std::cout << "vertical";
vertical
To detemine direction you should check slope: if it is 0, line is horisontal, if it is positive, line is rising, negative — falling.
Last edited on
That way vertical lines (when slope denominator is 0) will be represented by infinity special value you can check with isinf() function


Is that the only way to correctly determine if the line is vertical? I just haven't learned that method as of yet.

To detemine direction you should check slope: if it is 0, line is horisontal, if it is positive, line is rising, negative — falling.


How would I go about checking slope within a function? Tad bit confused.
Last edited on
How would I go about checking slope within a function? Tad bit confused.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
enum line_direction {HORISONTAL, VERTICAL, ASCENDING, DESCENDING};

//...

line_direction direction(double slope)
{
    if(std::isinf(slope))
        return VERTICAL;
    if(slope < 0)
        return DESCENDING;
    if(slope > 0)
        return ASCENDING;
    return HORISONTAL;
} 

Is that the only way to correctly determine if the line is vertical?
You can pass nominator and denominator instead of slope into function and check if denominator is 0 here. Like with atan2() function.
Topic archived. No new replies allowed.