Find the intersection of a line with a plane: Difference between revisions

m
→‎{{header|Wren}}: Added libheader.
(Added 11l)
m (→‎{{header|Wren}}: Added libheader.)
 
(48 intermediate revisions by 23 users not shown)
Line 1:
[[Category:Geometry]]
[[Category:Collision detection]]
{{task}}
{{draft task}}Finding the intersection of an infinite ray with a plane in 3D is an important topic in collision detection.
Finding the intersection of an infinite ray with a plane in 3D is an important topic in collision detection.
 
 
;Task:
Find the point of intersection for the infinite ray with direction   (0, -1, -1)   passing through position   (0, 0, 10)   with the infinite plane with a normal vector of   (0, 0, 1)   and which passes through [0, 0, 5].
 
<br><br>
Find the point of intersection for the infinite ray with direction (0,-1,-1) passing through position (0, 0, 10) with the infinite plane with a normal vector of (0, 0, 1) and which passes through [0, 0, 5].
 
=={{header|11l}}==
 
<langsyntaxhighlight lang="11l">F intersection_point(ray_direction, ray_point, plane_normal, plane_point)
returnR 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)))</langsyntaxhighlight>
 
{{out}}
<pre>
The ray intersects the plane at (0, -5, 5)
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE REALPTR="CARD"
TYPE VectorR=[REALPTR x,y,z]
 
PROC PrintVector(VectorR POINTER v)
Print("(") PrintR(v.x)
Print(",") PrintR(v.y)
Print(",") PrintR(v.z)
Print(")")
RETURN
 
PROC Vector(REAL POINTER vx,vy,vz VectorR POINTER v)
v.x=vx v.y=vy v.z=vz
RETURN
 
PROC VectorSub(VectorR POINTER a,b,res)
RealSub(a.x,b.x,res.x)
RealSub(a.y,b.y,res.y)
RealSub(a.z,b.z,res.z)
RETURN
 
PROC VectorDot(VectorR POINTER a,b REAL POINTER res)
REAL tmp1,tmp2
 
RealMult(a.x,b.x,res)
RealMult(a.y,b.y,tmp1)
RealAdd(res,tmp1,tmp2)
RealMult(a.z,b.z,tmp1)
RealAdd(tmp1,tmp2,res)
RETURN
 
PROC VectorMul(VectorR POINTER a REAL POINTER b VectorR POINTER res)
RealMult(a.x,b,res.x)
RealMult(a.y,b,res.y)
RealMult(a.z,b,res.z)
RETURN
 
BYTE FUNC IsZero(REAL POINTER a)
CHAR ARRAY s(10)
 
StrR(a,s)
IF s(0)=1 AND s(1)='0 THEN
RETURN (1)
FI
RETURN (0)
 
BYTE FUNC Intersection(VectorR POINTER
rayVector,rayPoint,planeNormal,planePoint,result)
REAL tmpx,tmpy,tmpz,prod1,prod2,prod3
VectorR tmp
 
Vector(tmpx,tmpy,tmpz,tmp)
 
VectorSub(rayPoint,planePoint,tmp)
VectorDot(tmp,planeNormal,prod1)
VectorDot(rayVector,planeNormal,prod2)
 
IF IsZero(prod2) THEN
RETURN (1)
FI
 
RealDiv(prod1,prod2,prod3)
VectorMul(rayVector,prod3,tmp)
VectorSub(rayPoint,tmp,result)
RETURN (0)
 
PROC Test(VectorR POINTER rayVector,rayPoint,planeNormal,planePoint)
BYTE res
REAL px,py,pz
VectorR p
 
Vector(px,py,pz,p)
res=Intersection(rayVector,rayPoint,planeNormal,planePoint,p)
 
Print("Ray vector: ")
PrintVector(rayVector) PutE()
Print("Ray point: ")
PrintVector(rayPoint) PutE()
Print("Plane normal: ")
PrintVector(planeNormal) PutE()
Print("Plane point: ")
PrintVector(planePoint) PutE()
 
IF res=0 THEN
Print("Intersection point: ")
PrintVector(p) PutE()
ELSEIF res=1 THEN
PrintE("There is no intersection")
FI
PutE()
RETURN
 
PROC Main()
REAL r0,r1,r5,r10,rm1
VectorR rayVector,rayPoint,planeNormal,planePoint
 
Put(125) PutE() ;clear screen
 
ValR("0",r0) ValR("1",r1) ValR("5",r5)
ValR("10",r10) ValR("-1",rm1)
 
Vector(r0,rm1,rm1,rayVector)
Vector(r0,r0,r10,rayPoint)
Vector(r0,r0,r1,planeNormal)
Vector(r0,r0,r5,planePoint)
Test(rayVector,rayPoint,planeNormal,planePoint)
 
