the cedar ledge

# Finding a Line Which Contains Two Points

Date: May 20 2020

Summary: An explanation on how to find a line that fits to two points

Keywords: ##zettel #algebra #line #point #slope #formula #archive

# Bibliography

Not Available

### Algorithm

Let $p_{1} = (x_{1}, y_{1})$, $p_{2} = (x_{2}, y_{2})$, $p = (x_{p}, y_{p})$ where $p_{1}$ and $p_{2}$ are two points of interest and $p$ is either $p_{1}$ or $p_{2}$. The algorithm for determining the line that contains points $p_{1}$ and $p_{2}$ utilizes the Point-Slope Formula:

$y_{2} - y_{1} = m(x_{2} - x_{1})$

Can be rewritten such that

$m = \frac{ y_{2} - y_{1}}{x_{2} - x_{1}}$

To define the slope of the line in question. To generalize this to a generic solution, one reevaluates for the point slope formula using $y$ and $x$ as general terms:

$y - y_{p} = m(x - x_{p})$

To produce the final generic equation, reorganizing yields:

$y(x) = \frac{x \cdot \left( y_{2} - y_{1} \right)}{x_{2} - x_{1}} - \frac{x_{p} \cdot \left( y_{2} - y_{1} \right)}{x_{2} - x_{1}} + y_{p}$

### Example

using Plots
gr()

# Utilizing an implicit return from the generic function
y(x, x_1, y_1, x_2, y_2) = x .* (y_2 - y_1) ./ (x_2 - x_1) .- x_1 .* (y_2 - y_1) ./ (x_2 - x_1) .+ y_1

input = -5:5
output = y(input, 2, -1, 0, 3)

plot(input,
output,
framestyle=:zerolines,
label="Fitted Line",
title="Line Fitting Two Points",
xlim=(-5, 5),
ylim=(-5, 5)
)
scatter!((2, -1), label="Point 1", marker=5)
scatter!((0, 3), label="Point 2", marker=5) ## How To Cite

Zelko, Jacob. Finding a Line Which Contains Two Points. https://jacobzelko.com/05202020224416-line-two-points. May 20 2020.

## Discussion:

CC BY-SA 4.0 Jacob Zelko. Last modified: January 17, 2023. Website built with Franklin.jl and the Julia programming language.