From 85d8e614d870161b99bdfbb9ea48f7d6e852d51d Mon Sep 17 00:00:00 2001 From: ParkSuMin Date: Wed, 24 Dec 2025 21:24:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BE=D0=BA=D1=80=D1=83=D0=B6=D0=BD=D0=BE=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Пока без наложения базовых ограничений --- Canvas.cpp | 94 ++++++++++++++++++++++++++++++++------------------ Canvas.h | 1 + DRAWer_2_0.cpp | 6 ++++ DRAWer_2_0.h | 2 ++ DRAWer_2_0.ui | 35 +++++++++++-------- 5 files changed, 91 insertions(+), 47 deletions(-) diff --git a/Canvas.cpp b/Canvas.cpp index 492fef6..f39bfb8 100644 --- a/Canvas.cpp +++ b/Canvas.cpp @@ -98,6 +98,16 @@ Curve* Canvas::findAt(QPointF& pos, qreal tolerance) return line; } } + else if (Circle* circle = dynamic_cast(curve)) { + QPointF center(*circle->center.x, *circle->center.y); + double radius = *circle->rad; + + double distToCenter = dist_P2P(center, pos); + // Проверяем, лежит ли точка вблизи окружности (с допуском) + if (std::abs(distToCenter - radius) <= tolerance) { + return circle; + } + } } return nullptr; } @@ -240,7 +250,7 @@ void Canvas::mousePressEvent(QMouseEvent* event) Line* found = dynamic_cast(findAt(scene)); if (found) { - draggedLine = found; + draggedCurve = found; QPointF lineCenter( (*found->p1.x + *found->p2.x) / 2.0, (*found->p1.y + *found->p2.y) / 2.0 @@ -251,7 +261,9 @@ void Canvas::mousePressEvent(QMouseEvent* event) Circle* found_circle = dynamic_cast(findAt(scene)); if (found_circle) { - // TODO + draggedCurve = found_circle; + QPointF center(*found_circle->center.x, *found_circle->center.y); + dragOffset = scene - center; return; } } @@ -401,6 +413,7 @@ void Canvas::mousePressEvent(QMouseEvent* event) mode = Mode::None; } solve_for_canvas(); + return; } // ====================== Режим Parallel: задание параллельности ====================== else if (mode == Mode::Parallel) { @@ -581,45 +594,60 @@ void Canvas::mouseMoveEvent(QMouseEvent* event) } } } - - solve_for_canvas(); } // ====================== Перемещение линии ====================== - else if (draggedLine) { - QPointF newCenter = UCS_POSITION - dragOffset; - QPointF oldCenter( - (*draggedLine->p1.x + *draggedLine->p2.x) / 2.0, - (*draggedLine->p1.y + *draggedLine->p2.y) / 2.0 - ); + else if (draggedCurve) { + if (Line* draggedLine = dynamic_cast(draggedCurve)) { + QPointF newCenter = UCS_POSITION - dragOffset; + QPointF oldCenter( + (*draggedLine->p1.x + *draggedLine->p2.x) / 2.0, + (*draggedLine->p1.y + *draggedLine->p2.y) / 2.0 + ); - // Вычисляем смещение - double dx = newCenter.x() - oldCenter.x(); - double dy = newCenter.y() - oldCenter.y(); + // Вычисляем смещение + double dx = newCenter.x() - oldCenter.x(); + double dy = newCenter.y() - oldCenter.y(); - // Перемещаем обе точки линии - Point* linePoints[2] = { draggedLine->start_ref, draggedLine->end_ref }; + // Перемещаем обе точки линии + Point* linePoints[2] = { draggedLine->start_ref, draggedLine->end_ref }; - for (int i = 0; i < 2; i++) { - Point* currentPoint = linePoints[i]; + for (int i = 0; i < 2; i++) { + Point* currentPoint = linePoints[i]; - *currentPoint->x += dx; - *currentPoint->y += dy; + *currentPoint->x += dx; + *currentPoint->y += dy; - // Обновляем совпадающие точки - for (Point* pair : points) { - if (pair != currentPoint && areCoincident(currentPoint, pair)) { - *pair->x = *currentPoint->x; - *pair->y = *currentPoint->y; + // Обновляем совпадающие точки + for (Point* pair : points) { + if (pair != currentPoint && areCoincident(currentPoint, pair)) { + *pair->x = *currentPoint->x; + *pair->y = *currentPoint->y; + } + } + } + } + else if (Circle* circle = dynamic_cast(draggedCurve)) { + QPointF newCenter = UCS_POSITION - dragOffset; + *circle->center.x = newCenter.x(); + *circle->center.y = newCenter.y(); + *circle->center_ref->x = newCenter.x(); + *circle->center_ref->y = newCenter.y(); + + // Также обновляем все совпадающие точки (если центр совпадает с другими точками через P2P) + for (Point* other : points) { + if (other != circle->center_ref && areCoincident(circle->center_ref, other)) { + *other->x = newCenter.x(); + *other->y = newCenter.y(); } } } - solve_for_canvas(); } #ifdef _DEBUG else showObjectTag(WIDGET_POSITION); #endif + solve_for_canvas(); } void Canvas::mouseReleaseEvent(QMouseEvent* event) @@ -631,8 +659,8 @@ void Canvas::mouseReleaseEvent(QMouseEvent* event) solve_for_canvas(); } - if (draggedLine) { - draggedLine = nullptr; + if (draggedCurve) { + draggedCurve = nullptr; solve_for_canvas(); } @@ -681,16 +709,16 @@ void Canvas::paintEvent(QPaintEvent* event) p.drawEllipse(QPointF(*line->p2.x, *line->p2.y), 5, 5); } else if (Circle* circle = dynamic_cast(curve)) { + p.setPen(QPen(Qt::black, 2)); + p.setBrush(Qt::NoBrush); - // Настройка пера для линии - QPen linePen = QPen(Qt::black, 2); - p.setPen(linePen); + p.drawEllipse(QPointF(*circle->center.x, *circle->center.y), + *circle->rad, *circle->rad); - p.drawEllipse(QPointF(*circle->center.x, *circle->center.y), *circle->rad, *circle->rad); - QBrush pointBrush = QBrush(Qt::darkBlue); - p.setBrush(pointBrush); + p.setBrush(QBrush(Qt::darkBlue)); p.setPen(Qt::NoPen); p.drawEllipse(QPointF(*circle->center.x, *circle->center.y), 5, 5); + p.setBrush(Qt::NoBrush); } } diff --git a/Canvas.h b/Canvas.h index 8661fcc..0f444cc 100644 --- a/Canvas.h +++ b/Canvas.h @@ -251,6 +251,7 @@ private: // ====================== Данные для перемещения объектов ====================== Point* draggedPoint{ nullptr }; ///< Точка, которую перемещают Line* draggedLine{ nullptr }; ///< Линия, которую перемещают + Curve* draggedCurve { nullptr }; QPointF dragOffset; ///< Смещение при начале перемещения // ====================== Данные геометрической системы ====================== diff --git a/DRAWer_2_0.cpp b/DRAWer_2_0.cpp index 7c62787..9a95dad 100644 --- a/DRAWer_2_0.cpp +++ b/DRAWer_2_0.cpp @@ -43,3 +43,9 @@ void DRAWer_2_0::on_Perpendicular_Button_clicked() ui.widget->changeMode(Mode::Perpendicular); } + +void DRAWer_2_0::on_Circle_Button_clicked() +{ + ui.widget->changeMode(Mode::DrawingCircle); +} + diff --git a/DRAWer_2_0.h b/DRAWer_2_0.h index a4e5f87..df1232a 100644 --- a/DRAWer_2_0.h +++ b/DRAWer_2_0.h @@ -24,6 +24,8 @@ private slots: void on_Perpendicular_Button_clicked(); + void on_Circle_Button_clicked(); + private: Ui::DRAWer_2_0Class ui; }; diff --git a/DRAWer_2_0.ui b/DRAWer_2_0.ui index 88e5956..66f3857 100644 --- a/DRAWer_2_0.ui +++ b/DRAWer_2_0.ui @@ -28,19 +28,6 @@ - - - - - 75 - 24 - - - - Line - - - @@ -58,6 +45,26 @@ + + + + + 75 + 24 + + + + Line + + + + + + + Circle + + + @@ -163,7 +170,7 @@ 0 0 776 - 21 + 22