# Equation of a line

### From Devipedia

The equation of a line is a rule that allows us to determine if any point (x,y) lies on a line.

Because we have already determined the slope (gradient) and because we have at least one other point on the line, it's easy to determine if any other point is on the line using the equation y = mx+b. Note that any straight line can be represented by an equation: y = mx + b.

from turtle import * from math import * setworldcoordinates(-15,-15,15,15) # define our points x1 = 12.0 x2 = 2.0 y1 = 8.0 y2 = 3.0 penup() goto(x1,y1) pendown() dot(10, 'red') # the first point goto(x2,y2) # draws a thin line to the second point dot(10, 'red') # the second point m = (y1-y2)/(x1-x2) # Define function that tests if point is on our lines def find_on_line(x,y): if (y - y1 == m * (x - x1)): return True else: return False # Ask for a point and test to see if it falls on either of our line segments while True: t = raw_input('Enter a point in the form x,y: ') p = t.split(',') if len(p) != 2: break x = int(p[0]) y = int(p[1]) penup() goto(x, y) pendown() if find_on_line(x,y): dot(10, 'green') print "found it" else: dot(10, 'red')

I did say that the equation of the line can be written as y = mx+b---and then I turned around and implemented find_on_line() with the equation y - y1 = m(x - x1). What's up with that? We'll get to that in a minute.

The program above takes a known point (x1,y1), a known slope (m), and a point entered from the command line (x,y). If the equation is satisfied, we draw a green dot and print "found it". Otherwise, we draw a red dot to show where we missed. It's a bit like playing Battleship when you think about it.

In the source code above, I used the equation y - y1 = m(x - x1) to determine if any sample point (x,y) was on a pre-defined line. Because we drew the line by specifying two points to turtle, we knew of a point on the line (x1, y1) and, because we had solved for the gradient, we also knew the slope. It was easy enough to find if our point was on the line by plugging in the variables and determining if both sides of the equation were the same.

What if I'd had the gradient of the line but no comparison point? Using y = mx +b we could find the y-intercept, the point on the y-axis where the line crosses if the x-axis value is zero. I whipped up a little function to demonstrate:

def find_b(x,y,m): #solve for b... # y = mx + b b = y + -(m*x) print "y-intercept is %d" % b print "eq2 is solved: %d = %d" % (y, (m*x)+b) return b

Using this function, we can calculate the y-intercept given only a gradient value and any arbitrary x,y point value. For example, if the point is (4,5) and slope is .5, b is 3.

To test our result, we pass (0,b) as the x,y values to the function find_on_line(). The program changes in the following version don't add functionality, but do programatically show the equation y = mx +b in action.

from turtle import * from math import * setworldcoordinates(-15,-15,15,15) # define our points x1 = 12.0 x2 = 2.0 y1 = 8.0 y2 = 3.0 penup() goto(x1,y1) pendown() dot(10, 'red') # the first point goto(x2,y2) # draws a thin line to the second point dot(10, 'red') # the second point m = (y1-y2)/(x1-x2) # Define function that tests if point is on our lines def find_on_line(x,y,m,x1,y1): if (y - y1 == m * (x - x1)): print "eq1 true: %d = %d" % (y-y1, m*(x-x1)) return True else: print "eq1 false: %d = %d" % (y-y1, m*(x-x1)) return False def find_y_intercept(x,y,m): #solve for b... # y = mx + b b = y + -(m*x) print "slope is %f" % m print "y-intercept is %f" % b print "eq2 is solved: %f = %f" % (y, (m*x)+b) return b # Ask for a point and test to see if it falls on either of our line segments while True: t = raw_input('Enter a point in the form x,y: ') p = t.split(',') if len(p) != 2: break x = int(p[0]) y = int(p[1]) penup() goto(x, y) pendown() # find using x,y, point on line (x1,y1) and slope (m) if find_on_line(x,y,m,x1,y1): dot(10, 'green') print "found it" else: dot(10, 'red') # find point using only x,y and slope as inputs b = find_y_intercept(x,y,m) if find_on_line(0,b,m,x1,y1): dot(6, 'blue') print "found b"