Assignment 1: Write C++/Java program to draw the following pattern using Line drawing algorithms.
Program:

The Program is created using QTCreator.

And consist of following files:

(click on these link to view)
  • mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

//create global image object
QImage image(551,271,QImage::Format_RGB888);

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

int MainWindow::sign(int x)
{
    if(x<0)
        return -1;
    else if(x==0)
        return 0;
    else
        return 1;
}
void MainWindow::dda(float x1,float y1,float x2,float y2)
{
    float x,y,dx,dy,l,i;

    QRgb value;
    value=qRgb(255,255,255);

    if(abs(x2-x1)>abs(y2-y1))
        l=abs(x2-x1);
    else
        l=abs(y2-y2);

    dx=(x2-x1)/l;
    dy=(y2-y1)/l;

    x=x1+0.5;
    y=y1+0.5;

    i=1;
    do
    {
       image.setPixel(x,y,value);
       x = x + dx;
       y = y + dy;
       i++;
    }while(i <= l);

    ui->label_5->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::bresenham(int x1,int y1,int x2,int y2)
{
    int x,y,dx,dy,e;
    int s1,s2,temp,flag;

    QRgb value;
    value=qRgb(255,255,255);

    x=x1;
    y=y1;

    dx=abs(x2-x1);
    dy=abs(y2-y1);

    s1=sign((x2-x1));
    s2=sign((y2-y1));

    if(dy>dx)
    {
        temp=dy;
        dy=dx;
        dx=temp;
        flag=1;
    }

    e=(2*dy)-dx;

    for(int i=1;i<=dx;i++)
    {
        image.setPixel(x,y,value);

        while(e>0)
        {
            if(flag==1)
                x=x+s1;
            else
                y=y+s2;
            e=e-(2*dx);
        }

        if(flag==1)
            y=y+s2;
        else
            x=x+s1;

        e=e+(2*dy);
    }

    ui->label_5->setPixmap(QPixmap::fromImage(image));

}

void MainWindow::on_pushButton_clicked()
{
    int x,y,height,width;

    x=ui->x->toPlainText().toInt();
    y=ui->y->toPlainText().toInt();
    height=ui->h->toPlainText().toInt();
    width=ui->w->toPlainText().toInt();

    bresenham(x,y,x+width,y);
    bresenham(x,y+height,x+width,y+height);

    int a,b,c,d;

    a=x+(width/2);
    b=y+(height/2);
    c=(a+x+width)/2;
    d=(b+y+height)/2;

     bresenham(c,d,(a+x)/2,d);
     bresenham((x+a)/2,(b+y)/2,c,(y+b)/2);
     bresenham(c,(y+b)/2,c,d);
     bresenham((a+x)/2,d,(x+a)/2,(b+y)/2);

     bresenham(x,y,x,y+height);
     bresenham(x+width,y,x+width,y+height);
}

void MainWindow::on_pushButton_2_clicked()
{
    float x=ui->x->toPlainText().toFloat();
    float y=ui->y->toPlainText().toFloat();
    float height=ui->h->toPlainText().toFloat();
    float width=ui->w->toPlainText().toFloat();

    float t1=x+(width/2);
    float t2=y+(height/2);

    dda(t1,y,x+width,t2);
    dda(x+width,t2,t1,y+height);
    dda(t1,y+height,x,t2);
    dda(x,t2,t1,y);
}
  • main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}
  • mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void dda(float,float,float,float);
    void bresenham(int,int,int,int);
    int sign(int);
    
private slots:


    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
 
  • mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>435</width>
    <height>502</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>31</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>X</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>10</y>
      <width>46</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>Y</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="x">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>0</y>
      <width>81</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="y">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>0</y>
      <width>81</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>50</y>
      <width>31</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Height</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>50</y>
      <width>46</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>Width</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="h">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>40</y>
      <width>81</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="w">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>40</y>
      <width>81</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>170</y>
      <width>371</width>
      <height>281</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>90</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Rectangle</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>90</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Polygon</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>435</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>
 
 
 
Output:



 

Comments

Post a Comment

Popular posts from this blog