Friday, April 18, 2014

Scan line polygon filling in QT Creator C++

mainwindow.h file:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QImage>
#include<QRgb>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    void dda(float,float,float,int ,int ,int ,int ,QRgb);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    int n;
    int t;
    int xp;
    int yp;
    int xa[100];
    int ya[100];
    QImage k;
private slots: /*
    void next();
    void draw(); Functions
    void an();
    void check(); */
};

#endif // MAINWINDOW_H
--------------------------------------------------------------

mainwindow.cpp file:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPixmap>
#include<QRgb>
#include<QImage>
void MainWindow::dda(float m,float x,float y, int x0,int y0,
int x1,int y1,QRgb c)
{
    if(x0<=x1 && y0<=y1)
    {
    if(m<=1 && m>=-1)
    {
        while(x<x1)
        {
            y=y+m;
            x=x+1;
            k.setPixel(x,y,c); //setPixel
        }
    }
    else
    {
        while(y<y1)
        {
            y=y+1;
            x=x+1/m;
            k.setPixel(x,y,c); //setPixel
        }
    }
    }
    else if(x1<=x0 && y1<=y0)
    {
    if(m<=1 && m>=-1)
    {
        while(x>x1)
        {
            y=y-m;
            x=x-1;
            k.setPixel(x,y,c); //setPixel
        }
    }
    else
    {
        while(y>y1)
        {
            y=y-1;
            x=x-1/m;
            k.setPixel(x,y,c); //setPixel
        }
    }
    }
    else if(x0<=x1 && y1<=y0)
    {
    if(m<=1 && m>=-1)
    {
        while(x<x1)
        {
            y=y+m;
            x=x+1;
            k.setPixel(x,y,c); //setPixel
        }
    }
    else
    {
        while(y>y1)
        {
            y=y-1;
            x=x-(1/m);
            k.setPixel(x,y,c); //setPixel
        }
    }
    }
    else if(x1<=x0 && y0<=y1)
    {
    if(m<=1 && m>=-1)
    {
        while(x>x1)
        {
            y=y-m;
            x=x-1;
            k.setPixel(x,y,c); //setPixel
        }
    }
    else
    {
        while(y<y1)
        {
            y=y+1;
            x=x+1/m;
            k.setPixel(x,y,c); //setPixel
        }
    }
    }
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    n=0;
    t=0;
    QImage l(500,500,QImage::Format_ARGB32);
    k=l;
  connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(an()));
  connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(next()));
  connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(draw()));
  connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(check()));
}                        //Function connects

void MainWindow::an()
{
    n=ui->lineEdit->text().toInt();
    ui->lineEdit->hide();
    ui->label->hide();
}

void MainWindow::next()
{
    int x0,x1,y0,y1;
    x0=ui->lineEdit_2->text().toInt();
    y0=ui->lineEdit_3->text().toInt();
    x1=ui->lineEdit_4->text().toInt();
    y1=ui->lineEdit_5->text().toInt();
    xa[t]=x0;
    ya[t]=y0;
    QString j;
    j=QString::number(x1,'g',6);
    ui->lineEdit_2->setText(j);
    j=QString::number(y1,'g',6);
    ui->lineEdit_3->setText(j);
    ui->lineEdit_4->setText("");
    ui->lineEdit_5->setText("");
    if(t==0)
    {
        xp=x0;
        yp=y0;
    }
    QRgb c;
    c=qRgb(255,0,0); RGB value
    float m,x,y;
    m=y1-y0;
    m=m/(x1-x0);
    x=x0;
    y=y0;
    dda(m,x,y,x0,y0,x1,y1,c);
    ui->label_6->setPixmap(QPixmap::fromImage(k));
   // ui->label_6->show();
    t++;
    if(t>n-2)
    {
        m=yp-y1;
        m=m/(xp-x1);
        x=x1;
        y=y1;
        xa[n-1]=x1;
        ya[n-1]=y1;
        dda(m,x,y,x1,y1,xp,yp,c);
        ui->pushButton->hide();
    }
}

void MainWindow::draw() //Draw() Function
{
    ui->label_6->setPixmap(QPixmap::fromImage(k));
    ui->label_6->show();
}

void MainWindow::check() //check() Function
{
    QRgb c;
    c=qRgb(255,0,0);
    int xc[20];
    int count=0;
    for(int i=1;i<100;i++)
    {
        for(int j=0;j<500;j++)
        {
            if(k.pixel(j,i)==c)
            {
                xc[count]=j;
                count++;
                for(int u=0;u<n;u++)
                {
                    if(j==xa[u] && i==ya[u])
                    {
                        xc[count]=j;
                        count++;
                    }
                }
            }
        }
        for(int j=0;j<count;j=j+2)
        {
            dda(0,xc[j],i,xc[j],i,xc[j+1],i,c);
        }
        count=0;
    }
     ui->label_6->setPixmap(QPixmap::fromImage(k));
     ui->label_6->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

Output







2 comments:

  1. don't get even after removing several comments in function declartion

    ReplyDelete
  2. How can i run this and make the rectangle show? i tried to click it but the rectangle didn't show

    ReplyDelete

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...