4
$\begingroup$

I have 2 parallel lines which are touching a rectangle. I know the coordinates (x1,y1), (x2,y2), (x3,y3), (x4,y4)

How can I find with Mathematica from that the orthogonal distance between the lines?

enter image description here

$\endgroup$
3
  • $\begingroup$ Can you post you real coordinate? $\endgroup$
    – yode
    Commented Oct 17, 2016 at 11:39
  • 1
    $\begingroup$ RegionDistance[InfiniteLine[{{x1, y1}, {x2, y2}}], {x3, y4}] $\endgroup$
    – yode
    Commented Oct 17, 2016 at 11:43
  • $\begingroup$ x1 = 0.; y1 = 532.964; x2 = 950; y2 = 545.204; x3 = 0.; y3 = 235.665; x4 = 950; y4 = 247.905; $\endgroup$
    – lio
    Commented Oct 17, 2016 at 11:45

3 Answers 3

7
$\begingroup$

As @yode points out MMA has clever built in functions like RegionDistance to help with problems like this.

However, in case speed is a requirement (if you had many such calculations to perform), you would certainly be better off in this instance implementing a mathematical solution:

ParallelLineDistance[{x1_, y1_}, {x2_, y2_}, {x3_, y3_}] := 
 Module[{w, v},
  w = {x3, y3} - {x1, y1};
  v = {x2, y2} - {x1, y1};
  Norm[w - v.w/Norm[v]^2 v]
 ]

Timings:

RepeatedTiming[
  ParallelLineDistance[{x1, y1}, {x2, y2}, {x3, y3}]
 ]

{0.00001189, 297.274}

RepeatedTiming[
  RegionDistance[InfiniteLine[{{x1, y1}, {x2, y2}}], {x3, y3}]
 ]

{0.00025, 297.274}

$\endgroup$
3
  • $\begingroup$ Thank you very much. Why is your solution so much faster than RegionDistance? $\endgroup$
    – lio
    Commented Oct 18, 2016 at 12:22
  • 3
    $\begingroup$ In short because RegionDistance can handle many other much broader cases, and therefore likely has to do a lot of checking / pre-parsing of the problem - you'd have to ask others on the site here for the details if you're interested. Whereas simple algebra is about as quick as it gets. Note that if speed really is an issue then there may be faster ways still, especially in the context of evaluating ParallelLineDistance over a list. $\endgroup$ Commented Oct 18, 2016 at 13:05
  • $\begingroup$ By the way, Projection[] is built-in... $\endgroup$ Commented Dec 18, 2016 at 15:16
4
$\begingroup$
Manipulate[
 Module[{mp = {x1, 1 + y1}/2, cp, ri},
  cp = Cross[mp - {0, y1}];
  ri = RegionIntersection[InfiniteLine[{mp, mp + cp}], 
    InfiniteLine[{{x3, 0}, {x3 + (1/(1 - y1)) x1, 1}}]]; 
  Graphics[{Thickness[0.01], FaceForm[None], EdgeForm[Black], 
    Rectangle[], 
    Point[{{0, y1}, {x1, 1}, {x3, 0}, {x3 + (1/(1 - y1)) x1, 1}}], 
    Green, Line[{{0, y1}, {x1, 1}}], Red, 
    Line[{{x3, 0}, {x3 + (1/(1 - y1)) x1, 1}}], Blue, 
    Line[{mp, ri[[1]]}], 
    Text[Framed[Norm[mp - ri[[1]]], 
      Background -> White], (mp + ri[[1]])/2]}, 
   AspectRatio -> Automatic]],
 {x1, 0, 1}, {y1, 0, 1}, {x3, 0, 1}]

enter image description here

$\endgroup$
3
$\begingroup$

A variation of the answer by Quantum_Oli

Your data:

x1=0.;y1=532.964;x2=950;y2=545.204;x3=0.;y3=235.665;x4=950;y4=247.905;

$\{\text{$\Delta $x},\text{$\Delta $y}\}$ from Point3 to the Point1

p3=Flatten@Differences[{{x3,y3},{x1,y1}}]

{0.,297.299}

$\{\text{$\Delta $x},\text{$\Delta $y}\}$ from Point2 to the Point1

p2=Flatten@Differences[{{x2,y2},{x1,y1}}]

{-950.,-12.24}

Distance between the lines

DistanceBetweenLines[w_,v_]:=Norm[w-v.w/Norm[v]^2 v]
DistanceBetweenLines[p3,p2]

297.274

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