circle.h file:
#ifndef CIRCLE_H
#define CIRCLE_H
#include <QMainWindow>
#include "ui_circle.h"
namespace Ui {
class circle;
}
class circle : public QMainWindow
{
Q_OBJECT
public:
explicit circle(QWidget *parent = 0);
~circle();
private:
Ui::circle *ui;
private slots:
void drawcircle();
};
#endif
___________________________________________________________
circle.cpp file:
#include "circle.h"
#include "ui_circle.h"
#include"math.h"
class line //class Line
{
public:
QImage draw(int x1,int y1,int x2,int y2, QImage img)
{
int dx,dy;
int g;
if(x2>x1)
dx=x2-x1;
else
dx=x1-x2;
if(y2>y1)
dy=y2-y1;
else
dy=y1-y2;
if(dx>=dy)
{
g=(2*dy)-dx;
while(x1!=x2)
{
if(g>0)
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
g=g+(2*(dy-dx));
img.setPixel(x1,y1,qRgb(255,255,255)); //set Pixel
}
else
{
if(x1<x2)
x1++;
else
x1--;
g=g+(2*dy);
img.setPixel(x1,y1,qRgb(255,255,255));
}
}
}
if(dx<dy)
{
g=(2*dx)-dy;
while(y1!=y2)
{
if(g>=0)
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
g=g+(2*(dx-dy));
img.setPixel(x1,y1,qRgb(255,255,255));
}
else
{
if(y1<y2)
y1++;
else
y1--;
g=g+(2*dx);
img.setPixel(x1,y1,qRgb(255,255,255));
}
}
}
return img;
}
};
circle::circle(QWidget *parent) : //Class Circle
QMainWindow(parent),
ui(new Ui::circle)
{
ui->setupUi(this);
connect(this->ui->pushButton,SIGNAL(clicked()),
this,SLOT(drawcircle()));
}
circle::~circle() //Destructor
{
delete ui;
}
void circle::drawcircle() //Drawcircle
{
line l1;
int xc,yc;
int x1,y1,x2,y2;
int r;
int d;
QImage img(400,400,QImage::Format_RGB888);
img.fill(Qt::black);
xc = this->ui->textEdit->toPlainText().toInt();
yc = this->ui->textEdit_2->toPlainText().toInt();
r = this->ui->textEdit_3->toPlainText().toInt();
x1=0;
y1=r;
x2=x1;
y2=y1;
d=3-(2*r);
while(x1<y1)
{
x2=x1;
y2=y1;
x1++;
if(d<0)
{
d=d+(4*x1)+6;
}
else
{
y1=y1-1;
d=d+(4*(x1-y1))+10;
}
img=l1.draw(xc+x1,yc+y1,xc+x2,yc+y2,img);
img=l1.draw(xc-x1,yc+y1,xc-x2,yc+y2,img);
img=l1.draw(xc+x1,yc-y1,xc+x2,yc-y2,img);
img=l1.draw(xc-x1,yc-y1,xc-x2,yc-y2,img);
img=l1.draw(xc+y1,yc+x1,xc+y2,yc+x2,img);
img=l1.draw(xc-y1,yc+x1,xc-y2,yc+x2,img);
img=l1.draw(xc+y1,yc-x1,xc+y2,yc-x2,img);
img=l1.draw(xc-y1,yc-x1,xc-y2,yc-x2,img);
}
ui->label_4->setPixmap(QPixmap::fromImage(img));
}
#ifndef CIRCLE_H
#define CIRCLE_H
#include <QMainWindow>
#include "ui_circle.h"
namespace Ui {
class circle;
}
class circle : public QMainWindow
{
Q_OBJECT
public:
explicit circle(QWidget *parent = 0);
~circle();
private:
Ui::circle *ui;
private slots:
void drawcircle();
};
#endif
___________________________________________________________
circle.cpp file:
#include "circle.h"
#include "ui_circle.h"
#include"math.h"
class line //class Line
{
public:
QImage draw(int x1,int y1,int x2,int y2, QImage img)
{
int dx,dy;
int g;
if(x2>x1)
dx=x2-x1;
else
dx=x1-x2;
if(y2>y1)
dy=y2-y1;
else
dy=y1-y2;
if(dx>=dy)
{
g=(2*dy)-dx;
while(x1!=x2)
{
if(g>0)
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
g=g+(2*(dy-dx));
img.setPixel(x1,y1,qRgb(255,255,255)); //set Pixel
}
else
{
if(x1<x2)
x1++;
else
x1--;
g=g+(2*dy);
img.setPixel(x1,y1,qRgb(255,255,255));
}
}
}
if(dx<dy)
{
g=(2*dx)-dy;
while(y1!=y2)
{
if(g>=0)
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
g=g+(2*(dx-dy));
img.setPixel(x1,y1,qRgb(255,255,255));
}
else
{
if(y1<y2)
y1++;
else
y1--;
g=g+(2*dx);
img.setPixel(x1,y1,qRgb(255,255,255));
}
}
}
return img;
}
};
circle::circle(QWidget *parent) : //Class Circle
QMainWindow(parent),
ui(new Ui::circle)
{
ui->setupUi(this);
connect(this->ui->pushButton,SIGNAL(clicked()),
this,SLOT(drawcircle()));
}
circle::~circle() //Destructor
{
delete ui;
}
void circle::drawcircle() //Drawcircle
{
line l1;
int xc,yc;
int x1,y1,x2,y2;
int r;
int d;
QImage img(400,400,QImage::Format_RGB888);
img.fill(Qt::black);
xc = this->ui->textEdit->toPlainText().toInt();
yc = this->ui->textEdit_2->toPlainText().toInt();
r = this->ui->textEdit_3->toPlainText().toInt();
x1=0;
y1=r;
x2=x1;
y2=y1;
d=3-(2*r);
while(x1<y1)
{
x2=x1;
y2=y1;
x1++;
if(d<0)
{
d=d+(4*x1)+6;
}
else
{
y1=y1-1;
d=d+(4*(x1-y1))+10;
}
img=l1.draw(xc+x1,yc+y1,xc+x2,yc+y2,img);
img=l1.draw(xc-x1,yc+y1,xc-x2,yc+y2,img);
img=l1.draw(xc+x1,yc-y1,xc+x2,yc-y2,img);
img=l1.draw(xc-x1,yc-y1,xc-x2,yc-y2,img);
img=l1.draw(xc+y1,yc+x1,xc+y2,yc+x2,img);
img=l1.draw(xc-y1,yc+x1,xc-y2,yc+x2,img);
img=l1.draw(xc+y1,yc-x1,xc+y2,yc-x2,img);
img=l1.draw(xc-y1,yc-x1,xc-y2,yc-x2,img);
}
ui->label_4->setPixmap(QPixmap::fromImage(img));
}
Output
No comments:
Post a Comment