2011-01-10 4 views
1

http://mathworld.wolfram.com/Circle-LineIntersection.html 다음에이 원선 교차 감지를 작성했지만 그 것처럼 보이거나 뭔가 빠졌습니다. Point2가 원을 withing에 때원선 교차로가 제대로 작동하지 않습니까?

public static bool Intersect 
    (Vector2f CirclePos, float CircleRad, Vector2f Point1, Vector2f Point2) 
    { 
     Vector2f p1 = Vector2f.MemCpy(Point1); 
     Vector2f p2 = Vector2f.MemCpy(Point2); 

     // Normalize points 
     p1.X -= CirclePos.X; 
     p1.Y -= CirclePos.Y; 
     p2.X -= CirclePos.X; 
     p2.Y -= CirclePos.Y; 

     float dx = p2.X - p1.X; 
     float dy = p2.Y - p1.Y; 
     float dr = (float)Math.Sqrt((double)(dx * dx) + (double)(dy * dy)); 
     float D = p1.X * p2.Y * p2.X - p1.Y; 

     float di = (CircleRad * CircleRad) * (dr * dr) - (D * D); 

     if (di < 0) return false; 
     else return true; 
    } 

가 true를 돌려주는 유일한 기회입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+2

float D = p1.X * p2.Y * p2.X - p1.Y impromore – Instantsoup

+3

제곱근을 취한 다음 다시 정사각형 ('dr')으로 바보처럼 보인다. –

답변

7
float D = p1.X * p2.Y * p2.X - p1.Y; 

사용자가이 줄에 연산자를 혼합했습니다.

+0

올바른 연산자는 무엇입니까? – Peter