In mathematics, the slope of a line is a number that describes both the direction and the steepness of the line. Slope is calculated by finding the ratio of the vertical change to the horizontal change between any two distinct points on a line. The steepness of a line is measured by the absolute value of the slope. A slope with a greater absolute value indicates a steeper line. The direction of a line is either increasing, decreasing, horizontal, or vertical:
Slope is calculated by finding the ratio of the vertical change to the horizontal change between any two distinct points on a line. The steepness of a line is measured by the absolute value of the slope. A slope with a greater absolute value indicates a steeper line. The direction of a line is either increasing, decreasing, horizontal, or vertical:
In mathematical language, the slope of the line connecting points (X1,Y1 ) and (X2,Y2) is:
m = delta y / delta x = vertical change / horizontal change = rise / run = Y2 - Y1 / X2 - X1
The formula fails for a vertical line, i.e., one that parallels the y axis. The slope is infinite, so the slope of a vertical line is considered undefined.
If y is a linear function of x, then the coefficient of x is the slope of the line created by plotting the function. Therefore, if the equation of the line is given in the form
y = mx + b
then m is the slope. This form of a lines equation is called the slope-intercept form, because b can be interpreted as the y-intercept of the line, that is, the y-coordinate where the line intersects the y-axis.
If the slope m of a line and a point (x1,y1) on the line are both known, then the equation of the line can be found using the point-slope formula:
y - y1 = m(x - x1)
The slope of the line defined by the linear equation ax + by + c = 0 is:
- a/b
The line segment connecting points (x1,y1) and (x2,y2) forms the hypotenuse of a right triangle and represents the distance between two points. The Pythagorean Theorem states that the square of the hypotenuse is equal to the sum of the squares of the other two sides, and it can be expressed as the Pythagorean equation:
a2 + b2 = c2
If the length of both a and b are known, then c can be calculated as:
c = square root of (a2 + b2)
If the length of the hypotenuse c and of one side (a or b) are known, then the length of the other side can be calculated as:
a = square root of (c2 - b2)
or
b = square root of (c2 - a2)
Write a C++ program that reads in two points (x1,y1) and (x2,y2) from standard input. The program writes to standard output the equation of the line, whether the line is vertical, horizontal, increasing, or decreasing, and the length of the line-segment connecting the two points. If l is a vertical line, its equation is x = a for some real number a. If l is a non-vertical line, output its equation in slope- intercept form. Your main function should return 1 if the line is vertical, or 0 if the line is not vertical.
Your program should demonstrate at least one if(..else) statement, one switch statement, and one conditional operator. This program demonstrates input and output redirection; there should not be any direct user interaction with this program.
Input is read from the stdin stream, using cin. The input consists of four real numbers separated by whitespace and represents the x-y coordinates of two points connected by a line. The numbers are in the order x1 y1 x2 y2.
Sample input
4 5 3 21
$ echo 4 5 3 21 | ./a.out
Line equation: y = -16x + 69
Slope direction: decreasing
Line segment length: 16.0312