0

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:

enter image description here

Arcotick
  • 629
  • 3
  • 13

1 Answers1

1
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.

Kuba
  • 136,707
  • 13
  • 279
  • 740