private void PlaceTileMapping(int x, int y, int tileSetId, int tileId) { var tileCoordinate = new TileCoordinate(x, y, tileSetId, tileId); if (CoordinateMap.Contains(tileCoordinate)) { var index = CoordinateMap.IndexOf(tileCoordinate); CoordinateMap[index] = tileCoordinate; } else { CoordinateMap.Add(tileCoordinate); } }
private bool PointIsVisible(Point origin, Point target) { Fraction slope; bool slopeIsUndefined = false; int deltaX = target.X - origin.X; int deltaY = target.Y - origin.Y; // Test for purely vertical slope if (deltaX == 0) { slopeIsUndefined = true; // This slope value will be ignored, but needs assigned so the program compiles slope = new Fraction(); } // Test for purely horizontal slope else if (deltaY == 0) { // This slope value will be ignored, but needs assigned so the program compiles slope = new Fraction(); } // Any other slope else { slope = new Fraction(deltaY, deltaX); } bool negativeDirection = false; // Check UP-DOWN direction when deniminator is 0 if (slopeIsUndefined) { if (target.Y < origin.Y) { negativeDirection = true; } } // Check LEFT-RIGHT direction when numerator is 0 if (slope.Numerator == 0 && target.X < origin.X) { negativeDirection = true; } //Console.WriteLine($"origin: {origin} | target: {target} | deltaX={deltaX} deltaY={deltaY} | slope: {slope.ToString()} neg: {negativeDirection}"); // Handling for straight vertical slope if (slopeIsUndefined) { // Note: Negative Y direction visually goes UP on the input grid, since the top-left corner is (0,0) if (negativeDirection) { for (int y = origin.Y - 1; y >= target.Y; y--) { Point currentPoint = new Point(origin.X, y); if (CoordinateMap.Contains(currentPoint)) { if (currentPoint.Equals(target)) { return(true); } else { return(false); } } } throw new InvalidProgramException($"Failed to process with origin: {origin} | target: {target}"); } // Note: Negative Y direction visually goes DOWN on the input grid, since the top-left corner is (0,0) else { for (int y = origin.Y + 1; y <= target.Y; y++) { Point currentPoint = new Point(origin.X, y); if (CoordinateMap.Contains(currentPoint)) { if (currentPoint.Equals(target)) { return(true); } else { return(false); } } } } } // Handling for straight horizontal slope if (slope.Numerator == 0) { if (negativeDirection) { for (int x = origin.X - 1; x >= target.X; x--) { Point currentPoint = new Point(x, origin.Y); if (CoordinateMap.Contains(currentPoint)) { if (currentPoint.Equals(target)) { return(true); } else { return(false); } } } } else { for (int x = origin.X + 1; x <= target.X; x++) { Point currentPoint = new Point(x, origin.Y); if (CoordinateMap.Contains(currentPoint)) { if (currentPoint.Equals(target)) { return(true); } else { return(false); } } } } } // Handling for all other slope values bool endLoop = false; long runningX = origin.X; long runningY = origin.Y; while (!endLoop) { runningX += slope.Denominator; runningY += slope.Numerator; Point currentPoint = new Point((int)runningX, (int)runningY); if (CoordinateMap.Contains(currentPoint)) { if (currentPoint.Equals(target)) { endLoop = true; return(true); } else { endLoop = true; return(false); } } } throw new InvalidProgramException($"Failed to find visible point with origin: {origin} | target: {target}"); }