尝试创建一个非常简单的布尔函数,以确定一条直线是否与一个球相交。
尽管问题类似,但这似乎不是我想要的:Intersection of a line and a Sphere? 我还尝试了以下列出的算法: http://www.docstoc.com/docs/7747820/Intersection-of-a-Line-and-a-Sphere 和 http://www.ccs.neu.edu/home/fell/CSU540/programs/RayTracingFormulas.htm,但都没有真正成功。
我最近的代码(用Haskell编写)如下:
尽管问题类似,但这似乎不是我想要的:Intersection of a line and a Sphere? 我还尝试了以下列出的算法: http://www.docstoc.com/docs/7747820/Intersection-of-a-Line-and-a-Sphere 和 http://www.ccs.neu.edu/home/fell/CSU540/programs/RayTracingFormulas.htm,但都没有真正成功。
我最近的代码(用Haskell编写)如下:
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)
如果两个点之间有直接的视线,它将返回true。这对于一些简单的情况有效,但对于应该有效的其他情况则失败,例如:
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)
非常感谢您的帮助。
top
和bot
具有面积的量纲,而result
是无量纲的;r
具有长度的量纲,因此result < r
没有意义。 - kennytm