<p>I don't know, why you consider your first approach hacky, but an alternative would be to analyze the shape of your brush and how it interacts with your underlying data structure. For example for your rectangle:</p>
<p>Let's assume the brush's sides are parallel to the sides of the image. Then you can calculate how man pixels are covered by your offset <code>d</code>. Let's call be the number of pixels between two corners <code>n</code>. Then you can determine the index of the upper left pixel of your rectangle and fill the next <code>n</code> indices since they represent the left border of your rectangle. Then add <code>h</code> to the index of the upper left point to get to the pixel right of it. Fill the next <code>n</code> pixels. Repeat this <code>n</code> times and you have finished filling your rectangle. Here is some untested code that should demonstrate the approach:</p>
<pre class="lang-cpp prettyprint-override"><code>float left_x = cursorNorm.x - d;
float upper_y = cursorNorm.y - d;
float lower_y = cursorNorm.y + d;

int index_upper_left = getPixelIndex(left_x, upper_y, w, h);
int index_lower_left = getPixelIndex(left_x, lower_y, w, h);

int n = index_lower_left - index_upper_left;

for (int i=0; i &lt; n; ++i)
    for (int j=0; j &lt; n; ++j)
        int index = index_upper_left + j*h + i
        pixels[index] = {255, 0, 0, 255};
</code></pre>
<p>Since this is all symmetric, you can vary this to start at the center point, run i and j just up to <code>n/2</code> and do something like that:</p>
<pre class="lang-cpp prettyprint-override"><code>for (int i=0; i &lt; n/2; ++i)
    for (int j=0; j &lt; n/2; ++j)
        int index_left = index_center - j*h
        int index_right = index_center + j*h

        pixels[index_left + i] = {255, 0, 0, 255};
        pixels[index_left - i] = {255, 0, 0, 255};
        pixels[index_right + i] = {255, 0, 0, 255};
        pixels[index_right - i] = {255, 0, 0, 255};
</code></pre>
<p>More complex shapes like circles get a little bit more complicated but you can calculate for each affected column of pixels the index of the upper and lower affected pixel and fill between them. Remember that you can abuse symmetry here. Meaning if you found the upper pixel of a column, the lower pixel has the same y distance to the y-value of the center point. Additionally, the bounds to the left and to the right of the center are also identical. A helpful algorithm to find the bounds might be <a href="https://www.geeksforgeeks.org/bresenhams-circle-drawing-algorithm/" rel="nofollow noreferrer">Bresenham’s circle drawing algorithm</a>.</p>

CC BY-SA 4.0
2020-08-02T14:31:33.710Z
2020-08-02T14:31:33.710Z
wychmaster
<p>I'm trying to create a simple painting application. And I was wondering about an efficient way to draw pixels on the screen.</p>
<p>I have a pixel struct:</p>
<pre><code>struct Pixel {
 unsigned char r, g, b, a;
};
</code></pre>
<p>And I have two methods for converting between pixel coordinates and normalized pixel coordinates. <code>w</code> and <code>h</code> are image width and height respectively.</p>
<pre><code>int getPixelIndex(float2 norm, int w, int h) {
   return (int)(norm.x*h)*w + norm.y*h;
}

float2 getPixelNormCoords(int index, int w, int h) {
   float2 norm;
   norm.x = (index % w) / (float)(w-1);
   norm.y = (index / h) / (float)(h-1);
   return norm;
}
</code></pre>
<p>Now if I want to draw a red pixel under the cursor I could do it like this:</p>
<pre><code>int pixelIndex = getPixelIndex(cursorNorm.x, cursorNorm.y, w, h);

pixels[pixelIndex] = {255, 0, 0, 255};
</code></pre>
<p>However what if I want to fill more pixels around the cursor, to create different brushes. Say I want to create a square brush with a given offset <code>d</code>. One hacky way to do this would be:</p>
<pre><code>float delta = 10e-4;
for (float i = -d; i &lt; d; i+=delta) {
  for (float j = -d; j &lt; d; j+=delta) {
    int pixelIndex = getPixelIndex(cursorNorm.x+i, cursorNorm.y+j, w, h);
    pixels[pixelIndex] = {255, 0, 0, 255};
  }
}
</code></pre>
<p>Another way would be to iterate over each pixel:</p>
<pre><code>for (int i = 0; i &lt; size(pixels); i++) {
   float2 norm = getPixelNormCoords(i, w, h);

   if (fabs(norm.x-cursorNorm.x) &lt; d &amp;&amp; fabs(norm.y-cursorNorm.y) &lt; d)
      pixels[i] = {255, 0, 0, 255};
}
</code></pre>
<p>However this way seems to be extremely costly.</p>
<p>Are there more efficient and less hacky methods to accomplish what I'm trying to do?</p>

