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?