Header Ads

CG Program to draw rectangle-diamond pattern

Problem Statement:-

Write C++/Java program to draw the following pattern using any Line drawing algorithms.

Code:-

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<qpicture.h>
#include<qpainter.h>
#include<math.h>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    void bresenham(int x1,int y1,int x2,int y2);
    void dda(int x1,int y1,int x2,int y2);

private:
    Ui::MainWindow *ui;
    QPicture pic;
    QPainter paint;
};

#endif // MAINWINDOW_H

mainwindow.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<qpainter.h>
#include<qpicture.h>
#include<math.h>

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


}

MainWindow::~MainWindow()
{
    delete ui;
}
int sign(float val)
{
    if(val<0)
    {
        return -1;
    }
    else if(val==0)
        return 0;
    else
        return 1;
}
void MainWindow::bresenham(int x1,int y1,int x2,int y2)
{

    int s1,s2,interchange,x,y,i;
    float dx,dy,e;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    x=x1;
    y=y1;
    s1=sign(x2-x1);
    s2=sign(y2-y1);

    if(dx<dy)
    {
        float temp=dx;
        dx=dy;
        dy=temp;
        interchange=1;
    }
    else
        interchange=0;
    i=1;
    e=2*dy-dx;
    while(i<=dx)
    {
        paint.drawPoint(150+x,150-y);
//            if(i==1)
//            {

//                QString sx=QString::number(x);
//                QString sy=QString::number(y);
//                sx="("+sx+" , "+sy+")";
//                paint.drawText(QPoint(150+x+10,150-y+10),sx);
//            }
        while(e>0)
        {
            if(interchange==1)
            {
                x=x+s1;
            }
            else
            {
                y=y+s2;
            }
            e=e-2*dx;
        }
        if(interchange==1)
        {
            y=y+s2;
        }
        else
        {
            x=x+s1;
        }
        e=e+2*dy;
        i++;
    }
//        QString sx=QString::number(x);
//        QString sy=QString::number(y);
//        sx="("+sx+" , "+sy+")";
//        paint.drawText(QPoint(150+x+10,150-y+10),sx);
}
void MainWindow::dda(int x1, int y1, int x2, int y2)
{
    int dx=(x2-x1);
        int dy=(y2-y1);
        int length;
        float x,y;
        if(abs(dx)>=abs(dy))
            length=abs(dx);
        else
            length=abs(dy);

        float xinc=dx/float(length);
        float yinc=dy/float(length);

        x=x1;
        y=y1;
        int i=1;
        while(i<=length)
        {
           // float ty=(y);
    paint.drawPoint(round(x)+150,150-round(y));
          //  img.setPixel(round(x)+150,150-round(y),qRgb(128,15,45));
            x=x+xinc;
            y=y+yinc;
            i++;
        }
}

void MainWindow::on_pushButton_clicked()
{    paint.begin(&pic);
    int x1=ui->textEdit->toPlainText().toInt();
        int y1=ui->textEdit_2->toPlainText().toInt();
        int x2=ui->textEdit_3->toPlainText().toInt();
        int y2=ui->textEdit_4->toPlainText().toInt();

        QBrush brush;
        brush.setColor(Qt::yellow);
        paint.setBrush(brush);
        paint.setPen(Qt::red);
        paint.drawLine(QPoint(150,0),QPoint(150,300));
        paint.drawLine(QPoint(0,150),QPoint(300,150));

        bresenham(x1,y1,x2,y1);
        bresenham(x2,y1,x2,y2);
        bresenham(x1,y1,x1,y2);
        bresenham(x1,y2,x2,y2);

        int xmid,ymid;
        xmid=(x1+x2)/2;
        ymid=(y1+y2)/2;

        dda(xmid,y1,x2,ymid);
         dda(x2,ymid,xmid,y2);
           dda(xmid,y2,x1,ymid);
             dda(x1,ymid,xmid,y1);

int xx1,yy1,xx2,yy2;
xx1=(xmid+x1)/2;
yy1=(ymid+y1)/2;
xx2=(xmid+x2)/2;
yy2=(ymid+y2)/2;

bresenham(xx1,yy1,xx2,yy1);
bresenham(xx2,yy1,xx2,yy2);
bresenham(xx1,yy1,xx1,yy2);
bresenham(xx1,yy2,xx2,yy2);
        paint.end();
        ui->label_5->setPicture(pic);
}



 mainwindow.ui

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?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>665</width>
    <height>386</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>0</x>
      <y>20</y>
      <width>31</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>X1</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>60</y>
      <width>21</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>X2</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>20</y>
      <width>31</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>Y1</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>60</y>
      <width>31</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>Y2</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>20</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_2">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>20</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_3">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>60</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_4">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>60</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>110</y>
      <width>97</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Draw</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>240</x>
      <y>10</y>
      <width>400</width>
      <height>300</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>665</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:-


Will be posted soon. 

No comments:

Powered by Blogger.