circle-AABB遏制测试
问题描述:
我目前正在写一个基于细分空间(这是一款游戏)的系统,我需要能够测试一个圆圈是否完全包含一个正方形。circle-AABB遏制测试
奖励积分,我要指出,我的系统工作在N个维度,因此,如果你的算法的工作方式是通过每个维度循环和做的事情,现在它是这样;)
答
在2^N个角落中,您只需检查超球面中心的最远角在超球面内。
所以:
distance = 0
for each dimension D:
a = abs(coordinate of sphere center in D - min coordinate of cube in D)
b = abs(coordinate of sphere center in D - max coordinate of cube in D)
distance += max(a,b)^2
if distance <= radius*radius then cube is in sphere.
答
// lower and upper are opposite corners (e.g. min and max for each dimension)
within(center,radius,lower,upper):
maxsq<-radius^2
sumsq<-0
for c<-0 to N-1
dl=(center[c]-lower[c])^2
du=(center[c]-upper[c])^2
sumsq<-sumsq+max(dl,du)
if sumsq > maxsq return false
return true
您可能希望存储球体的maxsq,而不是每次重新计算(一个非常小的花费)。
没错,这是正确的做法。如果你知道立方体的中心c'和它的半边长度h,那么这个条件可以写成: d(c,c')+ sqrt(dim * h^2)
Camille
2009-09-11 06:49:20
我刚想过当我准备睡觉的时候,这最后一晚的时间到了,直到早上才算安全:)干得好。 – 2009-09-11 17:59:26
卡米尔,我不认为这是正确的,除非立方体中心位于与圆心相比的对角线上。确实,h * sqrt(N)是包含立方体的圆环的半径,但考虑到正好在球体内的立方体(其中一个角落错过了接触边界),除非立方体的中心与半径到该角落,总和大于半径。 – 2009-09-11 18:19:43