Find the intersection of a line with a plane: Difference between revisions
Find the intersection of a line with a plane (view source)
Revision as of 13:00, 27 August 2022
, 1 year agosyntax highlighting fixup automation
Drkameleon (talk | contribs) (Added Arturo implementation) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 11:
=={{header|11l}}==
<
R ray_point - ray_direction * dot(ray_point - plane_point, plane_normal) / dot(ray_direction, plane_normal)
print(‘The ray intersects the plane at ’intersection_point((0.0, -1.0, -1.0), (0.0, 0.0, 10.0), (0.0, 0.0, 1.0), (0.0, 0.0, 5.0)))</
{{out}}
Line 23:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
DEFINE REALPTR="CARD"
Line 137:
Vector(r5,r1,r0,planePoint)
Test(rayVector,rayPoint,planeNormal,planePoint)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_the_intersection_of_a_line_with_a_plane.png Screenshot from Atari 8-bit computer]
Line 155:
=={{header|Ada}}==
<
with Ada.Text_IO;
Line 210:
Plane_Normal => (0.0, 0.0, 1.0),
Plane_Point => (0.0, 0.0, 5.0)));
end Intersection;</
{{out}}
<pre>( 0.000,-5.000, 5.000)</pre>
=={{header|APL}}==
<
⍝ The intersection I belongs to a line defined by point L and vector V, translates to:
⍝ A real parameter t exists, that satisfies I = L + tV
Line 233:
t ← ((P - L) dot N) ÷ V dot N
I ← L + t × V
</syntaxhighlight>
{{out}}
<pre>
Line 242:
=={{header|Arturo}}==
{{trans|Nim}}
<
addv: function [v1 :vector, v2 :vector]->
Line 269:
to :vector @[0.0, 0.0, 5.0]
print ["Intersection at:" coords]</
{{out}}
Line 276:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">/*
; https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection#Algebraic_form
l = line vector
Line 317:
Vector_Dot(v, w){
return v.1*w.1 + v.2*w.2 + v.3*w.3
}</
Examples:<
l1 := [0, -1, -1]
lo1 := [0, 0, 10]
Line 346:
}
MsgBox % output
return</
{{out}}
<pre>0.000000, -5.000000, 5.000000
Line 355:
=={{header|C}}==
Straightforward application of the intersection formula, prints usage on incorrect invocation.
<syntaxhighlight lang="c">
#include<stdio.h>
Line 408:
return 0;
}
</syntaxhighlight>
Invocation and output:
<pre>
Line 416:
=={{header|C sharp|C#}}==
<
namespace FindIntersection {
Line 467:
}
}
}</
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
Line 473:
=={{header|C++}}==
{{trans|Java}}
<
#include <sstream>
Line 526:
return 0;
}</
{{out}}
<pre>The ray intersects the plane at (0, -5, 5)</pre>
Line 532:
=={{header|D}}==
{{trans|Kotlin}}
<
struct Vector3D {
Line 587:
auto ip = intersectPoint(rv, rp, pn, pp);
writeln("The ray intersects the plane at ", ip);
}</
{{out}}
Line 594:
=={{header|F Sharp|F#}}==
{{trans|C#}}
<
type Vector(x : double, y : double, z : double) =
Line 626:
Console.WriteLine("The ray intersects the plane at {0}", ip)
0 // return an integer exit code</
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
Line 632:
=={{header|Factor}}==
{{trans|11l}}
<
:: intersection-point ( rdir rpt pnorm ppt -- loc )
Line 638:
"The ray intersects the plane at " write
{ 0 -1 -1 } { 0 0 10 } { 0 0 1 } { 0 0 5 } intersection-point .</
{{out}}
<pre>
Line 645:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 711:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>line intersects the plane at (0, -5, 5)</pre>
Line 717:
=={{header|Go}}==
{{trans|Kotlin}}
<
import "fmt"
Line 758:
ip := intersectPoint(rv, rp, pn, pp)
fmt.Println("The ray intersects the plane at", ip)
}</
{{out}}
Line 767:
=={{header|Groovy}}==
{{trans|Java}}
<
private static class Vector3D {
private double x, y, z
Line 815:
println("The ray intersects the plane at $ip")
}
}</
{{out}}
<pre>The ray intersects the plane at (0.0, -5.0, 5.0)</pre>
Line 822:
{{trans|Kotlin}}
Note that V3 is implemented similarly in the external library [https://hackage.haskell.org/package/linear-1.20.7/docs/Linear-V3.html linear].
<
import Text.Printf (printf)
Line 864:
rp = V3 0 0 10
pn = V3 0 0 1
pp = V3 0 0 5</
{{out}}
<pre>The ray intersects the plane at (0.0, -5.0, 5.0)</pre>
Line 870:
=={{header|J}}==
'''Solution:'''
<
p=: mp&{: %~ -~&{. mp {:@] NB. solve
intersectLinePlane=: [ +/@:* 1 , p NB. substitute</
'''Example Usage:'''
<
Plane=: 0 0 5 ,: 0 0 1 NB. Point, Normal
Line intersectLinePlane Plane
0 _5 5</
=={{header|Java}}==
{{trans|Kotlin}}
<
private static class Vector3D {
private double x, y, z;
Line 929:
System.out.println("The ray intersects the plane at " + ip);
}
}</
{{out}}
<pre>The ray intersects the plane at (0.000000, -5.000000, 5.000000)</pre>
Line 938:
In the following, a 3d vector is represented by a JSON array: [x, y, z]
<
def addVector:
transpose | add;
Line 966:
"The ray intersects the plane at:",
intersectPoint(rv; rp; pn; pp)</
{{out}}
<pre>
Line 978:
{{trans|Python}}
<
ndotu = dot(planenorm, raydir)
if ndotu ≈ 0 error("no intersection or line is within plane") end
Line 997:
ψ = lineplanecollision(planenorm, planepnt, raydir, raypnt)
println("Intersection at $ψ")</
{{out}}
Line 1,003:
=={{header|Kotlin}}==
<
class Vector3D(val x: Double, val y: Double, val z: Double) {
Line 1,038:
val ip = intersectPoint(rv, rp, pn, pp)
println("The ray intersects the plane at $ip")
}</
{{out}}
Line 1,046:
=={{header|Lua}}==
<
return {x=xval, y=yval, z=zval}
end
Line 1,083:
pp = make(0.0, 0.0, 5.0)
ip = intersectPoint(rv, rp, pn, pp)
print("The ray intersects the plane at " .. tostr(ip))</
{{out}}
<pre>The ray intersects the plane at (0, -5, 5)</pre>
=={{header|Maple}}==
<
geom3d:-line(L, [geom3d:-point(p2,0,0,10), [0,-1,-1]]);
geom3d:-intersection(px,L,P);
geom3d:-detail(px);</
{{Out}}
<pre>[["name of the object",px],["form of the object",point3d],["coordinates of the point",[0,-5,5]]]</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
{{out}}
<pre>Point[{0, -5, 5}]</pre>
Line 1,103:
=={{header|MATLAB}}==
{{trans|Kotlin}}
<
pdiff = rayPoint - planePoint;
Line 1,110:
prod3 = prod1 / prod2;
point = rayPoint - rayVector * prod3;</
{{out}}
<
ans =
0 -5 5
</syntaxhighlight>
=={{header|Modula-2}}==
<
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,181:
ReadChar;
END LinePlane.</
=={{header|Nim}}==
<syntaxhighlight lang="nim">
type Vector = tuple[x, y, z: float]
Line 1,218:
planeVector = (0.0, 0.0, 1.0),
planePoint = (0.0, 0.0, 5.0))
echo "Intersection at ", coords</
{{out}}
Line 1,225:
=={{header|Perl}}==
{{trans|Raku}}
<
package Plane; sub new { my ($c, $a) = @_; my $self = { V0 => $a->{V0}, n => $a->{n} } } # point / normal
Line 1,252:
my $P = Plane->new({ V0=>[0,0,5 ], n=>[0, 0, 1]});
print 'Intersection at point: ', join(' ', line_plane_intersection($L, $P)) . "\n";
</syntaxhighlight>
{{out}}
<pre>Intersection at point: 0 -5 5</pre>
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">dot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 1,272:
<span style="color: #0000FF;">?</span><span style="color: #000000;">intersection_point</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- (parallel to plane)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">intersection_point</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- (line within plane)</span>
<!--</
{{out}}
<pre>
Line 1,284:
{{trans|Java}}
{{works with|Picat}}
<syntaxhighlight lang="picat">
plus(U, V) = {U[1] + V[1], U[2] + V[2], U[3] + V[3]}.
Line 1,311:
IntersectPoint[3]
).
</syntaxhighlight>
{{out}}
<pre>
Line 1,322:
{{works with|GNU Prolog}}
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">
:- initialization(main).
Line 1,368:
intersect_point(RayVector, RayPoint, PlaneNormal, PlanePoint, p(X, Y, Z)),
format("The ray intersects the plane at (~f, ~f, ~f)\n", [X, Y, Z]).
</syntaxhighlight>
{{out}}
<pre>
Line 1,378:
Based on the approach at geomalgorithms.com<ref>http://geomalgorithms.com/a05-_intersect-1.html</ref>
<
from __future__ import print_function
import numpy as np
Line 1,404:
Psi = LinePlaneCollision(planeNormal, planePoint, rayDirection, rayPoint)
print ("intersection at", Psi)</
{{out}}
Line 1,411:
=={{header|R}}==
{{trans|MATLAB}}
<
pdiff <- ray_point - plane_point
Line 1,420:
return(point)
}</
{{out}}
<
[1] 0 -5 5</
=={{header|Racket}}==
{{trans|Sidef}}
<
;; {{trans|Sidef}}
;; vectors are represented by lists
Line 1,450:
(line-plane-intersection (Line '(0 0 10) '(0 -1 -1))
(Plane '(0 0 5) '(0 0 1)))
'(0 -5 5)))</
{{out}}
Line 1,460:
{{trans|Python}}
<syntaxhighlight lang="raku"
has $.P0; # point
has $.u⃗; # ray
Line 1,483:
Line.new( :P0(0,0,10), :u⃗(0,-1,-1) ),
Plane.new( :V0(0,0, 5), :n⃗(0, 0, 1) )
);</
{{out}}
<pre>Intersection at point: (0 -5 5)</pre>
Line 1,490:
===version 1===
This program does NOT handle the case when the line is parallel to or within the plane.
<
Parse Value '0 0 1' With n.1 n.2 n.3 /* Normal Vector of the plane */
Parse Value '0 0 5' With p.1 p.2 p.3 /* Point in the plane */
Line 1,508:
z=a.3+t*v.3
Say 'Intersection: P('||x','y','z')'</
{{out}}
Line 1,516:
===version 2===
handle the case that the line is parallel to the plane or lies within it.
<
Parse Value '1 2 3' With n.1 n.2 n.3
Parse Value '3 3 3' With p.1 p.2 p.3
Line 1,638:
End
End
Return res </
{{out}}
<pre>Plane definition: x+2*y+3*z=18
Line 1,646:
=={{header|Ruby}}==
{{trans|C#}}
<
def intersectPoint(rayVector, rayPoint, planeNormal, planePoint)
Line 1,665:
end
main()</
{{out}}
<pre>The ray intersects the plane at Vector[0.0, -5.0, 5.0]</pre>
Line 1,672:
{{trans|Kotlin}}
<
#[derive(Copy, Clone, Debug, PartialEq)]
Line 1,777:
println!("{:?}", intersect(rv, rp, pn, pp));
}
</syntaxhighlight>
=={{header|Scala}}==
<
val (rv, rp, pn, pp) =
(Vector3D(0.0, -1.0, -1.0), Vector3D(0.0, 0.0, 10.0), Vector3D(0.0, 0.0, 1.0), Vector3D(0.0, 0.0, 5.0))
Line 1,805:
println(s"The ray intersects the plane at $ip")
}</
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/oLTlNZk/0 ScalaFiddle (JavaScript)].
=={{header|Sidef}}==
{{trans|Raku}}
<
P0, # point
u⃗, # ray
Line 1,833:
Line(P0: [0,0,10], u⃗: [0,-1,-1]),
Plane(V0: [0,0, 5], n⃗: [0, 0, 1]),
))</
{{out}}
<pre>Intersection at point: [0, -5, 5]</pre>
Line 1,839:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Class Vector3D
Line 1,890:
End Sub
End Module</
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
Line 1,896:
=={{header|Wren}}==
{{trans|Kotlin}}
<
construct new(x, y, z) {
_x = x
Line 1,931:
var pp = Vector3D.new(0, 0, 5)
var ip = intersectPoint.call(rv, rp, pn, pp)
System.print("The ray intersects the plane at %(ip).")</
{{out}}
Line 1,940:
=={{header|zkl}}==
{{trans|Raku}}{{trans|Python}}
<
class Plane{ fcn init(pxyz, normal_xyz){ var pt=pxyz, normal=normal_xyz; } }
Line 1,953:
//w.zipWith('+,line.ray.apply('*,si)).zipWith('+,plane.pt); // or
w.zipWith('wrap(w,r,pt){ w + r*si + pt },line.ray,plane.pt);
}</
<
Line( T(0.0, 0.0, 10.0), T(0.0, -1.0, -1.0) ),
Plane(T(0.0, 0.0, 5.0), T(0.0, 0.0, 1.0) ))
);</
{{out}}
<pre>
|