0

Im currently having an issue using monogame where I am trying to draw a line by setting values within the data array itself rather than using the mono draw api.

Im using a modified version of the Bresenhams line algorithm, which seems to work if i draw using spritebatch, however I wish to use an array to set pixel colours.

The issue im having is finding the relevant coordinates as my monogame array is 1 Dimensional (width*height):

        var data = new Microsoft.Xna.Framework.Color[width*height];
        //my 1 dimensional array

        int dx = Math.Abs(point2.x - point1.x);
        int dy = Math.Abs(point2.y - point1.y);

        int sx = (point1.x < point2.x) ? 1 : -1;
        int sy = (point1.y < point2.y) ? 1 : -1;

        int err = dx - dy;

        for(int i=1;i<data.Length;++i)
        {

            data[i] = color; // me setting the pixel value


            if (point1.x == point2.x && point1.y == point2.x)
            {
                break;
            }

            int e2 = 2 * err;

            if (e2 > -dy)
            {
                err = err - dy;
                point1.x = point1.x + sx;
            }

            if (e2 < dx)
            {
                err = err + dx;
                point1.y = point1.y + sy;
            }
        }

Is there a simple way to find the pixel that relates to the point1.x and point1.y position where my pixels are contained within a 1D array.

I.E if i have (5*5) or (width*height) sized array (25), whilst looping how do i know whether the current loop counter represents the appropriate x,y position within the 1D array?

I have tried converting it into a 2D array, however i would rather avoid unnecessary loops of converting between a 1D and 2D array.

D3181
  • 2,037
  • 5
  • 19
  • 44

1 Answers1

1

You can check it with

if(i == point.y * width + point.x)
{
    //do something
}
Jakub Dąbek
  • 1,044
  • 1
  • 8
  • 17