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?
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}
RegionDistance
?
$\endgroup$
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
Projection[]
is built-in...
$\endgroup$
Commented
Dec 18, 2016 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[[1]]}],
Text[Framed[Norm[mp - ri[[1]]],
Background -> White], (mp + ri[[1]])/2]},
AspectRatio -> Automatic]],
{x1, 0, 1}, {y1, 0, 1}, {x3, 0, 1}]
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
RegionDistance[InfiniteLine[{{x1, y1}, {x2, y2}}], {x3, y4}]
$\endgroup$x1 = 0.; y1 = 532.964; x2 = 950; y2 = 545.204; x3 = 0.; y3 = 235.665; x4 = 950; y4 = 247.905;
$\endgroup$