1

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.

Scotty
  • 259
  • 1
  • 12
  • So, you want points intersecting a (rotated) rectangle? But in a strange co-ordinate system.. – Blorgbeard Mar 02 '15 at 03:12
  • OK, so your coordinate system is just rectangular, but you render it rotated by 45 degrees? So you could use something like [this](http://stackoverflow.com/questions/2752725/finding-whether-a-point-lies-inside-a-rectangle-or-not)? – Blorgbeard Mar 02 '15 at 03:28
  • Not really. Basically I need a DDA line algorithm that can draw a line with extra thickness. Bresenham, but modified to have thickened lines.. and perhaps variation. – Scotty Mar 02 '15 at 03:43
  • is there a difference between a thick line and a rectangle? it sounds like you want to draw a rectangle around a given set of selected coordinates (rather than determine if a coordinate is inside a rectangle)? – Rufus L Mar 02 '15 at 04:20
  • This won't be used for graphics. Just a trajectory across the above depicted grid. Also I think drawing a rectangle would be too complicated for something which needs to be a quick execution. – Scotty Mar 02 '15 at 15:32

0 Answers0