开发者

Circle - Line Intersection not working properly?

I wrote this circle-line intersection detection after http://mathworld.wolfram.com/Circle-LineIntersection.html, but it appears like it or I am missing something.

    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;
 开发者_Python百科   }

The only occasion it returns true is when Point2 is withing the circle. What am I doing wrong?


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

You've mixed up your operators on this line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