# Define function that goes from one point to another parametrically

I have two points p1 and p2, and I want to get the parametric equation of a linear function that passes through those, but ranging from one point to another.

For example, if I have 2 points {0,0} and {1,1/2}, the equation would be {t,t/2}.

The goal of having a parametric equation is to have the function defined even when the slope is $\infty$.

I tried this (here the angle[] function is Atan2, is basically ArcTan but ranging from 0-2π):

Piecewise[
{
{{t, Last@p1 + Tan@angle[p1, p2] (t - First@p1)},
Min[First@p1, First@p2] <= t && t <= Max[First@p1, First@p2]},
{{First@p1, t}, First@p1 == First@p2}
},
Indeterminate
]


But when a point is on top of the other the function is a vertical line, ranging from $-\infty$ to $\infty$; I would only want the function to be defined from Last@p1 to Last@p2.

In other words, I want a Graphics' Line, but using parametric equations.

Something like this:

-
p1 + t (p2 - p1) ? – Kuba Apr 22 '14 at 10:07
@Kuba That gives the complete line. I want the line to be defined from one point to another, not outside. – Arcotick Apr 22 '14 at 10:10
@Arcotick You just got to restrict t to between 0 and 1. (You can use Piecewise for this.) – C. E. Apr 22 '14 at 10:14
line[t_] := p1 + t (p2 - p1) /; 0 <= t <= 1 but your question is still unclear. Please refer to my previous question. And focus on what you are writing. – Kuba Apr 22 '14 at 10:21
You can still proceed with the function I've given, just Solve/NMinimize for t1, t2 with restrictions. Also, take a look at Line intersection algorithm – Kuba Apr 22 '14 at 10:35

line[t_, p1_, p2_] := p1 + t (p2 - p1)

Solve[Join[
{line[t1, {0, 0}, {1, 1}] == line[t2, {0, 1}, {1.2, 0}]},
{0 <= t1 <= 1, 0 <= t2 <= 1}],
{t1, t2}]

{{t1 -> 0.545455, t2 -> 0.454545}}

{line[t1, {0, 0}, {1, 1}], line[t2, {0, 1}, {1.2, 0}]} /. %

{{{0.545455, 0.545455},
{0.545455, 0.545455}}}


Implementation of Balaban's Line intersection algorithm in Mathematica is also very closely related.

-