Find the intersection point of a plane and ray

4

1

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

Kishan Kumar

Posted 2016-10-02T14:36:07.980

Reputation: 427

then i think i should remove it. – Kishan Kumar – 2016-10-02T15:20:25.753

Any specifications as to required precision? Say 3 decimal figures for example? – Luis Mendo – 2016-10-02T15:37:57.677

Do degenerate cases need to be handled? Ray parallel with the plane? Ray coincident with the plane? Any others??? – Digital Trauma – 2016-10-02T23:48:53.367

Nope. Not needed to handle those.. – Kishan Kumar – 2016-10-03T03:04:39.647

2

Some test cases would be nice. Also, relevant.

– orlp – 2016-10-03T10:04:25.590

A challenge like this could definitely use test cases; please add some! – Lynn – 2016-10-03T10:16:48.097

1It will be very helpful if the formulae involved are included in the question, else programmers will have to search for them. – rnso – 2016-10-03T13:40:58.597

@rnso i added a working function in my example. But Some one commented that my example is not a program but a function. So removed it. – Kishan Kumar – 2016-10-03T15:11:19.913

@orlp this is my first question of the type. So I'm sorry to the community for my mistakes – Kishan Kumar – 2016-10-03T15:12:36.590

@Lynn will add working testcases soon – Kishan Kumar – 2016-10-03T15:13:00.770

2

I would strongly advice to stop enforcing your very specific input/output rules. They really don't add anything to the challenge.

– Sanchises – 2016-10-04T16:32:30.530

ooh. thanks @sanchises for pointing it out.. My bad. Haven't seen those meta posts – Kishan Kumar – 2016-10-04T16:34:57.733

I guess someone had ought to explain to you why nobody seemed to be inclined to follow your rules while still demanding strictness on some other parts :) – Sanchises – 2016-10-04T16:57:51.767

will try to follow the rules i discover on meta. – Kishan Kumar – 2016-10-04T16:59:08.593

Answers

3

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&

Readable version:

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).

for Monica

Posted 2016-10-02T14:36:07.980

Reputation: 1 172

does it give output in desired format? – Kishan Kumar – 2016-10-04T13:46:40.460

@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 – 2016-10-04T13:59:33.217

No other answer shorter than this.. So, i have accepted this answer. – Kishan Kumar – 2017-11-05T12:05:39.927

3

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

Ton Hospel

Posted 2016-10-02T14:36:07.980

Reputation: 14 114

3+1 for "for all test cases". – Steven H. – 2016-10-03T19:30:36.460

does it give output in desired format? – Kishan Kumar – 2016-10-04T14:09:21.790

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 – 2016-10-04T14:46:32.347

1

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

Angs

Posted 2016-10-02T14:36:07.980

Reputation: 4 825

does it follow both output and input pattern... – Kishan Kumar – 2016-10-04T14:08:45.827

The optional version did. I changed it to do it anyway, at the cost of 20 bytes. – Angs – 2016-10-04T16:15:07.503

well, remove the extra 20 bytes. no need to comply with my input pattern – Kishan Kumar – 2016-10-04T17:36:11.397

It always complied with the input pattern - the output had different parentheses. I'll add an option that follows neither format. – Angs – 2016-10-04T20:26:38.287

Do the one that saves you most bytes.. – Kishan Kumar – 2016-10-05T03:19:54.813

1

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.

rturnbull

Posted 2016-10-02T14:36:07.980

Reputation: 3 689

0

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)+')')

XavCo7

Posted 2016-10-02T14:36:07.980

Reputation: 274

0

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))

darrylyeo

Posted 2016-10-02T14:36:07.980

Reputation: 6 214