Make parallel lines
And make TODO for P2P
This commit is contained in:
118
Canvas.cpp
118
Canvas.cpp
@@ -1,21 +1,43 @@
|
||||
#include "Canvas.h"
|
||||
|
||||
void Canvas::changeMode(Mode _mode)
|
||||
{
|
||||
mode = _mode;
|
||||
}
|
||||
|
||||
// TODO
|
||||
Line* Canvas::findAt(QPointF& pos)
|
||||
{
|
||||
for (Line* line : lines) {
|
||||
if (line->contains(pos)) {
|
||||
return line;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
LinePair Canvas::makeOrderedPair(Line* l1, Line* l2)
|
||||
{
|
||||
return (l1 < l2) ? std::make_pair(l1, l2) : std::make_pair(l2, l1);
|
||||
}
|
||||
|
||||
bool Canvas::areAlreadyParallel(Line* l1, Line* l2)
|
||||
{
|
||||
return parallelPairs.count(makeOrderedPair(l1, l2)) > 0;
|
||||
}
|
||||
|
||||
void Canvas::mousePressEvent(QMouseEvent* event)
|
||||
{
|
||||
QPointF scene = event->pos();
|
||||
|
||||
#ifdef _DEBUG
|
||||
qDebug() << "Scene point in" << scene.x() << scene.y();
|
||||
changeMode(DrawingLine);
|
||||
#endif
|
||||
#ifdef _DEBUG
|
||||
qDebug() << "Scene point in" << scene.x() << scene.y();
|
||||
#endif
|
||||
|
||||
if (mode == DrawingLine) {
|
||||
if (mode == Mode::DrawingLine) {
|
||||
if (!current_line) {
|
||||
current_line = new Line();
|
||||
current_line->set_tag(obj_count);
|
||||
double* x1 = new double(scene.x());
|
||||
double* y1 = new double(scene.y());
|
||||
|
||||
@@ -25,6 +47,9 @@ void Canvas::mousePressEvent(QMouseEvent* event)
|
||||
params.push_back(x1);
|
||||
params.push_back(y1);
|
||||
|
||||
points.push_back(Point(x1, y1, obj_count));
|
||||
points.push_back(Point(x2, y2, obj_count));
|
||||
|
||||
current_line->p1.x = x1;
|
||||
current_line->p1.y = y1;
|
||||
current_line->p2.x = x2;
|
||||
@@ -39,25 +64,83 @@ void Canvas::mousePressEvent(QMouseEvent* event)
|
||||
|
||||
lines.append(current_line);
|
||||
current_line = nullptr;
|
||||
}
|
||||
|
||||
//#ifdef _DEBUG
|
||||
// if (lines.size()) {
|
||||
// qDebug() << "Line coords" << lines[lines.size() - 1]->p1.get_X() <<
|
||||
// lines[lines.size() - 1]->p1.get_Y() <<
|
||||
// lines[lines.size() - 1]->p2.get_X() <<
|
||||
// lines[lines.size() - 1]->p2.get_Y();
|
||||
// changeMode(None);
|
||||
// }
|
||||
//#endif
|
||||
|
||||
mode = Mode::None;
|
||||
obj_count++;
|
||||
update();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
else if (mode == Mode::Parallel) {
|
||||
static Line* first = nullptr;
|
||||
Line* found = findAt(scene);
|
||||
|
||||
if (!found) {
|
||||
first = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!first) {
|
||||
first = found;
|
||||
return;
|
||||
}
|
||||
|
||||
if (found == first){
|
||||
first = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!areAlreadyParallel(found, first)) {
|
||||
sys.addConstraintParallel(*found, *first, constraints_count++);
|
||||
parallelPairs.insert(makeOrderedPair(found, first));
|
||||
first = nullptr;
|
||||
mode = Mode::None;
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
#ifdef _DEBUG
|
||||
qDebug() << "Line" << first << "and" << found << "are parallel. Abort!";
|
||||
#endif
|
||||
QMessageBox::information(this,
|
||||
QString("Wrong"),
|
||||
QString("Parallel lines can not be more parallel!"),
|
||||
QMessageBox::Ok
|
||||
);
|
||||
first = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::paintEvent(QPaintEvent*)
|
||||
{
|
||||
QPainter p(this);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
if (!params.empty()) {
|
||||
int res = sys.solve(params);
|
||||
if (res == SolveStatus::Success) {
|
||||
sys.applySolution();
|
||||
}
|
||||
else {
|
||||
#ifdef _DEBUG
|
||||
qDebug() << "INVAILD LAST CONSTRAINT. REMOVE HIM";
|
||||
#endif
|
||||
sys.removeConstraint(sys.get_last_constraint());
|
||||
}
|
||||
}
|
||||
for (Line* line : lines) {
|
||||
line->draw(p);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Canvas::Canvas(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
sys = System();
|
||||
current_line = nullptr;
|
||||
setMouseTracking(true);
|
||||
setBackgroundRole(QPalette::Base);
|
||||
@@ -76,6 +159,7 @@ Canvas::~Canvas()
|
||||
|
||||
lines.clear();
|
||||
params.clear();
|
||||
points.clear();
|
||||
|
||||
if (current_line)
|
||||
delete current_line;
|
||||
|
||||
Reference in New Issue
Block a user