首页 > 解决方案 > 确定一个点是否在等轴测图上的圆内

问题描述

我试图找出一种方法来检查某个点是在等轴测图上的圆圈内还是圆圈外。我目前正在使用以下方法来绘制圆圈:

    public static List<Coords> GetBrushCircleCoords(int x0, int y0, int radius)
    {
        List<Coords> coords = new List<Coords>();
        int x = radius;
        int y = 0;
        int err = 0;

        while (x >= y)
        {
            coords.Add(new Coords(x0 + x, y0 + y));
            coords.Add(new Coords(x0 + y, y0 + x));
            coords.Add(new Coords(x0 - y, y0 + x));
            coords.Add(new Coords(x0 - x, y0 + y));
            coords.Add(new Coords(x0 - x, y0 - y));
            coords.Add(new Coords(x0 - y, y0 - x));
            coords.Add(new Coords(x0 + y, y0 - x));
            coords.Add(new Coords(x0 + x, y0 - y));

            y += 1;
            err += 1 + 2 * y;
            if (2 * (err - x) + 1 > 0)
            {
                x -= 1;
                err += 1 - 2 * x;
            }
        }
        return coords;
    }

我试图确定该点是否在圆内的方法基本上是取所需的点,确定它到中心的距离并使用以下方法检查它是否大于半径:

    public static int GetDistance(Coords _from, Coords _to)
    {
        return Math.Max(Math.Abs(_from.X - _to.X), Math.Abs(_from.Y - _to.Y));
    }

但是,GetDistance 方法似乎不是计算它的最佳方法,因为它计算的距离比 GetBrushCircleCoords 上使用的距离要短得多。确定某个点是否在这个圆圈内/外的正确方法是什么?

标签: c#

解决方案


在欧几里得平面上,距离函数(度量)由勾股定理给出。所以 GetDistance 不应该是这样的:

public static double GetDistance(Coords from, Coords to)
{
    //a^2 + b^2 = c^2
    var a = from.X - to.X;
    var b = from.Y - to.Y;
    var c = Math.Sqrt(a*a+b*b);
    return c;
}

推荐阅读