Advertisement
yahorrr

Untitled

Apr 25th, 2022
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. using System;
  2.  
  3. namespace GetNeighbors
  4. {
  5.     public static class CartesianCoordinates
  6.     {
  7.         /// <summary>
  8.         /// Gets from a set of points only points that are h-neighbors for a point with integer coordinates x and y.
  9.         /// </summary>
  10.         /// <param name="point">Given point with integer coordinates x and y.</param>
  11.         /// <param name="h">Distance around a given point.</param>
  12.         /// <param name="points">A given set of points.</param>
  13.         /// <returns>Only points that are h-neighbors for a point with integer coordinates x and y.</returns>
  14.         /// <exception cref="ArgumentNullException">Throw when array points is null.</exception>
  15.         /// <exception cref="ArgumentException">Throw when h-distance is less or equals zero.</exception>
  16.         public static Point[] GetNeighbors(Point point, int h, params Point[] points)
  17.         {
  18.             if (points is null)
  19.             {
  20.                 throw new ArgumentNullException(nameof(points));
  21.             }
  22.  
  23.             if (h <= 0)
  24.             {
  25.                 throw new ArgumentException("Distance can't be less or equals zero", nameof(h));
  26.             }
  27.  
  28.             Point[] hNeighbors = Array.Empty<Point>();
  29.             int count = 0;
  30.  
  31.             for (int i = 0; i < points.Length; i++)
  32.             {
  33.                 if (Math.Abs(point.X - points[i].X) <= h && Math.Abs(point.Y - points[i].Y) <= h)
  34.                 {
  35.                     count++;
  36.                     if (count > hNeighbors.Length)
  37.                     {
  38.                         Array.Resize(ref hNeighbors, hNeighbors.Length == 0 ? 1 : hNeighbors.Length * 2);
  39.                     }
  40.  
  41.                     hNeighbors[count - 1] = points[i];
  42.                 }
  43.             }
  44.  
  45.             Array.Resize(ref hNeighbors, count);
  46.  
  47.             return hNeighbors;
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement