【问题标题】:Circle - Line Intersection not working properly?Circle - Line Intersection 无法正常工作?
【发布时间】:2011-01-10 20:34:49
【问题描述】:

我在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 与圆圈在一起。我做错了什么?

【问题讨论】:

  • float D = p1.X * p2.Y * p2.X - p1.Y 似乎不正确
  • 取平方根然后再次平方 (dr) 似乎很愚蠢。

标签: c# math intersection


【解决方案1】:
float D = p1.X * p2.Y * p2.X - p1.Y;

你在这条线上混淆了你的运营商。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2016-05-25
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多