# Find the intersection point of a plane and ray

Given a ray with a point and a vector and a plane with a point and a normal vector to the plane. You have to find the intersection point of the plane and the ray..

So your job if you choose to accept is to write a shortest function that will do the job.

RULES:
The submission must be a complete program
And that's all

INPUT FORMAT:
x y z rx ry rz //gives the ray point and the ray vectors
x y z px py pz //gives the point on the plane and the vector normal to plane

OUTPUT FORMAT:
(x,y,z) //intersection point

Test Case:
2 3 4 0.577 0.577 0.577
7 1 3 1 0 0
(7,8,9)


The shortest codes in bytes win.

References: https://www.siggraph.org/education/materials/HyperGraph/raytrace/rayplane_intersection.htm thanks orlp for the reference

• then i think i should remove it. – Kishan Kumar Oct 2 '16 at 15:20
• Any specifications as to required precision? Say 3 decimal figures for example? – Luis Mendo Oct 2 '16 at 15:37
• Some test cases would be nice. Also, relevant. – orlp Oct 3 '16 at 10:04
• It will be very helpful if the formulae involved are included in the question, else programmers will have to search for them. – rnso Oct 3 '16 at 13:40
• I would strongly advice to stop enforcing your very specific input/output rules. They really don't add anything to the challenge. – Sanchises Oct 4 '16 at 16:32

# Mathematica, 20 bytes

Takes 4 lists as input. The dot product operator . has precedence over multiplication and division, so no more parentheses are needed.

#+#2(#3-#).#4/#2.#4&


x + v (r - x).n / v.n


where the arguments in order are x (ray point), v (ray direction), r (plane point), n (plane normal).

• does it give output in desired format? – Kishan Kumar Oct 4 '16 at 13:46
• @KishanKumar If by desired format you mean with parentheses, then no. Neither is the input a bunch of numbers separated by spaces. Mathematica works in lists, which are enclosed by {} and separated by ,. – for Monica Oct 4 '16 at 13:59
• No other answer shorter than this.. So, i have accepted this answer. – Kishan Kumar Nov 5 '17 at 12:05

# Perl, 107 bytes

Tested for all test cases (i.e. not tested at all) Run with each input number on its own line on STDIN

perl -M5.010 plane.pl
0
1
0
0
0
1
3
3
1
0
0
1
^D


plane.pl:

#!/usr/bin/perl
_=<>for a..l;say$a+($t=($j*($g-$a)+$k*($h-$b)+$l*($i-$c))/($j*$d+$k*$e+$l*$f))*$d,$",$b+$t*$e,$",$c+$t*$f


This kind of problem is absolutely not a good match for perlgolf. Perl lacks vector operations and uses too many $s • +1 for "for all test cases". – Steven H. Oct 3 '16 at 19:30 • does it give output in desired format? – Kishan Kumar Oct 4 '16 at 14:09 • It prints the 3 coordinates on one line separated by spaces without (). Adding the (,,) takes 10 trivial bytes (just add the extra characters to the say) – Ton Hospel Oct 4 '16 at 14:46 ## Haskell, 137 157/102 bytes Follows both input and output pattern. Program reads input from stdin. 157 bytes. z=zipWith main=do [(p,r),(l,n)]<-map(splitAt 3.map read.words).lines<$>getContents
print$(\[a,b,c]->(a,b,c))$z(+)p$map(*(sum(z(*)n(z(-)l p))/sum(z(*)n r)))r  With an input file that has format [[x,y,z],[rx,ry,rz],[x,y,z],[px,py,pz]] and output of format [x,y,z] (102 bytes): z=zipWith main=do [p,r,l,n]<-read<$>getContents
print$z(+)p$map(*(sum(z(*)n(z(-)l p))/sum(z(*)n r)))r

• does it follow both output and input pattern... – Kishan Kumar Oct 4 '16 at 14:08
• The optional version did. I changed it to do it anyway, at the cost of 20 bytes. – Angs Oct 4 '16 at 16:15
• well, remove the extra 20 bytes. no need to comply with my input pattern – Kishan Kumar Oct 4 '16 at 17:36
• It always complied with the input pattern - the output had different parentheses. I'll add an option that follows neither format. – Angs Oct 4 '16 at 20:26
• Do the one that saves you most bytes.. – Kishan Kumar Oct 5 '16 at 3:19

# R, 88 bytes

x=scan()
v=scan()
r=scan()
n=scan()
C=cat
C("(");C(x+v*sum((r-x)*n)/sum(v*n),sep=", ");C(")")


Adapted from the Mathematica answer. Takes the following values from stdin, x (ray point), v (ray direction), r (plane point), n (plane normal). Outputs (7, 8, 9) for the test case.

## JavaScript (ES6), 157 bytes

This could probably be shorter. The test case is subject to rounding errors, but otherwise it respects the challenge.

P=prompt;[a,b,c,d,e,f,g,h,i,j,k,l]=(P()+' '+P()).split .map(e=>+e);alert('('+(a+(t=(j*(g-a)+k*(h-b)+l*(i-c))/(j*d+k*e+l*f))*d)+','+(b+t*e)+','+(c+t*f)+')')


# JavaScript (ES6), 93 bytes

(a,b,c,A,B,C,x,y,z,X,Y,Z)=>(${t=X*(x-a)+Y*(y-b)+Z*(z-c)/(X*A+Y*B+Z*C),[a+t*A,b+t*B,c+t*C]})  f= (a,b,c,A,B,C,x,y,z,X,Y,Z)=>(${t=X*(x-a)+Y*(y-b)+Z*(z-c)/(X*A+Y*B+Z*C),[a+t*A,b+t*B,c+t*C]})

console.log(f(2, 3, 4, 0.577350269, 0.577350269, 0.577350269, 7, 1, 3, 1, 0, 0))