#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QString>
#include <QPainter>
#include <QPaintEvent>
#include <QPen>
#include <QFont>
#include <QTextEdit>
#include <QComboBox>
#include "math.h"
#define MOVE_VAL 50
class Triangle
{
public:
int id;
int level;
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
};
class cDrawField : public QWidget
{
Q_OBJECT
public:
cDrawField(QWidget *parent = 0);
~cDrawField() {};
double tx1, ty1, tx2, ty2, ts;
double scale_val; // dlya mshtabirovaniya, koefficient
int x_val, y_val;
QPainter painter;
QList<Triangle> triag_lst;
bool time_bool;
int counter;
int rec_level;
//QComboBox *boxTriangle;
protected:
void paintEvent(QPaintEvent *);
void triangleDraw(double tmpx1, double tmpy1, double tmpx2, double tmpy2, double tmpx3, double tmpy3, int tmplevel);
};
cDrawField::cDrawField(QWidget *parent)
: QWidget(parent)
{
scale_val = 1;
x_val = 0;
y_val = 0;
tx1 = 0;
ty1 = 0;
tx2 = 0;
ty2 = 0;
ts = 0;
time_bool = true;
counter = 0;
rec_level = 0;
setMinimumSize(500, 500);
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
//QComboBox *boxTriangle;
public slots:
void setValue();
void drawRect();
void scaleMinus();
void scalePlus();
void moveLeft() { drawer->x_val -= MOVE_VAL; drawer->repaint(); };
void moveRight() { drawer->x_val += MOVE_VAL; drawer->repaint(); };
void moveUp() { drawer->y_val -= MOVE_VAL; drawer->repaint(); };
void moveDown() { drawer->y_val += MOVE_VAL; drawer->repaint(); };
void triagSelect(int index);
private:
QLineEdit *x1edit;
QLineEdit *y1edit;
QLineEdit *x2edit;
QLineEdit *y2edit;
QLineEdit *sedit;
QPushButton *automode;
QPushButton *resetmode;
QPushButton *gomode;
QComboBox *boxTriangle;
QTextEdit *textTriangle;
QPushButton *minus;
QPushButton *plus;
QPushButton *b_left, *b_right,
*b_up, *b_down;
cDrawField *drawer;
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *layout = new QHBoxLayout;
QGridLayout *grid = new QGridLayout;
QLabel *x1label = new QLabel(tr("x1="));
QLabel *y1label = new QLabel(tr("y1="));
QLabel *x2label = new QLabel(tr("x2="));
QLabel *y2label = new QLabel(tr("y2="));
QLabel *slabel = new QLabel(tr("S="));
QLabel *nolabel = new QLabel(trUtf8("№="));
x1edit = new QLineEdit;
y1edit = new QLineEdit;
x2edit = new QLineEdit;
y2edit = new QLineEdit;
sedit = new QLineEdit;
automode = new QPushButton(tr("Auto"));
resetmode = new QPushButton(tr("Reset"));
gomode = new QPushButton(tr("Go!"));
minus = new QPushButton(tr("-"));
plus = new QPushButton(tr("+"));
b_left = new QPushButton(tr("left"));
b_right = new QPushButton(tr("right"));
b_up = new QPushButton(tr("up"));
b_down = new QPushButton(tr("down"));
boxTriangle = new QComboBox(this);
textTriangle = new QTextEdit(this);
drawer = new cDrawField(this);
grid->addWidget(x1label, 0, 0);
grid->addWidget(y1label, 1, 0);
grid->addWidget(x2label, 2, 0);
grid->addWidget(y2label, 3, 0);
grid->addWidget(slabel, 4, 0);
grid->addWidget(x1edit, 0, 1);
grid->addWidget(y1edit, 1, 1);
grid->addWidget(x2edit, 2, 1);
grid->addWidget(y2edit, 3, 1);
grid->addWidget(sedit, 4, 1);
grid->addWidget(automode, 5, 1);
grid->addWidget(resetmode, 6, 1);
grid->addWidget(gomode, 7, 1);
//grid->addWidget(drawer, 0, 2, 8, 3);
grid->setRowStretch(8,1);
grid->addWidget(minus, 9, 0);
grid->addWidget(plus, 9, 1);
grid->addWidget(b_up, 10, 0);
grid->addWidget(b_left, 10, 1);
grid->addWidget(b_down, 11, 0);
grid->addWidget(b_right, 11, 1);
grid->addWidget(nolabel, 12, 0);
grid->addWidget(boxTriangle, 12, 1);
grid->addWidget(textTriangle, 13, 0, 1, 2);
//grid->setColumnStretch(2, 1);
//grid->setRowStretch(1,1);
layout->addLayout(grid);
layout->addWidget(drawer, 1);
//layout->setAlignment(Qt::AlignLeft);
setLayout(layout);
connect(resetmode, SIGNAL(clicked()), x1edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), y1edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), x2edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), y2edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), sedit, SLOT(clear()));
connect(automode, SIGNAL(clicked()), this, SLOT(setValue()));
connect(gomode, SIGNAL(clicked()), this, SLOT(drawRect()));
connect(minus, SIGNAL(clicked()), this, SLOT(scaleMinus()));
connect(plus, SIGNAL(clicked()), this, SLOT(scalePlus()));
connect(b_left, SIGNAL(clicked()), this, SLOT(moveLeft()));
connect(b_right, SIGNAL(clicked()), this, SLOT(moveRight()));
connect(b_up, SIGNAL(clicked()), this, SLOT(moveUp()));
connect(b_down, SIGNAL(clicked()), this, SLOT(moveDown()));
connect(boxTriangle, SIGNAL(activated(int)), this, SLOT(triagSelect(int)));
}
void MyWidget::triagSelect(int index)
{
if (index)
{
//QList<Triangle> &lst = drawer->triag_lst;
textTriangle->clear();
textTriangle->append(QString::number(qAbs(index - 12)));
textTriangle->append(QString::number(qAbs(index - 11)));
textTriangle->append(QString::number(qAbs(index - 10)));
textTriangle->append(QString::number(qAbs(index - 6)));
textTriangle->append(QString::number(qAbs(index - 5)));
textTriangle->append(QString::number(qAbs(index - 4)));
textTriangle->append(QString::number(index + 1));
textTriangle->append(QString::number(index + 2));
textTriangle->append(QString::number(index + 3));
}
}
void MyWidget::setValue()
{
double tx1, ty1, tx2, ty2, ts;
QString sx1, sy1, sx2, sy2, ss;
tx1 = rand() % 500;
ty1 = rand() % 500;
tx2 = rand() % 500;
ty2 = rand() % 500;
ts = rand() % 100;
sx1 = QString::number(tx1);
sy1 = QString::number(ty1);
sx2 = QString::number(tx2);
sy2 = QString::number(ty2);
ss = QString::number(ts);
x1edit->setText(sx1);
y1edit->setText(sy1);
x2edit->setText(sx2);
y2edit->setText(sy2);
sedit->setText(ss);
};
void MyWidget::drawRect()
{
QString sx1, sy1, sx2, sy2, ss;
sx1 = x1edit->text();
sy1 = y1edit->text();
sx2 = x2edit->text();
sy2 = y2edit->text();
ss = sedit->text();
drawer->tx1 = sx1.toDouble();
drawer->ty1 = sy1.toDouble();
drawer->tx2 = sx2.toDouble();
drawer->ty2 = sy2.toDouble();
drawer->ts = ss.toDouble();
drawer->time_bool = true;
//drawer->update();
/*for(int k=0; k<500; ++k)
{
boxTriangle->insertItem(k, QString::number(k));
}*/
boxTriangle->clear();
boxTriangle->addItem(QLatin1String("---"), -1);
QList<Triangle> &lst = drawer->triag_lst;
foreach (Triangle trg, lst)
{
boxTriangle->addItem(QString(tr("Triangle: %1")).arg(trg.id), trg.id);
}
drawer->update();
};
void MyWidget::scaleMinus()
{
drawer->scale_val -= 0.15;
drawer->repaint();
};
void MyWidget::scalePlus()
{
drawer->scale_val += 0.15;
drawer->repaint();
};
void cDrawField::paintEvent(QPaintEvent *e)
{
double a;
double b;
double s;
int k;
if(time_bool)
triag_lst.clear();
counter = 0;
painter.begin(this);
//painter.translate(this->width()/2, this->height()/2);
//painter.scale(10, 10);
painter.setPen(QPen(Qt::black, 1, Qt::SolidLine));
// определяем глубину рекурсии
k = 0;
a = qAbs(ty2 - ty1);
b = qAbs(tx2 - tx1);
s = (a * b) / 2;
while (s > ts)
{
k++;
s = ((1 / pow(2, k)) * a * b) / 2;
}
rec_level = k;
int t_tx1, t_ty1, t_tx2, t_ty2;
t_tx1 = (x_val+tx1)*scale_val+(this->width()/2-(tx1+(tx2-tx1)/2)*scale_val);
t_ty1 = (y_val+ty1)*scale_val+(this->height()/2-(ty1+(ty2-ty1)/2)*scale_val);
t_tx2 = (x_val+tx2)*scale_val+(this->width()/2-(tx1+(tx2-tx1)/2)*scale_val);
t_ty2 = (y_val+ty2)*scale_val+(this->height()/2-(ty1+(ty2-ty1)/2)*scale_val);
triangleDraw(t_tx1, t_ty1, t_tx2, t_ty2, t_tx1, t_ty2, 0);
triangleDraw(t_tx1, t_ty1, t_tx2, t_ty2, t_tx2, t_ty1, 0);
painter.end();
if(time_bool)
time_bool = false;
/*for(int k=0; k<triag_lst.size(); ++k)
{
*boxTriangle.insertItem(triag_lst[k].id);
}*/
e->accept();
}
void cDrawField::triangleDraw(double tmpx1, double tmpy1, double tmpx2, double tmpy2, double tmpx3, double tmpy3, int tmplevel)
{
double tmpx4, tmpy4, tmpx5, tmpy5, tmpx6, tmpy6;
double sqt; // площадь
double a, b, c, p; // стороны и полупериметр
QString count;
QString lev;
painter.setPen(QPen(Qt::black, 1, Qt::SolidLine));
painter.drawLine(tmpx1, tmpy1, tmpx2, tmpy2);
painter.drawLine(tmpx2, tmpy2, tmpx3, tmpy3);
painter.drawLine(tmpx3, tmpy3, tmpx1, tmpy1);
//counter++;
tmplevel++;
if (tmplevel < rec_level)
//if (tmplevel == (rec_level - 5))
{
count = QString::number(counter);
if(time_bool)
{
Triangle triangle;
triangle.id = counter;
triangle.level = tmplevel;
triangle.x1 = tmpx1;
triangle.y1 = tmpy1;
triangle.x2 = tmpx2;
triangle.y2 = tmpy2;
triangle.x3 = tmpx3;
triangle.y3 = tmpy3;
triag_lst.append(triangle);
}
painter.setFont(QFont("Times", 10, QFont::Normal));
painter.setPen(QPen(Qt::blue, 1, Qt::SolidLine));
count = QString::number(counter);
painter.drawText(tmpx1+10, tmpy1+10, count);
painter.setPen(QPen(Qt::red, 1, Qt::SolidLine));
counter++;
count = QString::number(counter);
painter.drawText(tmpx1, tmpy1, count);
counter++;
count = QString::number(counter);
painter.drawText(tmpx2, tmpy2, count);
counter++;
count = QString::number(counter);
painter.drawText(tmpx3, tmpy3, count);
}
tmpx4 = (tmpx1 + tmpx2) / 2.0;
tmpy4 = (tmpy1 + tmpy2) / 2.0;
tmpx5 = (tmpx2 + tmpx3) / 2.0;
tmpy5 = (tmpy2 + tmpy3) / 2.0;
tmpx6 = (tmpx1 + tmpx3) / 2.0;
tmpy6 = (tmpy1 + tmpy3) / 2.0;
a = sqrt(((tmpx4 - tmpx5) * (tmpx4 - tmpx5)) + ((tmpy4 - tmpy5) * (tmpy4 - tmpy5)));
b = sqrt(((tmpx5 - tmpx6) * (tmpx5 - tmpx6)) + ((tmpy5 - tmpy6) * (tmpy5 - tmpy6)));
c = sqrt(((tmpx6 - tmpx4) * (tmpx6 - tmpx4)) + ((tmpy6 - tmpy4) * (tmpy6 - tmpy4)));
p = (a + b + c) / 2.0;
sqt = sqrt((p - a) * (p - b) * (p - c))/(scale_val*scale_val);
if ((sqt > ts) && (ts != 0))
{
triangleDraw(tmpx1, tmpy1, tmpx4, tmpy4, tmpx6, tmpy6, tmplevel);
triangleDraw(tmpx2, tmpy2, tmpx4, tmpy4, tmpx5, tmpy5, tmplevel);
triangleDraw(tmpx3, tmpy3, tmpx5, tmpy5, tmpx6, tmpy6, tmplevel);
triangleDraw(tmpx4, tmpy4, tmpx5, tmpy5, tmpx6, tmpy6, tmplevel);
}
}
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}