Henry Rich wrote: > > Given circle/sphere with center C and radius R, and a line-segment with > startpoint S and endpoint E, write the J code to tell whether the > line-segment intersects the circle/sphere. > > R is an atom, the rest are lists with 2 or 3 atoms. > > This problem arises in collision detection for games and simulators, or > if you are trying to see whether a path intersects a round obstacle. > > I found a solution whose brevity surprised me. > > Henry Rich > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > > Here is a quickly written solution to the problem of determining whether there is intersection of line segment (S,E) and open ball (C,R) in d=2,3,4,...
intersect=:dyad : 0 R =. x 'C S E'=.y CS =.C-S CE =.C-E ES =.E-S dot =.[: +/ *"1 nrm =.[: %: dot~ dist=.nrm@([ - ] * dot)(% nrm) (R > nrm CS)+.(R > nrm CE)+.(0 >(CS dot ES)* CE dot ES)*. R > CS dist ES ) Some examples: 2 intersect 5 5;6 1;7 4 0 2 intersect 5 5;2 4;7 7 1 2 intersect 5 5;3 4;1 6 0 2 intersect 5 5;2 4;6 1 0 2 intersect 5 5;6 1;7 3 0 2 intersect 5 5;3 3;1 5 0 2 intersect 5 5;6 5;8 6 1 Also, exchange of S and E should not change the result. -- View this message in context: http://old.nabble.com/Puzzle%3A-line-circle-sphere-intersection-tp29900737s24193p29907122.html Sent from the J Programming mailing list archive at Nabble.com. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
