Repository navigation
Expand file tree
/
Copy pathpolygonmanager.cpp
More file actions
executable file
·144 lines (131 loc) · 4.48 KB
/
Copy pathpolygonmanager.cpp
File metadata and controls
executable file
·144 lines (131 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "polygonmanager.h"
#include <iostream>
#include <cmath>
#include <QMouseEvent>
#include "dialog.h"
#include "mainwindow.h"
constexpr double kDistanceSensitivity = 10;
constexpr double MinRefractiveIndex = 1.0;
constexpr double MaxRefractiveIndex = 4.0;
GeogebraPlane::GeogebraPlane(GraphicsView * graphics_view, QGraphicsScene *scene, MainWindow * main_window)
{
graphics_view_ = graphics_view;
scene_ = scene;
main_window_ = main_window;
// QObject::connect(graphics_view_, SIGNAL(PressLeftMouseButton(QPoint)), this, SLOT(AddPoint(QPoint)));
complete_polygons_.push_back(Polygon{});
}
struct Color
{
int red, green, blue;
};
QColor MakeColorFromRefractiveIndex(double index)
{
/*int red = 0, green = 0, blue = 0;
if (!((index >= MinRefractiveIndex) && (index <= MaxRefractiveIndex)))
{
return {red, green, blue};
}
double mid = (MinRefractiveIndex + MaxRefractiveIndex) / 2;
if (index >= mid)
{
green = round(255 * ((MaxRefractiveIndex - index) / (MaxRefractiveIndex - mid)));
red = round(255 - green / 20);
}
else
{
red = round(255 * ((index - MinRefractiveIndex) / (mid - MinRefractiveIndex)));
green = round(255 - red / 20);
}
return {red, green, blue};*/
QColor color;
color.setHsv(180 - std::round((index - 1) * 60), 255, 255);
return color;
}
void GeogebraPlane::UpdateScene()
{
// scene_->clear();
for (const auto& polygon: complete_polygons_)
{
QPolygon qt_polygon;
for (int vertex = 0; vertex < polygon.Size(); ++vertex)
{
auto point = polygon.GetVertex(vertex);
qt_polygon.push_back(point.GetQPoint());
}
auto color = MakeColorFromRefractiveIndex(polygon.GetRefractiveIndex());
// std::cout << red << " " << green << " " << blue << std::endl;
QBrush brush(color);
QPen pen;
scene_->addPolygon(qt_polygon, pen, brush);
}
for (int i = 0; i + 1 < incomplete_polygon_.Size(); ++i)
{
QLine line(incomplete_polygon_.GetVertex(i).GetQPoint(),
incomplete_polygon_.GetVertex(i + 1).GetQPoint());
QPen pen;
scene_->addLine(line, pen);
}
}
void GeogebraPlane::AddPoint(QPoint point)
{
for (const auto& polygon: complete_polygons_)
{
if (polygon.GetInsideStatus(point) != Polygon::InsideStatus::OUTSIDE)
{
emit ErrorDuringAddingPoint("Точка внутри одного из ранее созданных многоугольников!");
return;
}
}
if (incomplete_polygon_.Size() == 0)
{
incomplete_polygon_.AddPoint(point);
return;
}
Segment side(incomplete_polygon_.GetVertex(incomplete_polygon_.Size() - 1), point);
for (const auto& polygon: complete_polygons_)
{
if (polygon.DoesSegmentIntersectPolygon(side))
{
emit ErrorDuringAddingPoint("Отрезок пересекает многоугольник!");
return;
}
}
for (int i = 0; i + 2 < incomplete_polygon_.Size(); ++i)
{
if (DoSegmentsHaveCommonPoint(side, Segment(incomplete_polygon_.GetVertex(i),
incomplete_polygon_.GetVertex(i + 1))))
{
emit ErrorDuringAddingPoint("Самопересекающийся многоугольник!");
return;
}
}
if (Distance(incomplete_polygon_.GetVertex(0), point) < kDistanceSensitivity)
{
if (incomplete_polygon_.Size() <= 2)
{
emit ErrorDuringAddingPoint("Слишком маленький размер многоугольника!");
return;
}
for (const auto& polygon: complete_polygons_)
{
for (int j = 0; j < polygon.Size(); ++j)
{
if (incomplete_polygon_.GetInsideStatus(polygon.GetVertex(j))
!= Polygon::InsideStatus::OUTSIDE)
{
emit ErrorDuringAddingPoint("Вложенные многоугольники!");
return;
}
}
}
emit FinishEnteringPolygon(&incomplete_polygon_);
// std::cout << incomplete_polygon_.GetRefractiveIndex() << std::endl;
complete_polygons_.push_back(incomplete_polygon_);
incomplete_polygon_ = Polygon{};
UpdateScene();
return;
}
incomplete_polygon_.AddPoint(point);
UpdateScene();
}