【发布时间】:2021-11-10 19:52:43
【问题描述】:
我的任务是实现 GetNeighbors 方法,该方法从一组点返回仅与具有整数坐标 x 和 y 的点的 h 邻居的点。 (阅读代码中的摘要以获得更多理解)
其实我和Find k nearest neighbor 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 语句,它可以比较我拥有的点和来自点数组的点。嗯,怎么做,我对使用structs不是很熟悉,每次都是失败的。
这就是 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;
}
}
【问题讨论】:
-
什么是 h 邻居?您是否需要 h 最近的邻居(按欧几里得距离)或边长为 2h 的正方形内的邻居或最大欧几里得距离为 h 的点或其他点?
-
它是h距离内的邻居,它是方形的(图片发布)他们是紫色标记的。
标签: c# arrays algorithm struct coordinates