Assignment 3: Write C++/Java program for line drawing using DDA or Bresenhams algorithm with patterns such as solid, dotted, dash dot and thick.

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"
#include<math.h>

QImage image(511, 351, QImage::Format_RGB888);

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

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


int MainWindow:: sign(float a)
{
    if(a<0)
        return -1;
    else if(a==0)
        return 0;
    else
        return 1;
}

QImage getI()
{
    return image;
}

void MainWindow::on_pushButton_clicked()
{
    float x1,y1,x2,y2;

    x1=ui->X1->toPlainText().toFloat();
    y1=ui->Y1->toPlainText().toFloat();
    x2=ui->X2->toPlainText().toFloat();
    y2=ui->Y2->toPlainText().toFloat();

    float l,del_y,del_x,temp1,temp2,x,y;

    QRgb value;

    temp1=x2-x1;
    temp2=y2-y1;

    if(abs(temp1)>=abs(temp2))
          l=abs(temp1);
    else
          l=abs(temp2);

    del_x=temp1/l;
    del_y=temp2/l;

    x=x1+0.5*sign(del_x);
    y=y1+0.5*sign(del_y);


    value=qRgb(255,255,255);
    for(int i=0;i<=l;i++)
    {
         image.setPixel(int(x),int(y),value);


         x=x+del_x;
         y=y+del_y;
    }

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

}

void MainWindow::on_pushButton_2_clicked()
{
    float x1,y1,x2,y2;

    x1=ui->X1->toPlainText().toFloat();
    y1=ui->Y1->toPlainText().toFloat();
    x2=ui->X2->toPlainText().toFloat();
    y2=ui->Y2->toPlainText().toFloat();

    float l,del_y,del_x,temp1,temp2,x,y;

    QRgb value;

    temp1=x2-x1;
    temp2=y2-y1;

    if(abs(temp1)>=abs(temp2))
          l=abs(temp1);
    else
          l=abs(temp2);

    del_x=temp1/l;
    del_y=temp2/l;

    x=x1+0.5*sign(del_x);
    y=y1+0.5*sign(del_y);


    value=qRgb(255,255,255);
    for(int i=0;i<=l;i++)
    {
        if(i%2==0)
                  image.setPixel(int(x)+10,int(y),value);
         x=x+del_x;
         y=y+del_y;
    }

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

void MainWindow::on_pushButton_3_clicked()
{

    float x1,y1,x2,y2;

    x1=ui->X1->toPlainText().toFloat();
    y1=ui->Y1->toPlainText().toFloat();
    x2=ui->X2->toPlainText().toFloat();
    y2=ui->Y2->toPlainText().toFloat();

    float l,del_y,del_x,temp1,temp2,x,y;

    QRgb value;

    temp1=x2-x1;
    temp2=y2-y1;

    if(abs(temp1)>=abs(temp2))
          l=abs(temp1);
    else
          l=abs(temp2);

    del_x=temp1/l;
    del_y=temp2/l;

    x=x1+0.5*sign(del_x);
    y=y1+0.5*sign(del_y);


    value=qRgb(255,255,255);
    for(int i=0;i<=l;i++)
    {
        if(i%7!=0 && i%7!=1)
                  image.setPixel(int(x)+20,int(y),value);

         x=x+del_x;
         y=y+del_y;
    }

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

void MainWindow::on_pushButton_4_clicked()
{
    float x1,y1,x2,y2;
    int mod1=12;
    int mod2=16;
    int cnt=0;

    x1=ui->X1->toPlainText().toFloat();
    y1=ui->Y1->toPlainText().toFloat();
    x2=ui->X2->toPlainText().toFloat();
    y2=ui->Y2->toPlainText().toFloat();

    float l,del_y,del_x,temp1,temp2,x,y;

    QRgb value;

    temp1=x2-x1;
    temp2=y2-y1;

    if(abs(temp1)>=abs(temp2))
          l=abs(temp1);
    else
          l=abs(temp2);

    del_x=temp1/l;
    del_y=temp2/l;

    x=x1+0.5*sign(del_x);
    y=y1+0.5*sign(del_y);


    value=qRgb(255,255,255);
    for(int i=0;i<=l;i++)
    {
        if(! ((cnt==mod1) || (cnt==mod1+1) || (cnt==mod1+2) )
            && !( (cnt==mod2) || (cnt==mod2+1) || (cnt==mod2+2) )   )
           {
                 image.setPixel(int(x)+30,int(y),value);
           }

         x=x+del_x;
         y=y+del_y;

         if(cnt==18)
                cnt=0;
         cnt++;
    }

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

}

void MainWindow::on_pushButton_5_clicked()
{

    float x1,y1,x2,y2;

    x1=ui->X1->toPlainText().toFloat();
    y1=ui->Y1->toPlainText().toFloat();
    x2=ui->X2->toPlainText().toFloat();
    y2=ui->Y2->toPlainText().toFloat();

    float l,del_y,del_x,temp1,temp2,x,y;

    QRgb value;

    temp1=x2-x1;
    temp2=y2-y1;

    if(abs(temp1)>=abs(temp2))
          l=abs(temp1);
    else
          l=abs(temp2);

    del_x=temp1/l;
    del_y=temp2/l;

    x=x1+0.5*sign(del_x);
    y=y1+0.5*sign(del_y);


    value=qRgb(255,255,255);
    for(int i=0;i<=l;i++)
    {
         image.setPixel(int(x)+40,int(y),value);
         image.setPixel(int(x+1)+40,int(y),value);
         image.setPixel(int(x+2)+40,int(y),value);
         image.setPixel(int(x+3)+40,int(y),value);

         x=x+del_x;
         y=y+del_y;
    }

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

 
  • 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();
    QImage getI();
    void dda_line(float,float,float,float);

    int sign(float);
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_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>831</width>
    <height>683</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>70</y>
      <width>97</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Solid</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>70</y>
      <width>97</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Dotted</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>320</x>
      <y>70</y>
      <width>97</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Dashed</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>20</y>
      <width>31</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>X1</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="X1">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>10</y>
      <width>71</width>
      <height>41</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>20</y>
      <width>31</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>Y1</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="Y1">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>10</y>
      <width>71</width>
      <height>41</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_4">
    <property name="geometry">
     <rect>
      <x>460</x>
      <y>70</y>
      <width>97</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Dotted Dash</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_5">
    <property name="geometry">
     <rect>
      <x>600</x>
      <y>70</y>
      <width>97</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Thick</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>130</y>
      <width>511</width>
      <height>351</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QTextEdit" name="X2">
    <property name="geometry">
     <rect>
      <x>330</x>
      <y>10</y>
      <width>71</width>
      <height>41</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>430</x>
      <y>20</y>
      <width>31</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>Y2</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="Y2">
    <property name="geometry">
     <rect>
      <x>470</x>
      <y>10</y>
      <width>71</width>
      <height>41</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>20</y>
      <width>31</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>X2</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>831</width>
     <height>27</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