3

The issue of programmatically drawing lines using XNA has been covered here. However, I want to allow a user to draw on a canvas as one would with a drawing app such as MS Paint.

This of course requires each x and/or y coordinate change in the mouse pointer position to result in another "dot" of the line being drawn on the canvas in the crayon color in real time.

In the mouse move event, what XNA API considerations come into play in order to draw the line point by point? Literally, of course, I'm not drawing a line as such, but rather a sequence of "dots". Each "dot" can, and probably should, be larger than a single pixel. Think of drawing with a felt tip pen.

Community
  • 1
  • 1
Buggieboy
  • 4,636
  • 4
  • 55
  • 79

1 Answers1

7

The article you provided suggests a method of drawing lines with primitives; vector graphics, in other words. Applications like Paint are mostly pixel based (even though more advanced software like Photoshop has vector and rasterization features).

Bitmap editor

Since you want it to be "Paint-like" I would definitely go with the pixel based approach:

  1. Create a grid of color values. (Extend the System.Drawing.Bitmap class or implement your own.)
  2. Start the (game) loop:
    • Process input and update the color values in the grid accordingly.
    • Convert the Bitmap to a Texture2D.
    • Use a sprite batch or custom renderer to draw the texture to the screen.
  3. Save the bitmap, if you want.

Drawing on the bitmap

I added a rough draft of the image class I am using here at the bottom of the answer. But the code should be quite self-explanatory anyways.

As mentioned before you also need to implement a method for converting the image to a Texture2D and draw it to the screen.


First we create a new 10x10 image and set all pixels to white.

var image = new Grid<Color>(10, 10);
image.Initilaize(() => Color.White);

White 10*10 pixel grid

Next we set up a brush. A brush is in essence just a function that is applied on the whole image. In this case the function should set all pixels inside the specified circle to a dark red color.

// Create a circular brush
float brushRadius = 2.5f;
int brushX = 4;
int brushY = 4;
Color brushColor = new Color(0.5f, 0, 0, 1); // dark red

White 10*10 color grid with a circle and cross defining the brush area and center

Now we apply the brush. See this SO answer of mine on how to identify the pixels inside a circle. You can use mouse input for the brush offsets and enable the user to actually draw on the bitmap.

double radiusSquared = brushRadius * brushRadius;

image.Modify((x, y, oldColor) =>
{
    // Use the circle equation
    int deltaX = x - brushX;
    int deltaY = y - brushY;
    double distanceSquared = Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2); 

    // Current pixel lies inside the circle
    if (distanceSquared <= radiusSquared)
    {
        return brushColor;
    }

    return oldColor;
});

White 10*10 color grid with all pixels inside the circle set to a dark red

You could also interpolate between the brush color and the old pixel. For example, you can implement a "soft" brush by letting the blend amount depend on the distance between the brush center and the current pixel.

White 10*10 color grid with a soft dark red dot

Drawing a line

In order to draw a freehand line simply apply the brush repeatedly, each time with a different offset (depending on the mouse movement):

Drawing a line by repeatedly applying a circular brush


Custom image class

I obviously skipped some necessary properties, methods and data validation, but you get the idea:

public class Image
{
    public Color[,] Pixels { get; private set; }

    public Image(int width, int height)
    {
        Pixels= new Color[width, height];
    }

    public void Initialize(Func<Color> createColor)
    {
         for (int x = 0; x < Width; x++)
         {
             for (int y = 0; y < Height; y++)
             {
                  Pixels[x, y] = createColor();
             }
         }
    }

    public void Modify(Func<int, int, Color, Color> modifyColor)
    {
         for (int x = 0; x < Width; x++)
         {
             for (int y = 0; y < Height; y++)
             {
                  Color current = Pixels[x, y];
                  Pixels[x, y] = modifyColor(x, y, current);
             }
         }
    }
}
Community
  • 1
  • 1
Lucius
  • 3,705
  • 2
  • 22
  • 41
  • 1
    Good answer - although I think it would probably be better to work on a `Color[]` buffer that you can pass directly to `Texture2D.GetData`/`SetData`, rather than having the extra conversion step in there from a 2D array or a `Bitmap`. – Andrew Russell Jan 30 '13 at 02:39
  • 1
    Thank you! I chose a 2D array because I think it is more intuitive. Luckily the interface would not change much if it was replaced with a simple array. The loops in the `Image` methods would just flatten out to a single loop and the offsets would have to be calculated: `int x = i / Height;` and `int y = i % Height;`. – Lucius Jan 30 '13 at 02:54
  • 1
    Lucius, this is outstanding. Thanks so much. – Buggieboy Jan 31 '13 at 21:49