Finding the intersection of an infinite ray with a plane in 3D is an important topic in collision detection.



Task

Find the point of intersection for the infinite ray with direction   (0, -1, -1)   passing through position   (0, 0, 10)   with the infinite plane with a normal vector of   (0, 0, 1)   and which passes through [0, 0, 5].




# add as many as you please
def addVector:
  transpose | add;
 
# . - y
def minusVector(y):
  [.[0] - y[0], .[1] - y[1], .[2] - y[2]];
 
# scalar multiplication: . * s
def multVector(s):
    map(. * s);
 
def dot(y):
  .[0] * y[0] + .[1] * y[1] + .[2] * y[2];
 
def intersectPoint($rayVector; $rayPoint; $planeNormal; $planePoint):
  ($rayPoint | minusVector($planePoint)) as $diff
  | ($diff|dot($planeNormal)) as $prod1
  | ($rayVector|dot($planeNormal)) as $prod2
  | $rayPoint | minusVector($rayVector | multVector(($prod1 / $prod2) )) ;
 
 
def rv : [0, -1, -1];
def rp : [0,  0, 10];
def pn : [0,  0,  1];
def pp : [0,  0,  5];
 
"The ray intersects the plane at:",
intersectPoint(rv; rp; pn; pp)