Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace GetNeighbors
- {
- public static class CartesianCoordinates
- {
- /// <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("Distance can't be less or equals zero", nameof(h));
- }
- Point[] hNeighbors = Array.Empty<Point>();
- int count = 0;
- for (int i = 0; i < points.Length; i++)
- {
- if (Math.Abs(point.X - points[i].X) <= h && Math.Abs(point.Y - points[i].Y) <= h)
- {
- count++;
- if (count > hNeighbors.Length)
- {
- Array.Resize(ref hNeighbors, hNeighbors.Length == 0 ? 1 : hNeighbors.Length * 2);
- }
- hNeighbors[count - 1] = points[i];
- }
- }
- Array.Resize(ref hNeighbors, count);
- return hNeighbors;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement