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

m
syntax highlighting fixup automation
(Added Arturo implementation)
m (syntax highlighting fixup automation)
Line 11:
=={{header|11l}}==
 
<langsyntaxhighlight lang="11l">F intersection_point(ray_direction, ray_point, plane_normal, plane_point)
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)))</langsyntaxhighlight>
 
{{out}}
Line 23:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE REALPTR="CARD"
Line 137:
Vector(r5,r1,r0,planePoint)
Test(rayVector,rayPoint,planeNormal,planePoint)
RETURN</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Generic_Real_Arrays;
with Ada.Text_IO;
 
Line 210:
Plane_Normal => (0.0, 0.0, 1.0),
Plane_Point => (0.0, 0.0, 5.0)));
end Intersection;</langsyntaxhighlight>
{{out}}
<pre>( 0.000,-5.000, 5.000)</pre>
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="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
Line 233:
t ← ((P - L) dot N) ÷ V dot N
I ← L + t × V
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 242:
=={{header|Arturo}}==
{{trans|Nim}}
<langsyntaxhighlight lang="rebol">define :vector [x, y, z][]
 
addv: function [v1 :vector, v2 :vector]->
Line 269:
to :vector @[0.0, 0.0, 5.0]
 
print ["Intersection at:" coords]</langsyntaxhighlight>
 
{{out}}
Line 276:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">/*
<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
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">; task
l1 := [0, -1, -1]
lo1 := [0, 0, 10]
Line 346:
}
MsgBox % output
return</langsyntaxhighlight>
{{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">
<lang C>
#include<stdio.h>
 
Line 408:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and output:
<pre>
Line 416:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace FindIntersection {
Line 467:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
Line 473:
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <sstream>
 
Line 526:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0, -5, 5)</pre>
Line 532:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
struct Vector3D {
Line 587:
auto ip = intersectPoint(rv, rp, pn, pp);
writeln("The ray intersects the plane at ", ip);
}</langsyntaxhighlight>
 
{{out}}
Line 594:
=={{header|F Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
 
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</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
Line 632:
=={{header|Factor}}==
{{trans|11l}}
<langsyntaxhighlight lang="factor">USING: io locals math.vectors prettyprint ;
 
:: 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 .</langsyntaxhighlight>
{{out}}
<pre>
Line 645:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 11-07-2018
' compile with: fbc -s console
 
Line 711:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>line intersects the plane at (0, -5, 5)</pre>
Line 717:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 758:
ip := intersectPoint(rv, rp, pn, pp)
fmt.Println("The ray intersects the plane at", ip)
}</langsyntaxhighlight>
 
{{out}}
Line 767:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class LinePlaneIntersection {
private static class Vector3D {
private double x, y, z
Line 815:
println("The ray intersects the plane at $ip")
}
}</langsyntaxhighlight>
{{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].
<langsyntaxhighlight Haskelllang="haskell">import Control.Applicative (liftA2)
import Text.Printf (printf)
 
Line 864:
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 870:
=={{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 929:
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>
Line 938:
 
In the following, a 3d vector is represented by a JSON array: [x, y, z]
<langsyntaxhighlight lang="jq"># add as many as you please
def addVector:
transpose | add;
Line 966:
 
"The ray intersects the plane at:",
intersectPoint(rv; rp; pn; pp)</langsyntaxhighlight>
{{out}}
<pre>
Line 978:
{{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 997:
 
ψ = lineplanecollision(planenorm, planepnt, raydir, raypnt)
println("Intersection at $ψ")</langsyntaxhighlight>
 
{{out}}
Line 1,003:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
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")
}</langsyntaxhighlight>
 
{{out}}
Line 1,046:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function make(xval, yval, zval)
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))</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}}==
<langsyntaxhighlight Mathematicalang="mathematica">RegionIntersection[InfiniteLine[{0, 0, 10}, {0, -1, -1}], InfinitePlane[{0, 0, 5}, {{0, 1, 0}, {1, 0, 0}}]]</langsyntaxhighlight>
{{out}}
<pre>Point[{0, -5, 5}]</pre>
Line 1,103:
=={{header|MATLAB}}==
{{trans|Kotlin}}
<langsyntaxhighlight MATLABlang="matlab">function point = intersectPoint(rayVector, rayPoint, planeNormal, planePoint)
 
pdiff = rayPoint - planePoint;
Line 1,110:
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 1,181:
 
ReadChar;
END LinePlane.</langsyntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
<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</langsyntaxhighlight>
 
{{out}}
Line 1,225:
=={{header|Perl}}==
{{trans|Raku}}
<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 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>
</lang>
{{out}}
<pre>Intersection at point: 0 -5 5</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,284:
{{trans|Java}}
{{works with|Picat}}
<syntaxhighlight lang="picat">
<lang Picat>
plus(U, V) = {U[1] + V[1], U[2] + V[2], U[3] + V[3]}.
 
Line 1,311:
IntersectPoint[3]
).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,322:
{{works with|GNU Prolog}}
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">
<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>
</lang>
{{out}}
<pre>
Line 1,378:
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 1,404:
 
Psi = LinePlaneCollision(planeNormal, planePoint, rayDirection, rayPoint)
print ("intersection at", Psi)</langsyntaxhighlight>
 
{{out}}
Line 1,411:
=={{header|R}}==
{{trans|MATLAB}}
<langsyntaxhighlight Rlang="r">intersect_point <- function(ray_vec, ray_point, plane_normal, plane_point) {
 
pdiff <- ray_point - plane_point
Line 1,420:
 
return(point)
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight Rlang="r">>>intersect_point(c(0, -1, -1), c(0, 0, 10), c(0, 0, 1), c(0, 0, 5))
[1] 0 -5 5</langsyntaxhighlight>
 
=={{header|Racket}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="racket">#lang racket
;; {{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)))</langsyntaxhighlight>
 
{{out}}
Line 1,460:
{{trans|Python}}
 
<syntaxhighlight lang="raku" perl6line>class Line {
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) )
);</langsyntaxhighlight>
{{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.
<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 1,508:
z=a.3+t*v.3
 
Say 'Intersection: P('||x','y','z')'</langsyntaxhighlight>
 
{{out}}
Line 1,516:
===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,638:
End
End
Return res </langsyntaxhighlight>
{{out}}
<pre>Plane definition: x+2*y+3*z=18
Line 1,646:
=={{header|Ruby}}==
{{trans|C#}}
<langsyntaxhighlight lang="ruby">require "matrix"
 
def intersectPoint(rayVector, rayPoint, planeNormal, planePoint)
Line 1,665:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at Vector[0.0, -5.0, 5.0]</pre>
Line 1,672:
{{trans|Kotlin}}
 
<langsyntaxhighlight Rustlang="rust">use std::ops::{Add, Div, Mul, Sub};
 
#[derive(Copy, Clone, Debug, PartialEq)]
Line 1,777:
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,805:
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|Raku}}
<langsyntaxhighlight lang="ruby">struct Line {
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]),
))</langsyntaxhighlight>
{{out}}
<pre>Intersection at point: [0, -5, 5]</pre>
Line 1,839:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Class Vector3D
Line 1,890:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>The ray intersects the plane at (0.00, -5.00, 5.00)</pre>
Line 1,896:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">class Vector3D {
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).")</langsyntaxhighlight>
 
{{out}}
Line 1,940:
=={{header|zkl}}==
{{trans|Raku}}{{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,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);
}</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>
10,327

edits