Coincedent points

This commit is contained in:
ParkSuMin
2025-12-07 16:49:54 +03:00
parent 5c0e04df5b
commit ff06fd1db7
3 changed files with 63 additions and 3 deletions

View File

@@ -1,5 +1,9 @@
#include "Canvas.h" #include "Canvas.h"
static double dist_P2P(QPointF p1, QPointF p2) {
return sqrt(pow(p2.x() - p1.x(), 2) + pow(p2.y() - p1.y(), 2));
}
void Canvas::changeMode(Mode _mode) void Canvas::changeMode(Mode _mode)
{ {
mode = _mode; mode = _mode;
@@ -16,6 +20,20 @@ Line* Canvas::findAt(QPointF& pos)
return nullptr; return nullptr;
} }
Point* Canvas::findPointAt(QPointF pos, qreal tolerance)
{
for (Line* line : lines) {
QPointF p1(*line->p1.x, *line->p1.y);
QPointF p2(*line->p2.x, *line->p2.y);
if (dist_P2P(p1, pos) <= tolerance)
return &line->p1;
else if (dist_P2P(p2, pos) <= tolerance)
return &line->p2;
}
return nullptr;
}
LinePair Canvas::makeOrderedPair(Line* l1, Line* l2) LinePair Canvas::makeOrderedPair(Line* l1, Line* l2)
{ {
return (l1 < l2) ? std::make_pair(l1, l2) : std::make_pair(l2, l1); return (l1 < l2) ? std::make_pair(l1, l2) : std::make_pair(l2, l1);
@@ -102,7 +120,7 @@ void Canvas::mousePressEvent(QMouseEvent* event)
#ifdef _DEBUG #ifdef _DEBUG
qDebug() << "Line" << first << "and" << found << "are parallel. Abort!"; qDebug() << "Line" << first << "and" << found << "are parallel. Abort!";
#endif #endif
QMessageBox::information(this, QMessageBox::warning(this,
QString("Wrong"), QString("Wrong"),
QString("Parallel lines can not be more parallel!"), QString("Parallel lines can not be more parallel!"),
QMessageBox::Ok QMessageBox::Ok
@@ -113,6 +131,43 @@ void Canvas::mousePressEvent(QMouseEvent* event)
update(); update();
} }
else if (mode == Mode::Coincedent) {
Point* clickedPoint = findPointAt(scene);
if (!clickedPoint) {
firstPoint = nullptr;
return;
}
if (!firstPoint) {
firstPoint = clickedPoint;
return;
}
if (clickedPoint == firstPoint) {
firstPoint = nullptr;
return;
}
Line *l1 = nullptr, *l2 = nullptr;
for (Line* l : lines) {
if (&l->p1 == firstPoint || &l->p2 == firstPoint) l1 = l;
if (&l->p1 == clickedPoint || &l->p2 == clickedPoint) l2 = l;
}
if (l1 == l2) {
QMessageBox::warning(this, QString("NO!"), QString("P2P failed"));
firstPoint = nullptr;
return;
}
sys.addConstraintP2PCoincident(*firstPoint, *clickedPoint, constraints_count++);
firstPoint = nullptr;
mode = Mode::None;
update();
return;
}
} }
void Canvas::paintEvent(QPaintEvent*) void Canvas::paintEvent(QPaintEvent*)
@@ -163,5 +218,7 @@ Canvas::~Canvas()
if (current_line) if (current_line)
delete current_line; delete current_line;
if (firstPoint)
delete firstPoint;
} }

View File

@@ -18,7 +18,8 @@ enum class Mode : int
{ {
None = 0, None = 0,
DrawingLine = 1, DrawingLine = 1,
Parallel = 2 Parallel = 2,
Coincedent = 3
}; };
// Óäîáíûé òèï äëÿ õðàíåíèÿ ïàðû ïàðàëëåëüíûõ ëèíèé (ïîðÿäîê íå âàæåí) // Óäîáíûé òèï äëÿ õðàíåíèÿ ïàðû ïàðàëëåëüíûõ ëèíèé (ïîðÿäîê íå âàæåí)
@@ -45,6 +46,7 @@ protected:
private: private:
// ====================== Ïîèñê è âûáîð ====================== // ====================== Ïîèñê è âûáîð ======================
Line* findAt(QPointF&); // èùåò ëèíèþ ïîä êóðñîðîì Line* findAt(QPointF&); // èùåò ëèíèþ ïîä êóðñîðîì
Point* findPointAt(QPointF, qreal tolerance = 10.0);
// ====================== Ïàðàëëåëüíîñòü ====================== // ====================== Ïàðàëëåëüíîñòü ======================
LinePair makeOrderedPair(Line* l1, Line* l2); LinePair makeOrderedPair(Line* l1, Line* l2);
@@ -59,6 +61,7 @@ private:
std::set<LinePair> parallelPairs; // óæå çàïàðàëëåëåííûå ïàðû (çàùèòà îò äóáëåé) std::set<LinePair> parallelPairs; // óæå çàïàðàëëåëåííûå ïàðû (çàùèòà îò äóáëåé)
Line* current_line{ nullptr }; Line* current_line{ nullptr };
Point* firstPoint{ nullptr };
Mode mode{ Mode::None }; Mode mode{ Mode::None };
int obj_count{ 0 }; // òåã äëÿ íîâûõ îáúåêòîâ int obj_count{ 0 }; // òåã äëÿ íîâûõ îáúåêòîâ

View File

@@ -26,6 +26,6 @@ void DRAWer_2_0::on_pushButton_2_clicked()
void DRAWer_2_0::on_pushButton_3_clicked() void DRAWer_2_0::on_pushButton_3_clicked()
{ {
return; ui.widget->changeMode(Mode::Coincedent);
} }