public Vector3 GetTopLeft(IGridDimensions2D grid) { switch (AxisMode) { case EAxisMode.XZ: return(new Vector3(grid.TopLeft.x, 0, grid.TopLeft.y)); case EAxisMode.XY: return(new Vector3(grid.TopLeft.x, grid.TopLeft.y, 0)); } return(Vector3.zero); }
public Vector3 GetCellSize(IGridDimensions2D grid) { switch (AxisMode) { case EAxisMode.XZ: return(new Vector3(grid.CellSize.x, 0, grid.CellSize.y)); case EAxisMode.XY: return(new Vector3(grid.CellSize.x, grid.CellSize.y, 0)); } return(Vector3.zero); }
public Vector3 GetOffsets(IGridDimensions2D grid, int x, int y) { switch (AxisMode) { case EAxisMode.XZ: return(new Vector3(grid.CellSize.x * x, 0, grid.CellSize.y * y)); case EAxisMode.XY: return(new Vector3(grid.CellSize.x * x, grid.CellSize.y * y, 0)); } return(Vector3.zero); }
public IEnumerable <Vector2Int> Supercover(IGridDimensions2D grid) { int minX = (int)Math.Max(0, (topLeft.x - grid.TopLeft.x) / grid.CellSize.x); int minY = (int)Math.Max(0, (topLeft.y - grid.TopLeft.y) / grid.CellSize.y); int maxX = (int)Math.Min(grid.Columns - 1, (bottomRight.x - grid.TopLeft.x) / grid.CellSize.x); int maxY = (int)Math.Min(grid.Rows - 1, (bottomRight.y - grid.TopLeft.y) / grid.CellSize.y); for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { yield return(new Vector2Int(x, y)); } } }
/// <summary> /// The supercover of a circle is it's bounding box /// </summary> public IEnumerable <Vector2Int> Supercover(IGridDimensions2D grid) { Vector2 offsetPosition = center - grid.TopLeft; int minX = (int)Math.Max(0, (offsetPosition.x - radius) / grid.CellSize.x); int minY = (int)Math.Max(0, (offsetPosition.y - radius) / grid.CellSize.y); int maxX = (int)Math.Min(grid.Columns - 1, (offsetPosition.x + radius) / grid.CellSize.x); int maxY = (int)Math.Min(grid.Rows - 1, (offsetPosition.y + radius) / grid.CellSize.y); for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { yield return(new Vector2Int(x, y)); } } }
public IEnumerable <Vector2Int> Supercover(IGridDimensions2D grid) { return(supercover); }
public CachedShape(IGridDimensions2D grid, IConvex2D shape) { this.shape = shape; this.supercover = shape.Supercover(grid).ToArray(); }
public IEnumerable <Vector2Int> Supercover(IGridDimensions2D grid) { yield return(new Vector2Int( (int)(Math.Min(grid.Columns - 1, Math.Max(0, (x - grid.TopLeft.x) / grid.CellSize.x))), (int)(Math.Min(grid.Rows - 1, Math.Max(0, (y - grid.TopLeft.y) / grid.CellSize.y))))); }
/// <summary> /// Modified code from http://playtechs.blogspot.com/2007/03/raytracing-on-grid.html /// /// Traces the line segment and returns all cell indexes that are passed /// /// </summary> public IEnumerable <Vector2Int> Supercover(IGridDimensions2D grid) { // All flooring is done with a cast to int so this will only work // for positive values. Which is fine for our grid since after we normalized our coords // to the grid there are no negative values (except when we're out of bounds, but that's not allowed) // Set to offset of grid and make each integer correspond to a cell Vector2 normalizedV = (v - grid.TopLeft) / grid.CellSize; Vector2 normalizedW = (w - grid.TopLeft) / grid.CellSize; float lineDeltaX = Math.Abs(normalizedW.x - normalizedV.x); float lineDeltaY = Math.Abs(normalizedW.y - normalizedV.y); // starting position in grid int x = (int)normalizedV.x; int y = (int)normalizedV.y; int totalSteps = 1; int xDirection, yDirection; float error; if (lineDeltaX < float.Epsilon) { xDirection = 0; error = float.PositiveInfinity; } else if (w.x > v.x) { xDirection = 1; totalSteps += (int)normalizedW.x - x; error = (Mathf.Floor(normalizedV.x) + 1 - normalizedV.x) * lineDeltaY; } else { xDirection = -1; totalSteps += x - (int)normalizedW.x; error = (normalizedV.x - Mathf.Floor(normalizedV.x)) * lineDeltaY; } if (lineDeltaY < float.Epsilon) { yDirection = 0; error -= float.PositiveInfinity; } else if (w.y > v.y) { yDirection = 1; totalSteps += (int)normalizedW.y - y; error -= (Mathf.Floor(normalizedV.y) + 1 - normalizedV.y) * lineDeltaX; } else { yDirection = -1; totalSteps += y - (int)normalizedW.y; error -= (normalizedV.y - Mathf.Floor(normalizedV.y)) * lineDeltaX; } for (; totalSteps > 0; --totalSteps) { // Only return values that are inside the grid // we can't clamp the line before hand because that could alter the direction of the line if (x >= 0 && x < grid.Columns && y >= 0 && y < grid.Rows) { yield return(new Vector2Int(x, y)); } if (error > 0) { y += yDirection; error -= lineDeltaX; } else { x += xDirection; error += lineDeltaY; } } }