Task: Find the intersection of a line with a plane





Java: public 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 times(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
        public String toString() {
            return String.format("(%f, %f, %f)", x, y, z);
        }
    }

    private static Vector3D intersectPoint(Vector3D rayVector, Vector3D rayPoint, Vector3D planeNormal, Vector3D planePoint) {
        Vector3D diff = rayPoint.minus(planePoint);
        double prod1 = diff.dot(planeNormal);
        double prod2 = rayVector.dot(planeNormal);
        double prod3 = prod1 / prod2;
        return rayPoint.minus(rayVector.times(prod3));
    }

    public 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);
        System.out.println("The ray intersects the plane at " + ip);
    }
}







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;