Q:Write a program in C++ to draw a line with different styles thick, thin and dotted. Accept the start point and end point coordinates from the user.
Framework used:- QT
Platfrom :-Cross-platfrom(Linux/Windows)
The algorithm used to draw the line is dda algorithm
Code:-
(For code for other classes and files. See download link below)
mainwindow.cpp
Framework used:- QT
Platfrom :-Cross-platfrom(Linux/Windows)
The algorithm used to draw the line is dda algorithm
Code:-
(For code for other classes and files. See download link below)
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <algorithm> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } inline int MainWindow::sign(float num){ return ((num<0)?-1:(num>0)); } void MainWindow::on_pushButton_2_clicked() //code for thin line { int x1=ui->lineEdit->text().toInt(); int y1=ui->lineEdit_2->text().toInt(); int x2=ui->lineEdit_3->text().toInt(); int y2=ui->lineEdit_4->text().toInt(); QImage image(500,500,QImage::Format_RGB888); QRgb value; value=qRgb(0,255,0); int length=std::max(abs(x2-x1),abs(y2-y1)); float dx=(x2-x1)/(float)length; float dy=(y2-y1)/(float)length; float x=x1+0.5*sign(dx); float y=y1+0.5*sign(dy); int i=1; for(i=1;i<=length;i++){ image.setPixel(int(x),int(y),value); x=dx+x; y=y+dy; } ui->label_5->setPixmap(QPixmap::fromImage(image)); } void MainWindow::on_pushButton_4_clicked() //code for dotted line { int x1=ui->lineEdit->text().toInt(); int y1=ui->lineEdit_2->text().toInt(); int x2=ui->lineEdit_3->text().toInt(); int y2=ui->lineEdit_4->text().toInt(); QImage image(500,500,QImage::Format_RGB888); QRgb value; value=qRgb(0,255,0); int length=std::max(abs(x2-x1),abs(y2-y1)); float dx=(x2-x1)/(float)length; float dy=(y2-y1)/(float)length; float x=x1+0.5*sign(dx); float y=y1+0.5*sign(dy); int i=1; for(i=1;i<=length;i=i+2){ image.setPixel(int(x),int(y),value); x=dx*2+x; y=y+dy*2; } ui->label_5->setPixmap(QPixmap::fromImage(image)); } void MainWindow::on_pushButton_3_clicked() { int x1=ui->lineEdit->text().toInt(); int y1=ui->lineEdit_2->text().toInt(); int x2=ui->lineEdit_3->text().toInt(); int y2=ui->lineEdit_4->text().toInt(); QImage image(500,500,QImage::Format_RGB888); QRgb value; value=qRgb(0,200,0); int length=std::max(abs(x2-x1),abs(y2-y1)); float dx=(x2-x1)/(float)length; float dy=(y2-y1)/(float)length; float x=x1+0.5*sign(dx); float y=y1+0.5*sign(dy); int i=1; int t,t2; for(i=1;i<=length;i++){ for(t=-1;t<=1;t++){ for(t2=-1;t2<=1;t2++){ image.setPixel(int(x+t),int(y+t2),value); } } x=dx+x; y=y+dy; } ui->label_5->setPixmap(QPixmap::fromImage(image)); } void MainWindow::on_pushButton_clicked() { ui->lineEdit->setText(""); ui->lineEdit_2->setText(""); ui->lineEdit_3->setText(""); ui->lineEdit_4->setText(""); QImage image(500,500,QImage::Format_RGB888); ui->label_5->setPixmap(QPixmap::fromImage(image)); }
For all the files including screenshot related to this code click here:- mediafire
No comments:
Post a Comment