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

Content deleted Content added
Added Rust
Robbie (talk | contribs)
Line 68:
Intersection point is (0.000000,-5.000000,5.000000)
</pre>
 
=={{header|csharp}}==
<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}}==
Line 130 ⟶ 186:
{{out}}
<pre>The ray intersects the plane at (0.000000,-5.000000,5.000000)</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 11-07-2018