Circle - Line Intersection工作不正常?

时间:2011-01-10 20:34:49

标签: c# math intersection

我在http://mathworld.wolfram.com/Circle-LineIntersection.html之后写了这个圆线交叉检测,但它看起来像我或我遗漏了什么。

    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的唯一场合是Point2正在使用圆圈。我做错了什么?

1 个答案:

答案 0 :(得分:7)

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

你在这条线上混淆了你的经营者。