Find the intersection of a line with a plane: Difference between revisions
Content added Content deleted
(Add Factor) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 80: | Line 80: | ||
Intersection point is (0.000000,-5.000000,5.000000) |
Intersection point is (0.000000,-5.000000,5.000000) |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
<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|C++}}== |
=={{header|C++}}== |
||
Line 139: | Line 195: | ||
{{out}} |
{{out}} |
||
<pre>The ray intersects the plane at (0, -5, 5)</pre> |
<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}}== |
=={{header|D}}== |
||
Line 780: | Line 780: | ||
{{out}} |
{{out}} |
||
<pre>Intersection at point: 0 -5 5</pre> |
<pre>Intersection at point: 0 -5 5</pre> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2016.11}} |
|||
{{trans|Python}} |
|||
<lang perl6>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) ) |
|||
);</lang> |
|||
{{out}} |
|||
<pre>Intersection at point: (0 -5 5)</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 913: | Line 882: | ||
{{out}} |
{{out}} |
||
No output -- all tests passed! |
No output -- all tests passed! |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2016.11}} |
|||
{{trans|Python}} |
|||
<lang perl6>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) ) |
|||
);</lang> |
|||
{{out}} |
|||
<pre>Intersection at point: (0 -5 5)</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,234: | Line 1,235: | ||
}</lang> |
}</lang> |
||
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/oLTlNZk/0 ScalaFiddle (JavaScript)]. |
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/oLTlNZk/0 ScalaFiddle (JavaScript)]. |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl 6}} |
{{trans|Perl 6}} |