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


Not Available

Table of Contents

    1. Algorithm
    2. Example
  1. How To Cite
  2. References
  3. Discussion:


Let p1=(x1,y1)p_{1} = (x_{1}, y_{1}), p2=(x2,y2)p_{2} = (x_{2}, y_{2}), p=(xp,yp)p = (x_{p}, y_{p}) where p1p_{1} and p2p_{2} are two points of interest and pp is either p1p_{1} or p2p_{2}. The algorithm for determining the line that contains points p1p_{1} and p2p_{2} utilizes the Point-Slope Formula:

y2y1=m(x2x1) y_{2} - y_{1} = m(x_{2} - x_{1})

Can be rewritten such that

m=y2y1x2x1 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 yy and xx as general terms:

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

To produce the final generic equation, reorganizing yields:

y(x)=x(y2y1)x2x1xp(y2y1)x2x1+yp 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}


using Plots

# 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)

     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)

output image

How To Cite

Zelko, Jacob. Finding a Line Which Contains Two Points. May 20 2020.



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