public static IEnumerable <GridVector> ScaDir(Vector2 start, Vector2 dir, float dist) { GridVector startGrid = new GridVector(start); if (Mathf.Abs(dir.x) >= Mathf.Abs(dir.y)) { foreach (GridVector point in scanX(startGrid, dir, dist)) { yield return(point); } } else { foreach (GridVector point in scanY(startGrid, dir, dist)) { yield return(point); } } }
static IEnumerable <GridVector> scanY(GridVector start, Vector2 dir, float dist) { float ratioXY = Mathf.Abs(dir.x / dir.y); dist /= GRID_SIZE; int disty = Mathf.CeilToInt(dist / Mathf.Sqrt(1 + ratioXY * ratioXY)); int signx = (int)Mathf.Sign(dir.x); int signy = (int)Mathf.Sign(dir.y); for (int y = 0; y <= disty; ++y) { int x = Mathf.RoundToInt(y * ratioXY); GridVector grid = new GridVector(start.x + x * signx, start.y + y * signy); yield return(grid); yield return(grid + GridVector.left); yield return(grid + GridVector.right); } }
static IEnumerable <GridVector> scanX(GridVector start, Vector2 dir, float dist) { float ratioYX = Mathf.Abs(dir.y / dir.x); dist /= GRID_SIZE; int distx = Mathf.CeilToInt(dist / Mathf.Sqrt(1 + ratioYX * ratioYX)); // dist = sqrt(distx^2 + (distx*ratioYX)^2) int signx = (int)Mathf.Sign(dir.x); int signy = (int)Mathf.Sign(dir.y); for (int x = 0; x <= distx; ++x) { int y = Mathf.RoundToInt(x * ratioYX); GridVector grid = new GridVector(start.x + x * signx, start.y + y * signy); yield return(grid); yield return(grid + GridVector.up); yield return(grid + GridVector.down); } }