Bug in Image rendering algorithm?


I found a strange behaviour in the Graphics drawLine method. In the
following example, the four diagonal Pixels are red.
The other diagonal line

gg.drawLine(0.0f, 0.0f, 4.0f, 4.0f);

works as I would expect it.


[code]void MainComp::paint (Graphics& g)
g.fillAll (Colours::white);

  Image myImage (Image::RGB, 4, 4, true);
  Graphics gg (myImage);
  gg.setColour (Colours::red);

  gg.drawLine(0.0f, 3.0f, 4.0f, -1.0f); 
  // gg.drawLine(0.0f, 4.0f, 4.0f, 0.0f); does not end at the corners

  g.drawImage(&myImage, 1, 1, 4, 4, 0, 0, 4, 4); 



No, this isn’t a bug, you’re just getting muddled.

The co-ords refer to the top-left of the pixels you’re drawing, so of course your other line is off the edge by one pixel, because you’re telling it to start drawing beyond the edges of the image.


Hi Jules,
thanx for your quick response.

I’m not shure if I understand. Maybe i make an

gg.drawLine(0,3, 3,0);

startX/startY mark the bottom left pixel,
and endX/endY mark the top right pixel.
Only one of these two pixels is drawn.
If you swap the start values and the end
values, then the result allways remains
the same.

For this example the top right pixel is
not drawn, because endY<startY.

And if endY==startY, as in
gg.drawLine(0,1, 3,1);
then the pixel with the higher X-value (3,1)
is not drawn.

Is that right? Sorry if I bother you.


It’s only an approximation - the line-drawing algorithm is designed to be fast, not to guarantee which pixels get filled. And if I eventually add support for other hardware-assisted rendering, there’s even less guarantee that it would always behave the same way.

If for some reason it actually matters exactly which pixels get drawn, just use fillRect to set each one. Or better: create a Path that covers the area, as that will always be rendered with sub-pixel accuracy.


Could you make drawWaveform then?

I’m using drawLine for that, but every time the wave goes up and to the right there’s a gap.

Also it’s however many bazillion function calls. It isn’t really slow, but any speedup is a good thing.


Dave Heinemann


Have you thought about using drawVerticalLine to do waveforms? That’s how I always do them.