【发布时间】:2010-09-19 20:55:23
【问题描述】:
尝试制作一个非常简单的布尔函数来判断一条线是否与球体相交。
这似乎不是我想要的,尽管问题很相似: 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)
如果 2 个点具有直接的站点线,则返回 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的维度是Area,result是无维度的;r的维度是 Length,所以result < r没有意义。 -
@KennyTM:很好的观察。默认情况下,可惜维度不是我们类型系统的一部分。它会捕获很多错误。
标签: algorithm haskell 3d geometry