c# - 确定一个点是否在等轴测图上的圆内
问题描述
我试图找出一种方法来检查某个点是在等轴测图上的圆圈内还是圆圈外。我目前正在使用以下方法来绘制圆圈:
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 上使用的距离要短得多。确定某个点是否在这个圆圈内/外的正确方法是什么?
解决方案
在欧几里得平面上,距离函数(度量)由勾股定理给出。所以 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;
}
推荐阅读
- android - 如何为边框创建带有圆角矩形的图像
- android - Android 应用程序中是否使用了数据库?
- networking - 使用所有编码方案为 000001000101110 设计数字信号
- python - Python:model.fit() 错误,不支持无值
- python - Python正则表达式字符串替换问题
- java - 如何在java中声明和加入可完成的未来响应列表
- ios - 如何在不提示非香港用户提供权限的情况下使用带有后台交付的 Healthkit 观察者查询?
- clojure - “Quick Clojure”一书的勘误表?
- image - 我们如何在 PDF 文档中添加图像字段?
- apache-spark - Spark正在将一些行加载到一列中?