Draw Lines using DDA and Bresenham's Line Drawing Algorithm [CG]
Problem Statement:
Draw Lines using DDA and Bresenham's Line Drawing Algorithm [CG]
Code:
mainwindow.cpp
mainwindow.ui
Output:-
Draw Lines using DDA and Bresenham's Line Drawing Algorithm [CG]
Code:
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 162 163 164 | #include "mainwindow.h" #include "ui_mainwindow.h" #include<math.h> #include<qpicture.h> #include<qpainter.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::on_pushButton_clicked() { 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(); QPicture pic; QPainter paint; paint.begin(&pic); 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)); /* for(int j=0;j<300;j++) { paint.drawPoint(150,j); paint.drawPoint(j,150); } */ 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); paint.end(); ui->label_5->setPicture(pic); } void MainWindow::on_pushButton_2_clicked() { 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(); QPicture pic; QPainter paint; paint.begin(&pic); paint.drawLine(QPoint(150,0),QPoint(150,300)); paint.drawLine(QPoint(0,150),QPoint(300,150)); 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; QString sx=QString::number(x); QString sy=QString::number(y); sx="("+sx+" , "+sy+")"; paint.drawText(QPoint(150+x+10,150-y+10),sx); 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++; } sx=QString::number(round(x)); sy=QString::number(round(y)); sx="("+sx+" , "+sy+")"; paint.drawText(QPoint(150+x+10,150-y+10),sx); 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | <?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>712</width> <height>453</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>10</x> <y>100</y> <width>31</width> <height>21</height> </rect> </property> <property name="text"> <string>X2</string> </property> </widget> <widget class="QTextEdit" name="textEdit_2"> <property name="geometry"> <rect> <x>40</x> <y>70</y> <width>104</width> <height>21</height> </rect> </property> </widget> <widget class="QLabel" name="label_5"> <property name="geometry"> <rect> <x>180</x> <y>20</y> <width>450</width> <height>350</height> </rect> </property> <property name="text"> <string/> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>10</x> <y>40</y> <width>31</width> <height>21</height> </rect> </property> <property name="text"> <string>X1</string> </property> </widget> <widget class="QLabel" name="label_3"> <property name="geometry"> <rect> <x>10</x> <y>70</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>10</x> <y>130</y> <width>31</width> <height>21</height> </rect> </property> <property name="text"> <string>Y2</string> </property> </widget> <widget class="QTextEdit" name="textEdit_3"> <property name="geometry"> <rect> <x>40</x> <y>100</y> <width>104</width> <height>21</height> </rect> </property> </widget> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>40</x> <y>170</y> <width>97</width> <height>31</height> </rect> </property> <property name="text"> <string>Bresenham</string> </property> <property name="checkable"> <bool>false</bool> </property> </widget> <widget class="QTextEdit" name="textEdit_4"> <property name="geometry"> <rect> <x>40</x> <y>130</y> <width>104</width> <height>21</height> </rect> </property> </widget> <widget class="QTextEdit" name="textEdit"> <property name="geometry"> <rect> <x>40</x> <y>40</y> <width>104</width> <height>21</height> </rect> </property> </widget> <widget class="QPushButton" name="pushButton_2"> <property name="geometry"> <rect> <x>40</x> <y>220</y> <width>97</width> <height>31</height> </rect> </property> <property name="text"> <string>DDA</string> </property> </widget> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>712</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:-
Bresenhams Output |
DDA's Output |
Can you upload rest of the CGs code?
ReplyDeletesorry . I have not completed those. Will Upload as soon as possible. In this week.
Delete