0
$\begingroup$

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

$\endgroup$
9
  • 3
    $\begingroup$ p1 + t (p2 - p1) ? $\endgroup$
    – Kuba
    Commented Apr 22, 2014 at 10:07
  • $\begingroup$ @Kuba That gives the complete line. I want the line to be defined from one point to another, not outside. $\endgroup$
    – Arcotick
    Commented Apr 22, 2014 at 10:10
  • 2
    $\begingroup$ @Arcotick You just got to restrict t to between 0 and 1. (You can use Piecewise for this.) $\endgroup$
    – C. E.
    Commented Apr 22, 2014 at 10:14
  • 4
    $\begingroup$ 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. $\endgroup$
    – Kuba
    Commented Apr 22, 2014 at 10:21
  • 1
    $\begingroup$ 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 $\endgroup$
    – Kuba
    Commented Apr 22, 2014 at 10:35

1 Answer 1

1
$\begingroup$
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.