Distance between parallel lines inside of a rectangle

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?

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

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}

• Thank you very much. Why is your solution so much faster than RegionDistance?
– lio
Oct 18 '16 at 12:22
• 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. Oct 18 '16 at 13:05
• By the way, Projection[] is built-in... Dec 18 '16 at 15:16
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[]}],
Text[Framed[Norm[mp - ri[]],
Background -> White], (mp + ri[])/2]},
AspectRatio -> Automatic]],
{x1, 0, 1}, {y1, 0, 1}, {x3, 0, 1}] A variation of the answer by Quantum_Oli

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