The equation of the line through the points (X1, Y1)
and (X2, Y2)
is written in the form y = mx + b
.
What are the values of the slope m
and the y
-intercept b
?
m
= M
b
= B
Let's take a look at the graph:
The equation for the slope is m = \dfrac{y_2 - y_1}{x_2 - x_1}
.
Substitute both points.
m = \displaystyle \frac{Y2 - negParens(Y1)}{X2 - negParens(X1)} = fractionSimplification( Y2 - Y1, X2 - X1 )
.
Writing the equation of the line, we have y = ( M == -1 ? "-" : ( M == 1 ? "" : fractionReduce( Y2 - Y1, X2 - X1 ))) x + b
(the value of m
is equal to M
).
To find b
, we can substitute in either of the two points into the above equation. Let's go through both cases:
Using the first point (X1, Y1)
, substitute y = Y1
and x = X1
:
Y1 = (fractionReduce( Y2 - Y1, X2 - X1 ))(X1) + b
.
b = Y1 - fractionReduce( X1 * ( Y2 - Y1 ), X2 - X1 ) = fractionReduce( Y1 * (X2 - X1) - X1 * ( Y2 - Y1 ), X2 - X1 )
Using the second point (X2, Y2)
, substitute y = Y2
and x = X2
:
Y2 = (fractionReduce( Y2 - Y1, X2 - X1 ))(X2) + b
.
b = Y2 - fractionReduce( X2 * ( Y2 - Y1 ), X2 - X1 ) = fractionReduce( Y2 * (X2 - X1) - X2 * ( Y2 - Y1 ), X2 - X1 )
In both cases, the equation of the line is y = ( M == -1 ? "-" : ( M == 1 ? "" : fractionReduce( Y2 - Y1, X2 - X1 ))) x + fractionReduce( Y1 * (X2 - X1) - X1 * ( Y2 - Y1 ), X2 - X1 )
(the value of m
is equal to M
).