I have a coordinate grid in a 2D game. I need to, on a line with variable width, find multiple targets for a projectile of a particular size.
The coordinates are absolute integer values.
In my grid, and for my situation, it's different from a typical X and Y axis. For north, it's X-1, Y-1. South, X+1, Y+1. A diamond pattern.
0,0
0,1 1,1 1,0
0,2 1,2 2,2 2,1 2,0
0,3 1,3 2,3 3,3 3,2 3,1 3,0
etc...
There are no negative coordinates.
Here is a visual example of what I hope to accomplish. The two black lines represent an individual target area of individual collections, each of the red dots represent an object which needs to be verified as an object within a black line.
This is the original functional code that I want to adapt or replace. The collection of coordinates it makes, in a line, is of a fixed width. I need something that can make a wider line of targeted coordinates when needed.
List<coords> line(int xa, int ya, int xb, int yb)
{
int dx = xb - xa, dy = yb - ya, steps, k;
float xincrement, yincrement, x = xa, y = ya;
if (Math.Abs(dx) > Math.Abs(dy)) steps = Math.Abs(dx);
else steps = Math.Abs(dy);
xincrement = dx / (float)steps;
yincrement = dy / (float)steps;
var thisLine = new List<coords> {new coords(Math.Round(x), Math.Round(y))};
for (k = 0; k < MaxDistance; k++)
{
x += xincrement;
y += yincrement;
thisLine.Add(new coords(Math.Round(x), Math.Round(y)));
}
return thisLine;
}
Any answer should keep in mind that the collection is time sensitive and that performance is important since this will be used in a server environment.