1

I want to implement DDA and Bresenham's line Algorithm in Qt5. I am using QImage, because I need to turn on, i.e. set to 255, 255, 255, the value of each pixel between a given start and end point.

Code

QImage img(291,231,QImage::Format_ARGB32);  
QRgb rgb=qRgb(255,255,255); // White RGB
img.fill(QColor(Qt::black).rgb());  // Creating a black Background.
int n;
float x_1,y_1;
float x,y;
 if(dx>dy) //dx = x2 - x1 and dy = y2-y1 
     n=dx;
 else
     n=dy;

 x_1=(float)dx/n;
 y_1=(float)dy/n;
 img.setPixel(x1,y1,rgb);
 x=x1;
 y=y1;
 ui->label->setPixmap(QPixmap::fromImage(img));
 for(int i=0;i<n;i++)
 {
     x=x+x_1;
     y=y+y_1;
     img.setPixel(x,y,rgb);
     ui->label->setPixmap(QPixmap::fromImage(img));  // 
 }

To make it look better I want to change the black background to a graph like a 10x10 grid with black background and white separating lines. Then I'll be painting all the point b/w the start and end to white.

Eg - to Draw line b/w (2,2) & (6,6). Every square except at pos (3,3), (4,4), (5,5) which are white will be black.

How I can achieve this?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Shreya
  • 35
  • 5
  • 1
    You can draw with a [QPainter](https://doc.qt.io/qt-5/qpainter.html#QPainter-1) off-screen - [QPixmap](https://doc.qt.io/qt-5/qpixmap.html) is one of these `QPaintDevice`s you may draw into. `QPainter` should provide everything you "dream" of to make a nice looking grid. The alternative might be to use a `QImage` with RGBA values, fill it initially with `(0, 0, 0, 0)` (i.e. alpha = full transparent) and writing pixels with alpha = 255 (to make them solid). So, this `QImage` can be used to overlay a background with what ever you like. – Scheff's Cat Nov 14 '20 at 11:03
  • O.T.: As you mentioned Bresenham - I once did something similar... [SO: Midpoint thick ellipse drawing algorithm](https://stackoverflow.com/a/55983075/7478597) ;-) – Scheff's Cat Nov 14 '20 at 11:13

0 Answers0