To begin, let S =: S - S and E =: E - S and C =: C - S .  The original problem 
is equivalent to finding whether the new segment SE intersects the ball (sphere 
plus interior) with radius R and center the new C .

Let o =: [: +/ * (dot product).  Because S is now the origin, the straight line 
SE intersects the sphere if there is a real number t such that

(*: R) = (C - t*E) o (C - t*E)

The roots of the corresponding quadratic are given by

 > }. p. ( (C o C) - *: R ) , ( - 2 * C o E ) , ( E o E )

If the roots are imaginary, or real and both > 1 , or real and both < 0
the segment ES does not intersect the ball.  Otherwise it does.

It helps to remember that as S is now the origin, the segment SE consists of 
the 
points t*E where the real number t satisfies

(0 <: t) *. (t <: 1)

and to draw pictures of the cases where the roots are real and both > 1 , or 
real and both < 0 .

Kip Murray


On 10/6/2010 3:53 PM, 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to