Python line between two points

First, let's see it in action. Here are two points (you can drag them) and the equation of the line through them. Explanations follow.

../geometry/images/geom-line-equn.js

The Points

We use Cartesian Coordinates to mark a point on a graph by how far along and how far up it is:


Example: The point (12,5) is 12 units along, and 5 units up

Steps

There are 3 steps to find the Equation of the Straight Line :

  • 1. Find the slope of the line
  • 2. Put the slope and one point into the "Point-Slope Formula"
  • 3. Simplify

Step 1: Find the Slope (or Gradient) from 2 Points

What is the slope (or gradient) of this line?

Python line between two points

We know two points:

  • point "A" is (6,4) (at x is 6, y is 4)
  • point "B" is (2,3) (at x is 2, y is 3)

The slope is the change in height divided by the change in horizontal distance.

Looking at this diagram ...

Python line between two points

Slope m  =  change in ychange in x  =  yA − yBxA − xB

In other words, we:

  • subtract the Y values,
  • subtract the X values
  • then divide

Like this:

m  =   change in y change in x   =   4−3 6−2   =   1 4 = 0.25

It doesn't matter which point comes first, it still works out the same. Try swapping the points:

m  =   change in y change in x   =   3−4 2−6   =   −1 −4 = 0.25

Same answer.

Step 2: The "Point-Slope Formula"

Now put that slope and one point into the "Point-Slope Formula"

Python line between two points

Start with the "point-slope" formula (x1 and y1 are the coordinates of a point on the line):

y − y1 = m(x − x1)

We can choose any point on the line for x1 and y1, so let's just use point (2,3):

y − 3 = m(x − 2)

We already calculated the slope "m":

m = change in ychange in x = 4−36−2 = 14

And we have:

y − 3 = 14(x − 2)

That is an answer, but we can simplify it further.

Step 3: Simplify

Start with:y − 3 = 14(x − 2)

Multiply 14 by (x−2):y − 3 = x424

Add 3 to both sides:y = x424 + 3

Simplify:y = x4 + 52

And we get:

y = x4 + 52

Which is now in the Slope-Intercept (y = mx + b) form.

 

Check It!

Let us confirm by testing with the second point (6,4):

y = x/4 + 5/2 = 6/4 + 2.5 = 1.5 + 2.5 = 4

Yes, when x=6 then y=4, so it works!

Another Example

Example: What is the equation of this line?

Python line between two points

Start with the "point-slope" formula:

y − y1 = m(x − x1)

Put in these values:

  • x1 = 1
  • y1 = 6
  • m = (2−6)/(3−1) = −4/2 = −2

And we get:

y − 6 = −2(x − 1)

Simplify to Slope-Intercept (y = mx + b) form:

y − 6 = −2x + 2

y = −2x + 8

DONE!

The Big Exception

The previous method works nicely except for one particular case: a vertical line:

Python line between two points

A vertical line's gradient is undefined (because we cannot divide by 0):

m = yA − yBxA − xB = 4 − 12 − 2 = 30 = undefined

But there is still a way of writing the equation: use x= instead of y=, like this:

x = 2

 

 

7270, 525, 526, 1165, 1166, 7291, 7292, 7300, 7301, 7302

Equation of a Straight Line Straight Line Graph Calculator Algebra Index

Write a program to compute the slope of a line between two points (x1, y1) and (x2, y2).

September 05, 2022

Write a program to compute the slope of a line between two points (x1, y1) and (x2, y2).

Slope = y2-y1/x2-x1

Code

# Python program for slope of line

def slope(x1, y1, x2, y2):

if(x2 - x1 != 0):

return (float)(y2-y1)/(x2-x1)

return None

# driver code

x1 = 4

y1 = 2

x2 = 2

y2 = 5

print("Slope is :", slope(x1, y1, x2, y2))

Output

Slope is : -1.5




Happy coding :)

Tags python

How do you find the line between two points?

Steps to find the equation of a line from two points:.
Find the slope using the slope formula. ... .
Use the slope and one of the points to solve for the y-intercept (b). ... .
Once you know the value for m and the value for b, you can plug these into the slope-intercept form of a line (y = mx + b) to get the equation for the line..

What is the equation for a line in Python?

The equation y=mx+c y = m x + c represents a straight line graphically, where m is its slope/gradient and c its intercept.

How to create a line in Python?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.

How do I draw a line in Matplotlib Python?

Steps to Plot a Line Chart in Python using Matplotlib.
Step 1: Install the Matplotlib package. ... .
Step 2: Gather the data for the Line chart. ... .
Step 3: Capture the data in Python. ... .
Step 4: Plot a Line chart in Python using Matplotlib..