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

m
→‎{{header|Wren}}: Added libheader.
(→‎{{header|D}}: add evaldraw solution for intersect line plane)
m (→‎{{header|Wren}}: Added libheader.)
 
(4 intermediate revisions by 4 users not shown)
Line 591:
{{out}}
<pre>The ray intersects the plane at (0.000000,-5.000000,5.000000)</pre>
 
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight lang="ecmascript"easylang>class Vector3D {
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 += _xl[i] =* xr[i]
.
return res
.
proc scale f . l[] .
for i to 3
l[i] = _yl[i] =* yf
.
.
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}}==
Line 596 ⟶ 631:
 
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;};
Line 1,789 ⟶ 1,824:
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}}==
Line 2,042 ⟶ 2,088:
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-vector}}
<syntaxhighlight lang="ecmascript">class Vector3D {
<syntaxhighlight lang="wren">import "./vector" for Vector3
construct new(x, y, z) {
_x = x
_y = y
_z = z
}
 
x { _x }
y { _y }
z { _z }
 
+(v) { Vector3D.new(_x + v.x, _y + v.y, _z + v.z) }
 
-(v) { Vector3D.new(_x - v.x, _y - v.y, _z - v.z) }
 
*(s) { Vector3D.new(s * _x, s * _y, s * _z) }
 
dot(v) { _x * v.x + _y * v.y + _z * v.z }
 
toString { "(%(_x), %(_y), %(_z))" }
}
 
var intersectPoint = Fn.new { |rayVector, rayPoint, planeNormal, planePoint|
Line 2,072 ⟶ 2,099:
}
 
var rv = Vector3DVector3.new(0, -1, -1)
var rp = Vector3DVector3.new(0, 0, 10)
var pn = Vector3DVector3.new(0, 0, 1)
var pp = Vector3DVector3.new(0, 0, 5)
var ip = intersectPoint.call(rv, rp, pn, pp)
System.print("The ray intersects the plane at %(ip).")</syntaxhighlight>
9,485

edits