Готовая окружность

Пока без наложения базовых ограничений
This commit is contained in:
2025-12-24 21:24:46 +03:00
parent 95f99575e2
commit 85d8e614d8
5 changed files with 91 additions and 47 deletions

View File

@@ -98,6 +98,16 @@ Curve* Canvas::findAt(QPointF& pos, qreal tolerance)
return line; return line;
} }
} }
else if (Circle* circle = dynamic_cast<Circle*>(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; return nullptr;
} }
@@ -240,7 +250,7 @@ void Canvas::mousePressEvent(QMouseEvent* event)
Line* found = dynamic_cast<Line*>(findAt(scene)); Line* found = dynamic_cast<Line*>(findAt(scene));
if (found) { if (found) {
draggedLine = found; draggedCurve = found;
QPointF lineCenter( QPointF lineCenter(
(*found->p1.x + *found->p2.x) / 2.0, (*found->p1.x + *found->p2.x) / 2.0,
(*found->p1.y + *found->p2.y) / 2.0 (*found->p1.y + *found->p2.y) / 2.0
@@ -251,7 +261,9 @@ void Canvas::mousePressEvent(QMouseEvent* event)
Circle* found_circle = dynamic_cast<Circle*>(findAt(scene)); Circle* found_circle = dynamic_cast<Circle*>(findAt(scene));
if (found_circle) { if (found_circle) {
// TODO draggedCurve = found_circle;
QPointF center(*found_circle->center.x, *found_circle->center.y);
dragOffset = scene - center;
return; return;
} }
} }
@@ -401,6 +413,7 @@ void Canvas::mousePressEvent(QMouseEvent* event)
mode = Mode::None; mode = Mode::None;
} }
solve_for_canvas(); solve_for_canvas();
return;
} }
// ====================== Режим Parallel: задание параллельности ====================== // ====================== Режим Parallel: задание параллельности ======================
else if (mode == Mode::Parallel) { else if (mode == Mode::Parallel) {
@@ -581,45 +594,60 @@ void Canvas::mouseMoveEvent(QMouseEvent* event)
} }
} }
} }
solve_for_canvas();
} }
// ====================== Перемещение линии ====================== // ====================== Перемещение линии ======================
else if (draggedLine) { else if (draggedCurve) {
QPointF newCenter = UCS_POSITION - dragOffset; if (Line* draggedLine = dynamic_cast<Line*>(draggedCurve)) {
QPointF oldCenter( QPointF newCenter = UCS_POSITION - dragOffset;
(*draggedLine->p1.x + *draggedLine->p2.x) / 2.0, QPointF oldCenter(
(*draggedLine->p1.y + *draggedLine->p2.y) / 2.0 (*draggedLine->p1.x + *draggedLine->p2.x) / 2.0,
); (*draggedLine->p1.y + *draggedLine->p2.y) / 2.0
);
// Вычисляем смещение // Вычисляем смещение
double dx = newCenter.x() - oldCenter.x(); double dx = newCenter.x() - oldCenter.x();
double dy = newCenter.y() - oldCenter.y(); 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++) { for (int i = 0; i < 2; i++) {
Point* currentPoint = linePoints[i]; Point* currentPoint = linePoints[i];
*currentPoint->x += dx; *currentPoint->x += dx;
*currentPoint->y += dy; *currentPoint->y += dy;
// Обновляем совпадающие точки // Обновляем совпадающие точки
for (Point* pair : points) { for (Point* pair : points) {
if (pair != currentPoint && areCoincident(currentPoint, pair)) { if (pair != currentPoint && areCoincident(currentPoint, pair)) {
*pair->x = *currentPoint->x; *pair->x = *currentPoint->x;
*pair->y = *currentPoint->y; *pair->y = *currentPoint->y;
}
}
}
}
else if (Circle* circle = dynamic_cast<Circle*>(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 #ifdef _DEBUG
else else
showObjectTag(WIDGET_POSITION); showObjectTag(WIDGET_POSITION);
#endif #endif
solve_for_canvas();
} }
void Canvas::mouseReleaseEvent(QMouseEvent* event) void Canvas::mouseReleaseEvent(QMouseEvent* event)
@@ -631,8 +659,8 @@ void Canvas::mouseReleaseEvent(QMouseEvent* event)
solve_for_canvas(); solve_for_canvas();
} }
if (draggedLine) { if (draggedCurve) {
draggedLine = nullptr; draggedCurve = nullptr;
solve_for_canvas(); solve_for_canvas();
} }
@@ -681,16 +709,16 @@ void Canvas::paintEvent(QPaintEvent* event)
p.drawEllipse(QPointF(*line->p2.x, *line->p2.y), 5, 5); p.drawEllipse(QPointF(*line->p2.x, *line->p2.y), 5, 5);
} }
else if (Circle* circle = dynamic_cast<Circle*>(curve)) { else if (Circle* circle = dynamic_cast<Circle*>(curve)) {
p.setPen(QPen(Qt::black, 2));
p.setBrush(Qt::NoBrush);
// Настройка пера для линии p.drawEllipse(QPointF(*circle->center.x, *circle->center.y),
QPen linePen = QPen(Qt::black, 2); *circle->rad, *circle->rad);
p.setPen(linePen);
p.drawEllipse(QPointF(*circle->center.x, *circle->center.y), *circle->rad, *circle->rad); p.setBrush(QBrush(Qt::darkBlue));
QBrush pointBrush = QBrush(Qt::darkBlue);
p.setBrush(pointBrush);
p.setPen(Qt::NoPen); p.setPen(Qt::NoPen);
p.drawEllipse(QPointF(*circle->center.x, *circle->center.y), 5, 5); p.drawEllipse(QPointF(*circle->center.x, *circle->center.y), 5, 5);
p.setBrush(Qt::NoBrush);
} }
} }