Vector(r1,r1,r0,rayVector)
Vector(r1,r1,r0,rayPoint)
Vector(r0,r0,r1,planeNormal)
Vector(r5,r1,r0,planePoint)
Test(rayVector,rayPoint,planeNormal,planePoint)
RETURN</syntaxhighlight>
{{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]
<pre>
Ray vector: (0,-1,-1)
Ray point: (0,0,10)
Plane normal: (0,0,1)
Plane point: (0,0,5)
Intersection point: (0,-5,5)
 
Ray vector: (1,1,0)
Ray point: (1,1,0)
Plane normal: (0,0,1)
Plane point: (5,1,0)
There is no intersection
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Numerics.Generic_Real_Arrays;
with Ada.Text_IO;
 
procedure Intersection is
 
type Real is new Long_Float;
 
package Real_Arrays is
new Ada.Numerics.Generic_Real_Arrays (Real => Real);
use Real_Arrays;
 
package Real_IO is
new Ada.Text_IO.Float_IO (Num => Real);
 
subtype Vector_3D is Real_Vector (1 .. 3);
 
function Line_Plane_Intersection (Line_Vector : in Vector_3D;
Line_Point : in Vector_3D;
Plane_Normal : in Vector_3D;
Plane_Point : in Vector_3D)
return Vector_3D
is
Diff : constant Vector_3D := Line_Point - Plane_Point;
Denom : constant Real := Line_Vector * Plane_Normal;
begin
if Denom = 0.0 then
raise Constraint_Error with "Line does not intersect plane";
end if;
declare
Scale : constant Real :=
-Real'(Diff * Plane_Normal) / Denom;
Point : constant Vector_3D :=
Diff + Plane_Point + Scale * Line_Vector;
begin
return Point;
end;
end Line_Plane_Intersection;
 
procedure Put (V : in Vector_3D) is
use Ada.Text_IO, Real_IO;
begin
Put ("(");
Put (V (1)); Put (",");
Put (V (2)); Put (",");
Put (V (3)); Put (")");
end Put;
 
begin
Real_IO.Default_Exp := 0;
Real_IO.Default_Aft := 3;
 
Put (Line_Plane_Intersection (Line_Vector => (0.0, -1.0, -1.0),
Line_Point => (0.0, 0.0, 10.0),
Plane_Normal => (0.0, 0.0, 1.0),
Plane_Point => (0.0, 0.0, 5.0)));
end Intersection;</syntaxhighlight>
{{out}}
<pre>( 0.000,-5.000, 5.000)</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">⍝ Find the intersection of a line with a plane
⍝ 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
⍝ I belongs to the plan defined by point P and normal vector N. This means that any two points of the plane make a vector
⍝ normal to vector N. As I and P belong to the plane, the vector IP is normal to N.
⍝ This translates to: The scalar product IP.N = 0.
⍝ (P - I).N = 0 <=> (P - L - tV).N = 0
⍝ Using distributivity, then associativity, the following equations are established:
⍝ (P - L - tV).N = (P - L).N - (tV).N = (P - L).N - t(V.N) = 0
⍝ Which allows to resolve t: t = ((P - L).N) ÷ (V.N)
⍝ In APL, A.B is coded +/A x B
V ← 0 ¯1 ¯1
L ← 0 0 10
N ← 0 0 1
P ← 0 0 5
dot ← { +/ ⍺ × ⍵ }
t ← ((P - L) dot N) ÷ V dot N
I ← L + t × V
</syntaxhighlight>
{{out}}
<pre>
I
0 ¯5 5
</pre>
 
=={{header|Arturo}}==
{{trans|Nim}}
<syntaxhighlight lang="rebol">define :vector [x, y, z][]
 
addv: function [v1 :vector, v2 :vector]->
to :vector @[v1\x+v2\x, v1\y+v2\y, v1\z+v2\z]
 
subv: function [v1 :vector, v2 :vector]->
to :vector @[v1\x-v2\x, v1\y-v2\y, v1\z-v2\z]
 
mulv: function [v1 :vector, v2 :vector :floating][
if? is? :vector v2
-> return sum @[v1\x*v2\x v1\y*v2\y v1\z*v2\z]
else
-> return to :vector @[v1\x*v2, v1\y*v2, v1\z*v2]
]
 
intersect: function [lV, lP, pV, pP][
tdenom: mulv pV lV
if zero? tdenom -> return to :vector @[∞, ∞, ∞]
t: (mulv pV subv pP lP) / tdenom
return addv mulv lV t lP
]
 
coords: intersect to :vector @[0.0, neg 1.0, neg 1.0]
to :vector @[0.0, 0.0, 10.0]
to :vector @[0.0, 0.0, 1.0]
to :vector @[0.0, 0.0, 5.0]
 
print ["Intersection at:" coords]</syntaxhighlight>
 
{{out}}
 
<pre>Intersection at: [x:0.0 y:-5.0 z:5.0]</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">/*
; https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection#Algebraic_form
l = line vector
lo = point on the line
n = plane normal vector
Po = point on the plane
 
if (l . n) = 0 ; line and plane are parallel.
if (Po - lo) . n = 0 ; line is contained in the plane.
 
(P - Po) . n = 0 ; vector equation of plane.
P = lo + l * d ; vector equation of line.
((lo + l * d) - Po) . n = 0 ; Substitute line into plane equation.
(l . n) * d + (lo - Po) . n = 0 ; Expanding.
d = ((Po - lo) . n) / (l . n) ; solving for d.
P = lo + l * ((Po - lo) . n) / (l . n) ; solving P.
*/
 
intersectPoint(l, lo, n, Pn ){
if (Vector_dot(Vector_sub(Pn, lo), n) = 0) ; line is contained in the plane
return [1]
if (Vector_dot(l, n) = 0) ; line and plane are parallel
return [0]
diff := Vector_Sub(Pn, lo) ; (Po - lo)
prod1 := Vector_Dot(diff, n) ; ((Po - lo) . n)
prod2 := Vector_Dot(l, n) ; (l . n)
d := prod1 / prod2 ; d = ((Po - lo) . n) / (l . n)
return Vector_Add(lo, Vector_Mul(l, d)) ; P = lo + l * d
}
 
Vector_Add(v, w){
return [v.1+w.1, v.2+w.2, v.3+w.3]
}
Vector_Sub(v, w){
return [v.1-w.1, v.2-w.2, v.3-w.3]
}
Vector_Mul(v, s){
return [s*v.1, s*v.2, s*v.3]
}
Vector_Dot(v, w){
return v.1*w.1 + v.2*w.2 + v.3*w.3
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">; task
l1 := [0, -1, -1]
lo1 := [0, 0, 10]
n1 := [0, 0, 1]
Po1 := [0, 0, 5]
 
; line on plane
l2 := [1, 1, 0]
lo2 := [1, 1, 0]
n2 := [0, 0, 1]
Po2 := [5, 1, 0]
 
; line parallel to plane
l3 := [1, 1, 0]
lo3 := [1, 1, 1]
n3 := [0, 0, 1]
Po3 := [5, 1, 0]
 
output := ""
loop 3
{
result := ""
ip := intersectPoint(l%A_Index%, lo%A_Index%, n%A_Index%, Po%A_Index%)
for i, v in ip
result .= v ", "
output .= Trim(result, ", ") "`n"
}
MsgBox % output
return</syntaxhighlight>
{{out}}
<pre>0.000000, -5.000000, 5.000000
1 ; line on plane
0 ; line parallel to plane
</pre>
 
=={{header|C}}==
Straightforward application of the intersection formula, prints usage on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
#include<stdio.h>
 
Line 74 ⟶ 408:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and output:
<pre>
Line 80 ⟶ 414:
Intersection point is (0.000000,-5.000000,5.000000)
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
 
namespace FindIntersection {
class Vector3D {
private double x, y, z;
 
public Vector3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
 
public static Vector3D operator +(Vector3D lhs, Vector3D rhs) {
return new Vector3D(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
 
public static Vector3D operator -(Vector3D lhs, Vector3D rhs) {
return new Vector3D(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
}
 
public static Vector3D operator *(Vector3D lhs, double rhs) {
return new Vector3D(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
}
 
public double Dot(Vector3D rhs) {
return x * rhs.x + y * rhs.y + z * rhs.z;
}
 
public override string ToString() {
return string.Format("({0:F}, {1:F}, {2:F})", x, y, z);
}
}
 
class Program {
static Vector3D IntersectPoint(Vector3D rayVector, Vector3D rayPoint, Vector3D planeNormal, Vector3D planePoint) {
var diff = rayPoint - planePoint;
var prod1 = diff.Dot(planeNormal);
var prod2 = rayVector.Dot(planeNormal);
var prod3 = prod1 / prod2;
return rayPoint - rayVector * prod3;
}
 
static void Main(string[] args) {
var rv = new Vector3D(0.0, -1.0, -1.0);
var rp = new Vector3D(0.0, 0.0, 10.0);
var pn = new Vector3D(0.0, 0.0, 1.0);
var pp = new Vector3D(0.0, 0.0, 5.0);
var ip = IntersectPoint(rv, rp, pn, pp);
Console.WriteLine("The ray intersects the plane at {0}", ip);
}
}
}</syntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
 
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <sstream>
 
Line 136 ⟶ 526:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0, -5, 5)</pre>
 
=={{header|C#|C sharp}}==
<lang csharp>using System;
 
namespace FindIntersection {
class Vector3D {
private double x, y, z;
 
public Vector3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
 
public static Vector3D operator +(Vector3D lhs, Vector3D rhs) {
return new Vector3D(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
 
public static Vector3D operator -(Vector3D lhs, Vector3D rhs) {
return new Vector3D(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
}
 
public static Vector3D operator *(Vector3D lhs, double rhs) {
return new Vector3D(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
}
 
public double Dot(Vector3D rhs) {
return x * rhs.x + y * rhs.y + z * rhs.z;
}
 
public override string ToString() {
return string.Format("({0:F}, {1:F}, {2:F})", x, y, z);
}
}
 
class Program {
static Vector3D IntersectPoint(Vector3D rayVector, Vector3D rayPoint, Vector3D planeNormal, Vector3D planePoint) {
var diff = rayPoint - planePoint;
var prod1 = diff.Dot(planeNormal);
var prod2 = rayVector.Dot(planeNormal);
var prod3 = prod1 / prod2;
return rayPoint - rayVector * prod3;
}
 
static void Main(string[] args) {
var rv = new Vector3D(0.0, -1.0, -1.0);
var rp = new Vector3D(0.0, 0.0, 10.0);
var pn = new Vector3D(0.0, 0.0, 1.0);
var pp = new Vector3D(0.0, 0.0, 5.0);
var ip = IntersectPoint(rv, rp, pn, pp);
Console.WriteLine("The ray intersects the plane at {0}", ip);
}
}
}</lang>
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
struct Vector3D {
Line 253 ⟶ 587:
auto ip = intersectPoint(rv, rp, pn, pp);
writeln("The ray intersects the plane at ", ip);
}</langsyntaxhighlight>
 
{{out}}
<pre>The ray intersects the plane at (0.000000,-5.000000,5.000000)</pre>
 
=={{header|F#|F sharpEasyLang}}==
{{trans|Lua}}
<syntaxhighlight lang=easylang>
proc minus . l[] r[] res[] .
len res[] 3
for i to 3
res[i] = l[i] - r[i]
.
.
func dot l[] r[] .
for i to 3
res += l[i] * r[i]
.
return res
.
proc scale f . l[] .
for i to 3
l[i] = l[i] * f
.
.
proc inter_point rv[] rp[] pn[] pp[] . res[] .
minus rp[] pp[] dif[]
prd1 = dot dif[] pn[]
prd2 = dot rv[] pn[]
scale (prd1 / prd2) rv[]
minus rp[] rv[] res[]
.
rv[] = [ 0.0 -1.0 -1.0 ]
rp[] = [ 0.0 0.0 10.0 ]
pn[] = [ 0.0 0.0 1.0 ]
pp[] = [ 0.0 0.0 5.0 ]
inter_point rv[] rp[] pn[] pp[] res[]
print res[]
</syntaxhighlight>
 
=={{header|Evaldraw}}==
{{trans|C}}
 
Makes use of the intersectionPoint function to intersect 9 lines with 1 moving plane in a realtime demo.
[[File:Evaldraw line vs plane.png|thumb|alt=Grid of 3x3 3d points intersecting a 3D plane|Shows 3x3 grid of lines intersecting a plane. Gridlines drawn between intersection points. Intersection "time" value projected from 3d intersection point to 2d screen rendering.]]
<syntaxhighlight lang="c">
struct vec{x,y,z;};
enum{GRIDRES=3} // Keep a NxN grid of intersection results.
static vec intersections[GRIDRES][GRIDRES];
static vec ipos = {0,5,-15};
static vec ileft = {-1,0,0};
static vec iup = {0,-1,0};
static vec ifor = {0,0,1};
()
{
cls(0); clz(1e32);
setcam( ipos.x, ipos.y, ipos.z,
ileft.x, ileft.y, ileft.z, // flip right basis to left
iup.x, iup.y, iup.z, // flip down basis to up
ifor.x, ifor.y, ifor.z);
t=klock(0);
vec planePoint = {0,5,0}; // Plane Position
vec pN = {cos(t),1,sin(t)}; // PlaneNormal, un-normalized
normalize(pN);
 
for(x=0; x<GRIDRES; x++)
for(z=0; z<GRIDRES; z++)
{
scale = 4.5; halfgrid = scale*(GRIDRES-1)/2;
vec lineVector = {0,1,0}; // Direction of line
vec linePoint ={-halfgrid+scale*x, 5, -halfgrid+scale*z};
if (vecdot( lineVector, pN ) == 0 )
{
moveto(0,0); printf("Line and Plane dont intersect.");
} else {
vec isect;
isect_time = intersectionPoint(lineVector, linePoint, pN, planePoint, isect);
intersections[x][z] = isect; // Store for drawing grid
//setcol(255,255,0); drawsph(isect.x, isect.y, isect.z, .1);
setcol(255,0,0); line(linePoint, isect);
unproject(isect);
setfont(8,12); setcol(255,255,255); printf("t=%2.1f", isect_time);
}
}
// drawgridPlane
setcol(255,0,255);
for(i=0; i<GRIDRES; i++)
for(j=0; j<GRIDRES; j++) {
vec p00 = intersections[i][j];
vec p10 = intersections[(i+1)%GRIDRES][j];
vec p01 = intersections[i][(j+1)%GRIDRES]; // oob wraps to 0 anyhow
line(p00,p10);
line(p00,p01);
}
setcol(192,192,192); moveto(0,0); printf("Line vs Plane intersection");
}
intersectionPoint(vec lineVector, vec linePoint, vec planeNormal, vec planePoint, vec isect){
vec diff; vecsub(diff,linePoint,planePoint);
vec pd; vecadd(pd, diff,planePoint);
t = -vecdot(diff,planeNormal) / vecdot(lineVector,planeNormal);
vec scaledVec; vecscalar(scaledVec, lineVector, t);
vecadd(isect, pd, scaledVec);
return t;
}
line(vec a, vec b) { moveto(a.x,a.y,a.z); lineto(b.x,b.y,b.z); }
// -------------------------------------- VECTOR MATH
vecScalar( vec out, vec a, s ) {
out.x = a.x * s;
out.y = a.y * s;
out.z = a.z * s;
}
vecAdd( vec out, vec a, vec b) {
out.x = a.x + b.x;
out.y = a.y + b.y;
out.z = a.z + b.z;
}
vecAdd( vec out, vec b) {
out.x += b.x;
out.y += b.y;
out.z += b.z;
}
vecSub( vec out, vec a, vec b) {
out.x = a.x - b.x;
out.y = a.y - b.y;
out.z = a.z - b.z;
}
vecCross( vec out, vec a, vec b) {
out.x = a.y*b.z - a.z*b.y;
out.y = a.z*b.x - a.x*b.z;
out.z = a.x*b.y - a.y*b.x;
}
vecDot( vec a, vec b) {
return a.x*b.x + a.y*b.y + a.z*b.z;
}
length( vec v ) {
return sqrt( vecdot(v,v) );
}
normalize( vec v ) {
len = length(v);
if ( len ) { v.x /= len; v.y /= len; v.z /= len; }
}
unproject(vec pt) { // unproject a 3D screenpoint
vec from_eye; vecsub(from_eye, pt, ipos);
nx = vecdot(from_eye, ileft);
ny = vecdot(from_eye, iup);
nz = vecdot(from_eye, ifor);
if (nz <= 0.5) return; // behind eye
f = xres/2/nz; // 90 degree projection
moveto(nx*f + xres/2, ny*f + yres/2 );
}</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
 
type Vector(x : double, y : double, z : double) =
Line 292 ⟶ 776:
Console.WriteLine("The ray intersects the plane at {0}", ip)
 
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
 
=={{header|Factor}}==
{{trans|11l}}
<syntaxhighlight lang="factor">USING: io locals math.vectors prettyprint ;
 
:: intersection-point ( rdir rpt pnorm ppt -- loc )
rpt rdir pnorm rpt ppt v- v. v*n rdir pnorm v. v/n v- ;
 
"The ray intersects the plane at " write
{ 0 -1 -1 } { 0 0 10 } { 0 0 1 } { 0 0 5 } intersection-point .</syntaxhighlight>
{{out}}
<pre>
The ray intersects the plane at { 0 -5 5 }
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 11-07-2018
' compile with: fbc -s console
 
Line 363 ⟶ 861:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>line intersects the plane at (0, -5, 5)</pre>
Line 369 ⟶ 867:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 410 ⟶ 908:
ip := intersectPoint(rv, rp, pn, pp)
fmt.Println("The ray intersects the plane at", ip)
}</langsyntaxhighlight>
 
{{out}}
Line 416 ⟶ 914:
The ray intersects the plane at (0, -5, 5)
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class LinePlaneIntersection {
private static class Vector3D {
private double x, y, z
 
Vector3D(double x, double y, double z) {
this.x = x
this.y = y
this.z = z
}
 
Vector3D plus(Vector3D v) {
return new Vector3D(x + v.x, y + v.y, z + v.z)
}
 
Vector3D minus(Vector3D v) {
return new Vector3D(x - v.x, y - v.y, z - v.z)
}
 
Vector3D multiply(double s) {
return new Vector3D(s * x, s * y, s * z)
}
 
double dot(Vector3D v) {
return x * v.x + y * v.y + z * v.z
}
 
@Override
String toString() {
return "($x, $y, $z)"
}
}
 
private static Vector3D intersectPoint(Vector3D rayVector, Vector3D rayPoint, Vector3D planeNormal, Vector3D planePoint) {
Vector3D diff = rayPoint - planePoint
double prod1 = diff.dot(planeNormal)
double prod2 = rayVector.dot(planeNormal)
double prod3 = prod1 / prod2
return rayPoint - rayVector * prod3
}
 
static void main(String[] args) {
Vector3D rv = new Vector3D(0.0, -1.0, -1.0)
Vector3D rp = new Vector3D(0.0, 0.0, 10.0)
Vector3D pn = new Vector3D(0.0, 0.0, 1.0)
Vector3D pp = new Vector3D(0.0, 0.0, 5.0)
Vector3D ip = intersectPoint(rv, rp, pn, pp)
println("The ray intersects the plane at $ip")
}
}</syntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.0, -5.0, 5.0)</pre>
 
=={{header|Haskell}}==
{{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].
<langsyntaxhighlight Haskelllang="haskell">import Control.Applicative (liftA2)
import Text.Printf (printf)
 
Line 462 ⟶ 1,014:
rp = V3 0 0 10
pn = V3 0 0 1
pp = V3 0 0 5</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.0, -5.0, 5.0)</pre>
Line 468 ⟶ 1,020:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">mp=: +/ .* NB. matrix product
p=: mp&{: %~ -~&{. mp {:@] NB. solve
intersectLinePlane=: [ +/@:* 1 , p NB. substitute</langsyntaxhighlight>
'''Example Usage:'''
<langsyntaxhighlight lang="j"> Line=: 0 0 10 ,: 0 _1 _1 NB. Point, Ray
Plane=: 0 0 5 ,: 0 0 1 NB. Point, Normal
Line intersectLinePlane Plane
0 _5 5</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class LinePlaneIntersection {
private static class Vector3D {
private double x, y, z;
Line 527 ⟶ 1,079:
System.out.println("The ray intersects the plane at " + ip);
}
}</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.000000, -5.000000, 5.000000)</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
In the following, a 3d vector is represented by a JSON array: [x, y, z]
<syntaxhighlight lang="jq"># add as many as you please
def addVector:
transpose | add;
 
# . - y
def minusVector(y):
[.[0] - y[0], .[1] - y[1], .[2] - y[2]];
 
# scalar multiplication: . * s
def multVector(s):
map(. * s);
 
def dot(y):
.[0] * y[0] + .[1] * y[1] + .[2] * y[2];
 
def intersectPoint($rayVector; $rayPoint; $planeNormal; $planePoint):
($rayPoint | minusVector($planePoint)) as $diff
| ($diff|dot($planeNormal)) as $prod1
| ($rayVector|dot($planeNormal)) as $prod2
| $rayPoint | minusVector($rayVector | multVector(($prod1 / $prod2) )) ;
 
def rv : [0, -1, -1];
def rp : [0, 0, 10];
def pn : [0, 0, 1];
def pp : [0, 0, 5];
 
"The ray intersects the plane at:",
intersectPoint(rv; rp; pn; pp)</syntaxhighlight>
{{out}}
<pre>
The ray intersects the plane at:
[0,-5,5]
</pre>
 
 
=={{header|Julia}}==
Line 535 ⟶ 1,128:
{{trans|Python}}
 
<langsyntaxhighlight lang="julia">function lineplanecollision(planenorm::Vector, planepnt::Vector, raydir::Vector, raypnt::Vector)
ndotu = dot(planenorm, raydir)
if ndotu ≈ 0 error("no intersection or line is within plane") end
Line 554 ⟶ 1,147:
 
ψ = lineplanecollision(planenorm, planepnt, raydir, raypnt)
println("Intersection at $ψ")</langsyntaxhighlight>
 
{{out}}
Line 560 ⟶ 1,153:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
class Vector3D(val x: Double, val y: Double, val z: Double) {
Line 595 ⟶ 1,188:
val ip = intersectPoint(rv, rp, pn, pp)
println("The ray intersects the plane at $ip")
}</langsyntaxhighlight>
 
{{out}}
Line 603 ⟶ 1,196:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function make(xval, yval, zval)
return {x=xval, y=yval, z=zval}
end
Line 640 ⟶ 1,233:
pp = make(0.0, 0.0, 5.0)
ip = intersectPoint(rv, rp, pn, pp)
print("The ray intersects the plane at " .. tostr(ip))</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0, -5, 5)</pre>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">geom3d:-plane(P, [geom3d:-point(p1,0,0,5), [0,0,1]]);
geom3d:-line(L, [geom3d:-point(p2,0,0,10), [0,-1,-1]]);
geom3d:-intersection(px,L,P);
geom3d:-detail(px);</langsyntaxhighlight>
{{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}}==
<syntaxhighlight lang="mathematica">RegionIntersection[InfiniteLine[{0, 0, 10}, {0, -1, -1}], InfinitePlane[{0, 0, 5}, {{0, 1, 0}, {1, 0, 0}}]]</syntaxhighlight>
{{out}}
<pre>Point[{0, -5, 5}]</pre>
 
 
=={{header|MATLAB}}==
{{trans|Kotlin}}
<langsyntaxhighlight MATLABlang="matlab">function point = intersectPoint(rayVector, rayPoint, planeNormal, planePoint)
 
pdiff = rayPoint - planePoint;
Line 661 ⟶ 1,260:
prod3 = prod1 / prod2;
 
point = rayPoint - rayVector * prod3;</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight MATLABlang="matlab">>> intersectPoint([0 -1 -1], [0 0 10], [0 0 1], [0 0 5])
 
ans =
 
0 -5 5
</syntaxhighlight>
</lang>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE LinePlane;
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 732 ⟶ 1,331:
 
ReadChar;
END LinePlane.</langsyntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
type Vector = tuple[x, y, z: float]
 
 
func `+`(v1, v2: Vector): Vector =
## Add two vectors.
(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z)
 
func `-`(v1, v2: Vector): Vector =
## Subtract a vector to another one.
(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
 
func `*`(v1, v2: Vector): float =
## Compute the dot product of two vectors.
v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
 
func `*`(v: Vector; k: float): Vector =
## Multiply a vector by a scalar.
(v.x * k, v.y * k, v.z * k)
 
 
func intersection(lineVector, linePoint, planeVector, planePoint: Vector): Vector =
## Return the coordinates of the intersection of two vectors.
 
let tnum = planeVector * (planePoint - linePoint)
let tdenom = planeVector * lineVector
if tdenom == 0: return (Inf, Inf, Inf) # No intersection.
let t = tnum / tdenom
result = lineVector * t + linePoint
 
let coords = intersection(lineVector = (0.0, -1.0, -1.0),
linePoint = (0.0, 0.0, 10.0),
planeVector = (0.0, 0.0, 1.0),
planePoint = (0.0, 0.0, 5.0))
echo "Intersection at ", coords</syntaxhighlight>
 
{{out}}
<pre>Intersection at (x: 0.0, y: -5.0, z: 5.0)</pre>
 
=={{header|Perl}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="perl">package Line; sub new { my ($c, $a) = @_; my $self = { P0 => $a->{P0}, u => $a->{u} } } # point / ray
package Plane; sub new { my ($c, $a) = @_; my $self = { V0 => $a->{V0}, n => $a->{n} } } # point / normal
 
Line 763 ⟶ 1,402:
my $P = Plane->new({ V0=>[0,0,5 ], n=>[0, 0, 1]});
print 'Intersection at point: ', join(' ', line_plane_intersection($L, $P)) . "\n";
</syntaxhighlight>
</lang>
{{out}}
<pre>Intersection at point: 0 -5 5</pre>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{works with|Rakudo|2016.11}}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
{{trans|Python}}
<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>
<span style="color: #008080;">function</span> <span style="color: #000000;">intersection_point</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">line_vector</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line_point</span><span style="color: #0000FF;">,</span><span style="color: #000000;">plane_normal</span><span style="color: #0000FF;">,</span><span style="color: #000000;">plane_point</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dot</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line_vector</span><span style="color: #0000FF;">,</span><span style="color: #000000;">plane_normal</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"no intersection"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">diff</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line_point</span><span style="color: #0000FF;">,</span><span style="color: #000000;">plane_point</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">,</span><span style="color: #000000;">plane_point</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">dot</span><span style="color: #0000FF;">(</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">,</span><span style="color: #000000;">plane_normal</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line_vector</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">intersection_point</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;">10</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;">5</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">intersection_point</span><span style="color: #0000FF;">({</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</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;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</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;">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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
{0,-5,5}
{0.6,2.4,4.2}
"no intersection"
"no intersection"
</pre>
 
=={{header|Picat}}==
<lang perl6>class Line {
{{trans|Java}}
has $.P0; # point
{{works with|Picat}}
has $.u⃗; # ray
<syntaxhighlight lang="picat">
}
plus(U, V) = {U[1] + V[1], U[2] + V[2], U[3] + V[3]}.
class Plane {
has $.V0; # point
has $.n⃗; # normal
}
 
minus(U, V) = {U[1] - V[1], U[2] - V[2], U[3] - V[3]}.
sub infix:<∙> ( @a, @b where +@a == +@b ) { [+] @a «*» @b } # dot product
 
times(U, S) = {U[1] * S, U[2] * S, U[3] * S}.
sub line-plane-intersection ($𝑳, $𝑷) {
my $cos = $𝑷.n⃗ ∙ $𝑳.u⃗; # cosine between normal & ray
return 'Vectors are orthogonal; no intersection or line within plane'
if $cos == 0;
my $𝑊 = $𝑳.P0 «-» $𝑷.V0; # difference between P0 and V0
my $S𝐼 = -($𝑷.n⃗ ∙ $𝑊) / $cos; # line segment where it intersects the plane
$𝑊 «+» $S𝐼 «*» $𝑳.u⃗ «+» $𝑷.V0; # point where line intersects the plane
}
 
dot(U, V) = U[1] * V[1] + U[2] * V[2] + U[3] * V[3].
say 'Intersection at point: ', line-plane-intersection(
 
Line.new( :P0(0,0,10), :u⃗(0,-1,-1) ),
intersect_point(RayVector, RayPoint, PlaneNormal, PlanePoint) = IntersectPoint =>
Plane.new( :V0(0,0, 5), :n⃗(0, 0, 1) )
Diff = minus(RayPoint, PlanePoint),
);</lang>
Prod1 = dot(Diff, PlaneNormal),
Prod2 = dot(RayVector, PlaneNormal),
Prod3 = Prod1 / Prod2,
IntersectPoint = minus(RayPoint, times(RayVector, Prod3)).
 
main =>
RayVector = {0.0, -1.0, -1.0},
RayPoint = {0.0, 0.0, 10.0},
PlaneNormal = {0.0, 0.0, 1.0},
PlanePoint = {0.0, 0.0, 5.0},
IntersectPoint = intersect_point(RayVector, RayPoint, PlaneNormal, PlanePoint),
printf("The ray intersects the plane at (%f, %f, %f)\n",
IntersectPoint[1],
IntersectPoint[2],
IntersectPoint[3]
).
</syntaxhighlight>
{{out}}
<pre>
<pre>Intersection at point: (0 -5 5)</pre>
The ray intersects the plane at (0.000000, -5.000000, 5.000000)
</pre>
 
=={{header|Phix}}==
<lang Phix>function dot(sequence a, b) return sum(sq_mul(a,b)) end function
 
=={{header|Prolog}}==
function intersection_point(sequence line_vector,line_point,plane_normal,plane_point)
{{trans|Picat}}
atom a = dot(line_vector,plane_normal)
{{works with|GNU Prolog}}
if a=0 then return "no intersection" end if
{{works with|SWI Prolog}}
sequence diff = sq_sub(line_point,plane_point)
<syntaxhighlight lang="prolog">
return sq_add(sq_add(diff,plane_point),sq_mul(-dot(diff,plane_normal)/a,line_vector))
:- initialization(main).
end function
 
vector_plus(U, V, W) :-
U = p(X1, Y1, Z1),
V = p(X2, Y2, Z2),
X3 is X1 + X2,
Y3 is Y1 + Y2,
Z3 is Z1 + Z2,
W = p(X3, Y3, Z3).
 
vector_minus(U, V, W) :-
U = p(X1, Y1, Z1),
V = p(X2, Y2, Z2),
X3 is X1 - X2,
Y3 is Y1 - Y2,
Z3 is Z1 - Z2,
W = p(X3, Y3, Z3).
 
vector_times(U, S, V) :-
U = p(X1, Y1, Z1),
X2 is X1 * S,
Y2 is Y1 * S,
Z2 is Z1 * S,
V = p(X2, Y2, Z2).
 
vector_dot(U, V, S) :-
U = p(X1, Y1, Z1),
V = p(X2, Y2, Z2),
S is X1 * X2 + Y1 * Y2 + Z1 * Z2.
 
intersect_point(RayVector, RayPoint, PlaneNormal, PlanePoint, IntersectPoint) :-
vector_minus(RayPoint, PlanePoint, Diff),
vector_dot(Diff, PlaneNormal, Prod1),
vector_dot(RayVector, PlaneNormal, Prod2),
Prod3 is Prod1 / Prod2,
vector_times(RayVector, Prod3, Times),
vector_minus(RayPoint, Times, IntersectPoint).
 
main :-
?intersection_point({0,-1,-1},{0,0,10},{0,0,1},{0,0,5})
RayVector = p(0.0, -1.0, -1.0),
?intersection_point({3,2,1},{0,2,4},{1,2,3},{3,3,3})
RayPoint = p(0.0, 0.0, 10.0),
?intersection_point({1,1,0},{0,0,1},{0,0,3},{0,0,0}) -- (parallel to plane)
PlaneNormal = p(0.0, 0.0, 1.0),
?intersection_point({1,1,0},{1,1,0},{0,0,3},{0,0,0}) -- (line within plane)</lang>
PlanePoint = p(0.0, 0.0, 5.0),
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>
 
{0,-5,5}
The ray intersects the plane at (0.000000, -5.000000, 5.000000)
{0.6,2.4,4.2}
"no intersection"
"no intersection"
</pre>
 
Line 823 ⟶ 1,528:
Based on the approach at geomalgorithms.com<ref>http://geomalgorithms.com/a05-_intersect-1.html</ref>
 
<langsyntaxhighlight lang="python">#!/bin/python
from __future__ import print_function
import numpy as np
Line 849 ⟶ 1,554:
 
Psi = LinePlaneCollision(planeNormal, planePoint, rayDirection, rayPoint)
print ("intersection at", Psi)</langsyntaxhighlight>
 
{{out}}
<pre>intersection at [ 0 -5 5]</pre>
 
=={{header|R}}==
{{trans|MATLAB}}
<syntaxhighlight lang="r">intersect_point <- function(ray_vec, ray_point, plane_normal, plane_point) {
 
pdiff <- ray_point - plane_point
prod1 <- pdiff %*% plane_normal
prod2 <- ray_vec %*% plane_normal
prod3 <- prod1 / prod2
point <- ray_point - ray_vec * as.numeric(prod3)
 
return(point)
}</syntaxhighlight>
{{out}}
<syntaxhighlight lang="r">>>intersect_point(c(0, -1, -1), c(0, 0, 10), c(0, 0, 1), c(0, 0, 5))
[1] 0 -5 5</syntaxhighlight>
 
=={{header|Racket}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="racket">#lang racket
;; {{trans|Sidef}}
;; vectors are represented by lists
Line 879 ⟶ 1,600:
(line-plane-intersection (Line '(0 0 10) '(0 -1 -1))
(Plane '(0 0 5) '(0 0 1)))
'(0 -5 5)))</langsyntaxhighlight>
 
{{out}}
No output -- all tests passed!
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2016.11}}
{{trans|Python}}
 
<syntaxhighlight lang="raku" line>class Line {
has $.P0; # point
has $.u⃗; # ray
}
class Plane {
has $.V0; # point
has $.n⃗; # normal
}
 
sub infix:<∙> ( @a, @b where +@a == +@b ) { [+] @a «*» @b } # dot product
 
sub line-plane-intersection ($𝑳, $𝑷) {
my $cos = $𝑷.n⃗ ∙ $𝑳.u⃗; # cosine between normal & ray
return 'Vectors are orthogonal; no intersection or line within plane'
if $cos == 0;
my $𝑊 = $𝑳.P0 «-» $𝑷.V0; # difference between P0 and V0
my $S𝐼 = -($𝑷.n⃗ ∙ $𝑊) / $cos; # line segment where it intersects the plane
$𝑊 «+» $S𝐼 «*» $𝑳.u⃗ «+» $𝑷.V0; # point where line intersects the plane
}
 
say 'Intersection at point: ', line-plane-intersection(
Line.new( :P0(0,0,10), :u⃗(0,-1,-1) ),
Plane.new( :V0(0,0, 5), :n⃗(0, 0, 1) )
);</syntaxhighlight>
{{out}}
<pre>Intersection at point: (0 -5 5)</pre>
 
===With a geometric algebra library===
 
See task [[geometric algebra]]
 
<syntaxhighlight lang=raku>use Clifford:ver<6.2.1>;
 
# We pick a (non-degenerate) projective basis and
# we define the dual and meet operators.
my $I = [∧] my ($i, $j, $k, $l) = @e;
sub prefix:<∗>($M) { $M/$I }
sub infix:<∨>($A, $B) { ∗((∗$B)∧(∗$A)) }
 
my $direction = -$j - $k;
 
# Homogeneous coordinates of (X, Y, Z) are (X, Y, Z, 1)
my $point = 10*$k + $l;
 
# A projective line is a bivector
my $line = $direction ∧ $point;
 
# A projective plane is a trivector
my $plane = (5*$k + $l) ∧ ($k*-$i∧$j∧$k);
 
# The intersection is the meet
my $m = $line ∨ $plane;
 
# Affine coordinates of (X, Y, Z, W) are (X/W, Y/W, Z/W)
say $m/($m·$l) X· ($i, $j, $k);</syntaxhighlight>
{{out}}
<pre>(0 -5 5)</pre>
 
=={{header|REXX}}==
===version 1===
This program does NOT handle the case when the line is parallel to or within the plane.
<langsyntaxhighlight lang="rexx">/* REXX */
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 905 ⟶ 1,689:
z=a.3+t*v.3
 
Say 'Intersection: P('||x','y','z')'</langsyntaxhighlight>
 
{{out}}
Line 913 ⟶ 1,697:
===version 2===
handle the case that the line is parallel to the plane or lies within it.
<langsyntaxhighlight lang="rexx">/*REXX*/
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,035 ⟶ 1,819:
End
End
Return res </langsyntaxhighlight>
{{out}}
<pre>Plane definition: x+2*y+3*z=18
Line definition: x=3*t ; y=2+2*t ; z=4+t
Intersection: P(0.6,2.4,4.2)</pre>
 
=={{header|RPL}}==
≪ → rd rp pn pp
≪ rd rp pp - pn DOT * rd pn DOT /
≫ ≫ '<span style="color:blue">INTLP</span>' STO
 
[ 0 -1 -1 ] [ 0 0 0 ] [ 0 0 1 ] [ 0 0 5 ] <span style="color:blue">INTLP</span>
{{out}}
<pre>
1: [ 0 -5 -5 ]
</pre>
 
=={{header|Ruby}}==
{{trans|C#}}
<syntaxhighlight lang="ruby">require "matrix"
 
def intersectPoint(rayVector, rayPoint, planeNormal, planePoint)
diff = rayPoint - planePoint
prod1 = diff.dot planeNormal
prod2 = rayVector.dot planeNormal
prod3 = prod1 / prod2
return rayPoint - rayVector * prod3
end
 
def main
rv = Vector[0.0, -1.0, -1.0]
rp = Vector[0.0, 0.0, 10.0]
pn = Vector[0.0, 0.0, 1.0]
pp = Vector[0.0, 0.0, 5.0]
ip = intersectPoint(rv, rp, pn, pp)
puts "The ray intersects the plane at %s" % [ip]
end
 
main()</syntaxhighlight>
{{out}}
<pre>The ray intersects the plane at Vector[0.0, -5.0, 5.0]</pre>
 
=={{header|Rust}}==
{{trans|Kotlin}}
 
<langsyntaxhighlight Rustlang="rust">use std::ops::{Add, Div, Mul, Sub};
 
#[derive(Copy, Clone, Debug, PartialEq)]
Line 1,149 ⟶ 1,969:
println!("{:?}", intersect(rv, rp, pn, pp));
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object LinePLaneIntersection extends App {
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,177 ⟶ 1,997:
println(s"The ray intersects the plane at $ip")
}</langsyntaxhighlight>
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/oLTlNZk/0 ScalaFiddle (JavaScript)].
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby">struct Line {
P0, # point
u⃗, # ray
Line 1,204 ⟶ 2,025:
Line(P0: [0,0,10], u⃗: [0,-1,-1]),
Plane(V0: [0,0, 5], n⃗: [0, 0, 1]),
))</langsyntaxhighlight>
{{out}}
<pre>Intersection at point: [0, -5, 5]</pre>
Line 1,210 ⟶ 2,031:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Class Vector3D
Line 1,261 ⟶ 2,082:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-vector}}
<syntaxhighlight lang="wren">import "./vector" for Vector3
 
var intersectPoint = Fn.new { |rayVector, rayPoint, planeNormal, planePoint|
var diff = rayPoint - planePoint
var prod1 = diff.dot(planeNormal)
var prod2 = rayVector.dot(planeNormal)
var prod3 = prod1 / prod2
return rayPoint - rayVector*prod3
}
 
var rv = Vector3.new(0, -1, -1)
var rp = Vector3.new(0, 0, 10)
var pn = Vector3.new(0, 0, 1)
var pp = Vector3.new(0, 0, 5)
var ip = intersectPoint.call(rv, rp, pn, pp)
System.print("The ray intersects the plane at %(ip).")</syntaxhighlight>
 
{{out}}
<pre>
The ray intersects the plane at (0, -5, 5).
</pre>
 
=={{header|XPL0}}==
{{trans|Wren}}
<syntaxhighlight lang "XPL0">include xpllib;
 
func real IntersectPoint; real RayVector, RayPoint, PlaneNormal, PlanePoint;
real Diff(3), Prod1, Prod2, Prod3, Prod(3);
[VSub(Diff, RayPoint, PlanePoint);
Prod1:= VDot(Diff, PlaneNormal);
Prod2:= VDot(RayVector, PlaneNormal);
Prod3:= Prod1 / Prod2;
return VSub(Diff, RayPoint, VMul(Prod, RayVector, Prod3));
];
 
real RV, RP, PN, PP, IP;
[RV:= [0., -1., -1.];
RP:= [0., 0., 10.];
PN:= [0., 0., 1.];
PP:= [0., 0., 5.];
IP:= IntersectPoint(RV, RP, PN, PP);
Print("The ray intersects the plane at %1.1f, %1.1f, %1.1f\n", IP(0), IP(1), IP(2));
]</syntaxhighlight>
{{out}}
<pre>
The ray intersects the plane at 0.0, -5.0, 5.0
</pre>
 
=={{header|zkl}}==
{{trans|Perl 6Raku}}{{trans|Python}}
<langsyntaxhighlight lang="zkl">class Line { fcn init(pxyz, ray_xyz) { var pt=pxyz, ray=ray_xyz; } }
class Plane{ fcn init(pxyz, normal_xyz){ var pt=pxyz, normal=normal_xyz; } }
 
Line 1,280 ⟶ 2,152:
//w.zipWith('+,line.ray.apply('*,si)).zipWith('+,plane.pt); // or
w.zipWith('wrap(w,r,pt){ w + r*si + pt },line.ray,plane.pt);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("Intersection at point: ", linePlaneIntersection(
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) ))
);</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits