Welcome to another back-breaking, laborious topic of linear algebra!
This time, we’ll be learning how to find the straight line equation, with two points already given to us. I have also coded a Python program to help us, but I’ll go through that later.
Contents
Equation of a Straight Line
So, as always, this is the universal equation of a straight line-
![]()
is the gradient, or the slope, or the steepness of the line.
is the y-intercept.
Finding the Equation given Two Points
So, the two points. Let’s say Point A is on (-5,10), and Point B is on (-3,4). We have them, but we don’t have the gradient, nor the y-intercept.
We can find the y-intercept later. first, we start by finding the gradient. I hope you know the formula for finding the gradient.
What? Oh, okay. Here it is if you don’t know-
![]()
Now, we substitute the two points into the formula and solve for the gradient-
![]()
![]()
![]()
Yay, now we have found the gradient! We just need the y-intercept now! You can use the x-coordinates and y-coordinates of either point to substitute into the formula. For example, I used Point B’s coordinates (-3,4).
![]()
![]()
![]()
![]()
![]()
Great! Now that we have both the gradient,-3, and the y-intercept,-5, we can complete the straight line equation-
![]()
The Python Program
Like I said earlier, I have written a Python program that helps us with this. I’ll break them up in subprograms and explain them.
main()
def main():
print("Welcome! Enter two points, and I'll make the straight line equation. i will automatically round up the numbers")
print("")
print("Point A")
a_xpoint = int(input("Enter x-coordinate: "))
a_ypoint = int(input("Enter y-coordinate: "))
print("")
print("Point B")
b_xpoint = int(input("Enter x-coordinate: "))
b_ypoint = int(input("Enter y-coordinate: "))
equation_maker(a_xpoint,a_ypoint,b_xpoint,b_ypoint)
another_equation()
main()
In line 1, the subprogram main() is defined. To know more about Subprograms, click here! Then, line 2 explains the objective of the program.
Lines 3-11 ask the user for the coordinates of two points.
Lines 13 and 14 call the subprograms that will be used to form the equation. Lastly, outside of main(), line 16 calls the main() subprogram.
equation_maker()
Next, we will go into equation_maker().
def equation_maker(a_xpoint,a_ypoint,b_xpoint,b_ypoint):
gradient = (b_ypoint - a_ypoint) / (b_xpoint - a_xpoint)
y_inter = a_ypoint - gradient * a_xpoint
r_gradient = round(gradient)
r_y_inter = round(y_inter)
if r_gradient == 0:
equation = "y" + "=" + str(r_y_inter)
elif r_gradient == 1:
if r_y_inter < 0:
equation = "y" + "=" + "x" + str(r_y_inter)
else:
equation = "y" + "=" + "x" + "+" + str(r_y_inter)
else:
if r_y_inter < 0:
equation = "y" + "=" + str(r_gradient) + "x" + str(r_y_inter)
elif r_y_inter == 0:
equation = "y" + "=" + str(r_gradient) + "x"
else:
equation = "y" + "=" + str(r_gradient) + "x" + "+" + str(r_y_inter)
print("")
print(equation)
So, line 1 defines equation_maker(), and passes in the arguments needed in the subprogram. Line 2 calculates the gradient using the formula, and line 3 calculates the y-intercept.
Lines 4 and 5 rounds up the gradient and y-intercept, so there won’t be decimal places.
And lines 7-22, form the equation, while lines 24 and 25 print it out.
another_equation()
Now, we enter another_equation(), that lets the user form another equation.
def another_equation():
print("")
confirm_another = input("Would you like to make another equation?: ").lower()
print("")
if confirm_another == "yes":
main()
Line 1 defines the subprogram. Line 3 asks if the users wants to form another straight line equation, and sets their answer to lowercase.
Then, in the last two lines, if the user answers ‘yes’, it’ll run that main() subprogram again.
The Whole Code
So, the whole code looks like this-
def equation_maker(a_xpoint,a_ypoint,b_xpoint,b_ypoint):
gradient = (b_ypoint - a_ypoint) / (b_xpoint - a_xpoint)
y_inter = a_ypoint - gradient * a_xpoint
r_gradient = round(gradient)
r_y_inter = round(y_inter)
if r_gradient == 0:
equation = "y" + "=" + str(r_y_inter)
elif r_gradient == 1:
if r_y_inter < 0:
equation = "y" + "=" + "x" + str(r_y_inter)
else:
equation = "y" + "=" + "x" + "+" + str(r_y_inter)
else:
if r_y_inter < 0:
equation = "y" + "=" + str(r_gradient) + "x" + str(r_y_inter)
elif r_y_inter == 0:
equation = "y" + "=" + str(r_gradient) + "x"
else:
equation = "y" + "=" + str(r_gradient) + "x" + "+" + str(r_y_inter)
print("")
print(equation)
def another_equation():
print("")
confirm_another = input("Would you like to make another equation?: ").lower()
print("")
if confirm_another == "yes":
main()
def main():
print("Welcome! Enter two points, and I'll make the straight line equation. i will automatically round up the numbers")
print("")
print("Point A")
a_xpoint = int(input("Enter x-coordinate: "))
a_ypoint = int(input("Enter y-coordinate: "))
print("")
print("Point B")
b_xpoint = int(input("Enter x-coordinate: "))
b_ypoint = int(input("Enter y-coordinate: "))
equation_maker(a_xpoint,a_ypoint,b_xpoint,b_ypoint)
another_equation()
main()
Conclusion
That’s all! I hope you found this page useful! Enjoy the best part of this – the conclusion.
Next, I’ll show you how to find the straight line equation given the gradient and one point. Click here!
