2011-01-10 6 views
1

Ich schrieb diese Kreis-Linie-Kreuzung Erkennung nach http://mathworld.wolfram.com/Circle-LineIntersection.html, aber es scheint so, oder ich vermisse etwas.Circle - Line Intersection funktioniert nicht richtig?

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; 
    } 

Die einzige Gelegenheit, es true zurück, wenn Point2 den Kreis ist withing. Was mache ich falsch?

+2

float D = p1.X * p2.Y * p2.X - p1.Y scheint unsachgemäß – Instantsoup

+3

Es scheint albern, die Quadratwurzel zu nehmen und sie dann wieder zu quadrieren ('dr'). –

Antwort

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

Sie haben Ihre Betreiber auf dieser Linie verwechselt.

+0

Also, was ist der richtige Operator? – Peter