Finding the change in x and y
- In our above example, our first coordinate was (0, 0). This made it easy to calculate how much x and y changed.
- However when both coordinates are non-zero, it gets a bit more complicated.
- For example, calculate the gradient of the line between (1, 2) and (7, 10)
- To find how much x and y change, we need to subtract the first coordinate from the second
- We can use this in our equation to find the gradient of any line:
Special Cases
- There are two special cases to look out for when calculating the gradient of a line:
- When the line is horizontal
- There will be no change in y
- The gradient is 0
- When the line is vertical
- There will be no change in x
- The gradient is infinity
code (Python)
import math
def findGradient(p1, p2):
deltaY = p2[1] - p1[1]
deltaX = p2[0] - p1[0]
if deltaX == 0:
return math.inf
else:
return deltaY / deltaX
gradient = findGradient([1, 2], [7, 10])
print(gradient) # prints 1.3333333333333333