/// <summary> /// Finds common neighbours between two coordinates /// </summary> /// <param name="otherCoords">Other Coords</param> /// <returns>List of common neighbours</returns> public List <FloatCoords> GetCommonNeighboursWith(Coords otherCoords) { CoordsCalculator otherCoordsCalculator = new CoordsCalculator((FloatCoords)otherCoords); FloatCoords[] myNeighbours = GetNeighbours(); return(otherCoordsCalculator.GetNeighbours().Where(neighbour => myNeighbours.Contains(neighbour)).ToList()); }
/// <summary> /// Gets all coords of cells on a ray with destination /// </summary> /// <param name="destination">Ray's end node</param> /// <returns>List of coords the ray passes through</returns> public List <Coords> GetCoordsOnRayWith(FloatCoords destination) { void AddCellsBetweenVerticalPoints(double startPoint, double endPoint, int xCoord, ref List <Coords> outputList) { // Switch start- and endpoints if necessary if (startPoint > endPoint) { startPoint = endPoint + startPoint; endPoint = Math.Ceiling(startPoint - endPoint); startPoint = Math.Floor(startPoint - endPoint); } for (int i = (int)startPoint - 1; i <= endPoint + 1; i++) { Coords currentPoint = new Coords() { x = xCoord, y = i }; CoordsCalculator coordsCalculator = new CoordsCalculator((FloatCoords)currentPoint); List <Coords> cells = new List <Coords> { currentPoint }; cells.AddRange(coordsCalculator.GetNeighbours().Select(neighbour => (Coords)neighbour)); foreach (Coords cell in cells) { if (outputList.Contains(cell)) { continue; } outputList.Add(cell); } } } List <Coords> coordsOnRay = new List <Coords>(); FloatCoords startCoords = this.coords; // Switch start- and endpoints if necessary if (startCoords.x > destination.x) { FloatCoords x = startCoords; startCoords = destination; destination = x; } double directionCoefficient = (double)destination.x > (double)startCoords.x ? ((double)destination.y - startCoords.y) / ((double)destination.x - startCoords.x) : (double)destination.y - startCoords.y; FloatCoords previousCoords; FloatCoords currentCoords = startCoords; while (currentCoords.x <= destination.x) { previousCoords = currentCoords; currentCoords.x++; currentCoords.y += (float)directionCoefficient; AddCellsBetweenVerticalPoints(previousCoords.y, currentCoords.y, (int)previousCoords.x, ref coordsOnRay); } return(coordsOnRay); }