View File

@@ -251,6 +251,7 @@ private:
// ====================== Данные для перемещения объектов ====================== // ====================== Данные для перемещения объектов ======================
Point* draggedPoint{ nullptr }; ///< Точка, которую перемещают Point* draggedPoint{ nullptr }; ///< Точка, которую перемещают
Line* draggedLine{ nullptr }; ///< Линия, которую перемещают Line* draggedLine{ nullptr }; ///< Линия, которую перемещают
Curve* draggedCurve { nullptr };
QPointF dragOffset; ///< Смещение при начале перемещения QPointF dragOffset; ///< Смещение при начале перемещения
// ====================== Данные геометрической системы ====================== // ====================== Данные геометрической системы ======================

View File

@@ -43,3 +43,9 @@ void DRAWer_2_0::on_Perpendicular_Button_clicked()
ui.widget->changeMode(Mode::Perpendicular); ui.widget->changeMode(Mode::Perpendicular);
} }
void DRAWer_2_0::on_Circle_Button_clicked()
{
ui.widget->changeMode(Mode::DrawingCircle);
}

View File

@@ -24,6 +24,8 @@ private slots:
void on_Perpendicular_Button_clicked(); void on_Perpendicular_Button_clicked();
void on_Circle_Button_clicked();
private: private:
Ui::DRAWer_2_0Class ui; Ui::DRAWer_2_0Class ui;
}; };

View File

@@ -28,19 +28,6 @@
</property> </property>
<item> <item>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QPushButton" name="Line_Button">
<property name="minimumSize">
<size>
<width>75</width>
<height>24</height>
</size>
</property>
<property name="text">
<string>Line</string>
</property>
</widget>
</item>
<item row="0" column="0"> <item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout_6"> <layout class="QVBoxLayout" name="verticalLayout_6">
<item> <item>
@@ -58,6 +45,26 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="1" column="0">
<widget class="QPushButton" name="Line_Button">
<property name="minimumSize">
<size>
<width>75</width>
<height>24</height>
</size>
</property>
<property name="text">
<string>Line</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="Circle_Button">
<property name="text">
<string>Circle</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>
@@ -163,7 +170,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>776</width> <width>776</width>
<height>21</height> <height>22</height>
</rect> </rect>
</property> </property>
</widget> </widget>