<c++><image><pixels>
Paint pixels on the image around the cursor for painting application
Lenny White
# Paint pixels on the image around the cursor for painting application

(Asked by: username_0 on 2020-08-02)

I'm trying to create a simple painting application. And I was wondering about an efficient way to draw pixels on the screen.

I have a pixel struct:

```
struct Pixel {
 unsigned char r, g, b, a;
};

```

And I have two methods for converting between pixel coordinates and normalized pixel coordinates. `w` and `h` are image width and height respectively.

```
int getPixelIndex(float2 norm, int w, int h) {
   return (int)(norm.x*h)*w + norm.y*h;
}

float2 getPixelNormCoords(int index, int w, int h) {
   float2 norm;
   norm.x = (index % w) / (float)(w-1);
   norm.y = (index / h) / (float)(h-1);
   return norm;
}

```

Now if I want to draw a red pixel under the cursor I could do it like this:

```
int pixelIndex = getPixelIndex(cursorNorm.x, cursorNorm.y, w, h);

pixels[pixelIndex] = {255, 0, 0, 255};

```

However what if I want to fill more pixels around the cursor, to create different brushes. Say I want to create a square brush with a given offset `d`. One hacky way to do this would be:

```
float delta = 10e-4;
for (float i = -d; i < d; i+=delta) {
  for (float j = -d; j < d; j+=delta) {
    int pixelIndex = getPixelIndex(cursorNorm.x+i, cursorNorm.y+j, w, h);
    pixels[pixelIndex] = {255, 0, 0, 255};
  }
}

```

Another way would be to iterate over each pixel:

```
for (int i = 0; i < size(pixels); i++) {
   float2 norm = getPixelNormCoords(i, w, h);

   if (fabs(norm.x-cursorNorm.x) < d && fabs(norm.y-cursorNorm.y) < d)
      pixels[i] = {255, 0, 0, 255};
}

```

However this way seems to be extremely costly.

Are there more efficient and less hacky methods to accomplish what I'm trying to do?

**Tags:** c++, image, pixels


---

## 1 answers

### Answer by username_1 on 2020-08-02. Score: 1 | Accepted answer

I don't know, why you consider your first approach hacky, but an alternative would be to analyze the shape of your brush and how it interacts with your underlying data structure. For example for your rectangle:

Let's assume the brush's sides are parallel to the sides of the image. Then you can calculate how man pixels are covered by your offset `d`. Let's call be the number of pixels between two corners `n`. Then you can determine the index of the upper left pixel of your rectangle and fill the next `n` indices since they represent the left border of your rectangle. Then add `h` to the index of the upper left point to get to the pixel right of it. Fill the next `n` pixels. Repeat this `n` times and you have finished filling your rectangle. Here is some untested code that should demonstrate the approach:

```cpp
float left_x = cursorNorm.x - d;
float upper_y = cursorNorm.y - d;
float lower_y = cursorNorm.y + d;

int index_upper_left = getPixelIndex(left_x, upper_y, w, h);
int index_lower_left = getPixelIndex(left_x, lower_y, w, h);

int n = index_lower_left - index_upper_left;

for (int i=0; i < n; ++i)
    for (int j=0; j < n; ++j)
        int index = index_upper_left + j*h + i
        pixels[index] = {255, 0, 0, 255};

```

Since this is all symmetric, you can vary this to start at the center point, run i and j just up to `n/2` and do something like that:

```cpp
for (int i=0; i < n/2; ++i)
    for (int j=0; j < n/2; ++j)
        int index_left = index_center - j*h
        int index_right = index_center + j*h

        pixels[index_left + i] = {255, 0, 0, 255};
        pixels[index_left - i] = {255, 0, 0, 255};
        pixels[index_right + i] = {255, 0, 0, 255};
        pixels[index_right - i] = {255, 0, 0, 255};

```

More complex shapes like circles get a little bit more complicated but you can calculate for each affected column of pixels the index of the upper and lower affected pixel and fill between them. Remember that you can abuse symmetry here. Meaning if you found the upper pixel of a column, the lower pixel has the same y distance to the y-value of the center point. Additionally, the bounds to the left and to the right of the center are also identical. A helpful algorithm to find the bounds might be [Bresenham’s circle drawing algorithm](https://www.geeksforgeeks.org/bresenhams-circle-drawing-algorithm/).


---

