# Equation of a straight line

## Prerequisites

### Equation of a straight line

#### Introduction

• Given a set of coordinates such as: x 0 50 100 y 5 35 65
• We can plot these on a graph: • If all the points can be connected by a straight line then the relationship between x and y can be represented by the equation: • Where:
• m = the gradient of the slope.
• b = the point that the line intercepts the y axis.

#### Calculating the equation for the straight line

• Calculating m:
• The value for m can be found by calculating the gradient of the line: • Calculating b:
• If we know the value of y when x is 0, then b = y
• In the above example we are given the coordinate (0, 5)
• so b = 5
• If we do not know the value of y when x is 0, then:
• given a known coordinate, such as: (50, 35), and the gradient: 0.6
• then we plug these into our line equation: • And solve to find that b = 5
• Our equation for this straight line is therefore: #### code (Python)

import math

def findEquation(p1, p2):
deltaY = p2 - p1
deltaX = p2 - p1

if deltaX == 0:
return math.inf, 0
else:
m = deltaY / deltaX
b = p1 - m * p1
return m, b

m, b = findEquation([50, 35], [100, 65])
print(f'y = {m}x + {b}') # prints y = 0.6x + 5.0