Equation of a straight line

Prerequisites

Cartesian Coordinates Show

Gradient of a straight line Show

Equation of a straight line

Introduction

  • Given a set of coordinates such as:
    x050100
    y53565
  • We can plot these on a graph: graph of a straight line
  • If all the points can be connected by a straight line then the relationship between x and y can be represented by the equation: y = mx + b
  • 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: m = \frac{35 - 5}{50 - 0} = \frac{30}{50} = 0.6
  • 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: 35 = 0.6 * 50 + b
      • And solve to find that b = 5
  • Our equation for this straight line is therefore: y = 0.6x + 5

code (Python)

import math

def findEquation(p1, p2):
    deltaY = p2[1] - p1[1]
    deltaX = p2[0] - p1[0]

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

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