尝试制作一个非常简单的 bool 函数来判断直线是否与球体相交。
这似乎不是我想要的,尽管问题很相似: 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)
如有任何帮助,我们将不胜感激。
最佳答案
您使用了错误的等式。如果您的行表示为:
p1 + u (p2 - p1)
(其中 u
是标量),然后 top/bot
找到使该表达式尽可能靠近中心的 u
球体的。
所以我会修改你的代码:
where u = top/bot
nearestPoint = {- p1 + u (p2 - p1) -}
result = {- distance between nearestPoint and p3 -}
填写那个伪代码,你应该是金色的。您只是误解了 result
的含义。
顺便说一句,您可能可以通过使用 Data.VectorSpace
来大量清理您的代码。 .我可以使用它轻松完整地写出我的修正案:
import Data.VectorSpace
type Point = (Double, Double, Double)
inView :: Point -> Point -> Sphere -> Bool
inView p1 p2 (Sphere p3 r) = result < r
where u = top/bot
top = ...
bot = ...
nearestPoint = p1 ^+^ u *^ (p2 ^-^ p1)
result = magnitude (p3 ^-^ nearestPoint)
关于algorithm - 判断一条线是否与球体相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3747438/