3 回答

TA貢獻1946條經(jīng)驗 獲得超4個贊
E
是射線的起點, L
是射線的終點, C
是你要測試的球體的中心 r
是那個球體的半徑
d
f
P=E+t*d
Px
Py
注意:我們將問題簡化為2D,我們得到的解決方案也適用于3D
得到:
擴張
x2-2xh+h 2
+y 2
-2yk+k 2
-r 2 = 0
塞子
X=e x
+TD x
Y=e y
+TD y
(E) x
+TD x )2
-2(E) x
+TD x
)h+h 2
+(E) y
+TD y )2
-2(E) y
+TD y
)k+k 2
-r 2 = 0
爆炸
ex2+2E x
白破疫苗 x
+t 2dx2
-2e x
H-2Td x
H+h 2
+e y2
+2E y
白破疫苗 y
+t 2dy2
-2e y
K-2td y
K+k 2
-r 2 = 0
群
t2(D) x2
+d y2
)+2t(E) xdx
+e ydy
-d x
氫-d y
k)+e x2
+e y2
-2e x
氫-2e y
K+h 2
+k 2
-r 2 = 0
最后,
t2(_d*_d)+2t(_e*_d-_d*_c)+_e*_e-2(_e*_c)+_c*_c-r 2 = 0
*其中_d是向量d,*是點積。 然后,
t2(_d*_d)+2t(_d*(_e-c)+(_e-c)*(_e-c)-r 2 = 0
讓f=_e-_c
t2(_d*_d)+2t(_d*_f)+_f*_f-r 2 = 0
t2
float a = d.Dot( d ) ; float b = 2*f.Dot( d ) ; float c = f.Dot( f ) - r*r ; float discriminant = b*b-4*a*c; if( discriminant < 0 ) { // no intersection } else { // ray didn't totally miss sphere, // so there is a solution to // the equation. discriminant = sqrt( discriminant ); // either solution may be on or off the ray so need to test both // t1 is always the smaller value, because BOTH discriminant and // a are nonnegative. float t1 = (-b - discriminant)/(2*a); float t2 = (-b + discriminant)/(2*a); // 3x HIT cases: // -o-> --|--> | | --|-> // Impale(t1 hit,t2 hit), Poke(t1 hit,t2>1), ExitWound(t1<0, t2 hit), // 3x MISS cases: // -> o o -> | -> | // FallShort (t1>1,t2>1), Past (t1<0,t2<0), CompletelyInside(t1<0, t2>1) if( t1 >= 0 && t1 <= 1 ) { // t1 is the intersection, and it's closer than t2 // (since t1 uses -b - discriminant) // Impale, Poke return true ; } // here t1 didn't intersect so we are either started // inside the sphere or completely past it if( t2 >= 0 && t2 <= 1 ) { // ExitWound return true ; } // no intn: FallShort, Past, CompletelyInside return false ; }

TA貢獻1812條經(jīng)驗 獲得超5個贊
AC
AB
AD
D
.D
C
R

TA貢獻1824條經(jīng)驗 獲得超6個贊
// compute the euclidean distance between A and B LAB = sqrt( (Bx-Ax)2+(By-Ay)2 ) // compute the direction vector D from A to B Dx = (Bx-Ax)/LAB Dy = (By-Ay)/LAB // the equation of the line AB is x = Dx*t + Ax, y = Dy*t + Ay with 0 <= t <= LAB. // compute the distance between the points A and E, where // E is the point of AB closest the circle center (Cx, Cy) t = Dx*(Cx-Ax) + Dy*(Cy-Ay) // compute the coordinates of the point E Ex = t*Dx+Ax Ey = t*Dy+Ay // compute the euclidean distance between E and C LEC = sqrt((Ex-Cx)2+(Ey-Cy)2) // test if the line intersects the circle if( LEC < R ) { // compute distance from t to circle intersection point dt = sqrt( R2 - LEC2) // compute first intersection point Fx = (t-dt)*Dx + Ax Fy = (t-dt)*Dy + Ay // compute second intersection point Gx = (t+dt)*Dx + Ax Gy = (t+dt)*Dy + Ay } // else test if the line is tangent to circle else if( LEC == R ) // tangent point to circle is E else // line doesn't touch circle
添加回答
舉報