c# - 如何获取从固定点到特定距离的坐标邻居
问题描述
我的任务是实现 GetNeighbors 方法,该方法从一组点返回仅是具有整数坐标 x 和 y 的点的 h 邻居的点。(阅读代码中的摘要以获得更多理解)
实际上,我和这个人有同样的任务Find k 最近邻居 in C#,但那里的答案似乎不起作用。而且我想只用我猜应该期望的循环来做这件事:)>。
现在我的代码到目前为止:
/// <summary>
/// Gets from a set of points only points that are h-neighbors for a point with integer coordinates x and y.
/// </summary>
/// <param name="point">Given point with integer coordinates x and y.</param>
/// <param name="h">Distance around a given point.</param>
/// <param name="points">A given set of points.</param>
/// <returns>Only points that are h-neighbors for a point with integer coordinates x and y.</returns>
/// <exception cref="ArgumentNullException">Throw when array points is null.</exception>
/// <exception cref="ArgumentException">Throw when h-distance is less or equals zero.</exception>
public static Point[] GetNeighbors(Point point, int h, params Point[] points)
{
if (points is null)
{
throw new ArgumentNullException(nameof(points));
}
if (h <= 0)
{
throw new ArgumentException(null);
}
List<Point> neighbors = new List<Point>();
int left = point.X - h;
int right = point.X + h;
int bottom = point.Y - h;
int top = point.Y + h;
for (int y = top; y <= bottom; y++)
{
for (int x = left; x <= right; x++)
{
// Yeah...
}
}
return neighbors.ToArray();
}
所以到目前为止我所做的是在邻域中找到顶部、底部、左侧和右侧边界。我认为我只需要做一个 if 语句,它可以比较我拥有的点和来自 points 数组的点。嗯如何做到这一点,我对使用结构不是很熟悉,每次都是失败的。
这就是 struct Point 的构建方式:
/// <summary>
/// Represents a point on the coordinate plane.
/// </summary>
public readonly struct Point : System.IEquatable<Point>
{
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
public int X { get; }
public int Y { get; }
public static bool operator ==(Point left, Point right)
{
return left.Equals(right);
}
public static bool operator !=(Point left, Point right)
{
return !(left == right);
}
public override int GetHashCode()
{
return this.X.GetHashCode() ^ this.Y.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is null)
{
return false;
}
if (!(obj is Point))
{
return false;
}
if (obj.GetType() != this.GetType())
{
return false;
}
Point point = (Point)obj;
return this.Equals(point);
}
public bool Equals(Point other)
{
return this.X == other.X && this.Y == other.Y;
}
}
解决方案
您不能循环遍历坐标,而是遍历点。这是使用 LINQ 的解决方案:
public static Point[] GetNeighbors(Point point, int h, params Point[] points)
{
int minX = point.X - h;
int maxX = point.X + h;
int minY = point.Y - h;
int maxY = point.Y + h;
return points
.Where(p => p.X >= minX && p.X <= maxX && p.Y >= minY && p.Y <= maxY)
.ToArray();
}
使用 C# 9.0,您可以简化与模式匹配的比较
.Where(p => p.X is >=minX and <=maxX && p.Y is >=minY and <=maxY)
这是使用显式循环的解决方案。由于我们不知道结果数组的大小,我将结果存储在一个列表中:
...
var list = new List<Point>();
foreach (Point p in points) {
if (p.X is >=minX and <=maxX && p.Y is >=minY and <=maxY) {
list.Add(p);
}
}
return list.ToArray();
或者您可以返回一个列表。
另一种解决方案是使用迭代器方法:
public static IEnumerable<Point> GetNeighbors(Point point, int h, params Point[] points)
{
int minX = point.X - h;
int maxX = point.X + h;
int minY = point.Y - h;
int maxY = point.Y + h;
foreach (Point p in points) {
if (p.X is >=minX and <=maxX && p.Y is >=minY and <=maxY) {
yield return p;
}
}
}
请注意,这种方法不会将点存储在集合中,而是在循环结果时执行方法内部的循环(显式地在 foreach 循环中或通过应用等隐式地执行.ToArray()
).Count()
。
Point[] result = GetNeighbors(center, 2, p1, p2, p3, p4).ToArray();
推荐阅读
- javascript - 如何最好地在烧瓶应用程序中动态填充下拉列表?
- json - 修改 json 对象数组
- powershell - 使用employeeid更新员工经理
- python - 在烧瓶中创建可下载文件的问题
- swiftui - 防止 SwiftUI App 进入睡眠操作 WatchOS
- javascript - 谷歌注册不适用于第一次点击元素,但之后工作正常。为什么?
- apache-spark - 为什么一旦我将“mapValues()”应用于 RDD,属性就会消失?
- sql - SQL中多个表中的项目总和
- php - Laravel 文件上传(多个)
- c++ - 如何在 bazel 中使用 lib 组?