Trying to make a very simple boolean function that will find whether a line intersects a sphere.
This did not seem to be what I want, even though the question was similar: http://stackoverflow.com/questions/910565/intersection-of-a-line-and-a-sphere
Also I have tried the algorithms listed at:
http://www.docstoc.com/docs/7747820/Intersection-of-a-Line-and-a-Sphere
and
http://www.ccs.neu.edu/home/fell/CSU540/programs/RayTracingFormulas.htm
with no real luck.
My most recent code (in Haskell) looks like:
data Point = Point { x :: Float, y :: Float, z :: Float} deriving (Eq, Show, Read)
data Sphere = Sphere { center :: Point, radius :: Float } deriving (Eq, Show, Read)
inView :: Point -> Point -> Sphere -> Bool
inView (Point x1 y1 z1) (Point x2 y2 z2) (Sphere (Point x3 y3 z3) r)
| result > 0 && result < r = False
| otherwise = True
where result = top/bot
top = (x3 - x1) * (x2 - x1) + (y3 - y1) * (y2 - y1) + (z3 - z1) * (z2 - z1)
bot = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)
Where it returns true if 2 points have a direct line-of-site. This works for some simple cases, but fails for others that should work, such as:
inView (Point {x = 43.64, y = -183.20, z = 187.37}) (Point {x = 42.04, y = -183.58, z = 187.37}) (Sphere (Point 0 0 0) 5)
Any help would be appreciated.