-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrimitive.mpp
More file actions
215 lines (192 loc) · 5.38 KB
/
Primitive.mpp
File metadata and controls
215 lines (192 loc) · 5.38 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
export module CppUtils.Terminal.Primitive;
import std;
import CppUtils.Terminal.Area;
import CppUtils.Terminal.CharAttributes;
import CppUtils.Container.Size;
export namespace CppUtils::Terminal
{
enum class PrimitiveStyle
{
Outline,
Filled
};
inline auto drawLine(WritableAreaView& view, const Container::Size2& start, const Container::Size2& end, const CharAttributes& c) -> void
{
// https://fr.wikipedia.org/wiki/Algorithme_de_trac%C3%A9_de_segment_de_Bresenham
auto x0 = static_cast<int>(start.x());
auto y0 = static_cast<int>(start.y());
const auto x1 = static_cast<int>(end.x());
const auto y1 = static_cast<int>(end.y());
const auto deltaX = std::abs(x1 - x0);
const auto deltaY = -std::abs(y1 - y0);
const auto stepX = x0 < x1 ? 1 : -1;
const auto stepY = y0 < y1 ? 1 : -1;
auto drift = deltaX + deltaY;
while (true)
{
view.setChar({static_cast<std::size_t>(x0), static_cast<std::size_t>(y0)}, c);
if (x0 == x1 and y0 == y1)
break;
auto doubleDrift = 2 * drift;
if (doubleDrift >= deltaY)
{
drift += deltaY;
x0 += stepX;
}
if (doubleDrift <= deltaX)
{
drift += deltaX;
y0 += stepY;
}
}
}
inline auto drawRectangle(WritableAreaView& view, const Container::Size2& topLeft, const Container::Size2& size, PrimitiveStyle style, const CharAttributes& c) -> void
{
if (size.width() == 0 or size.height() == 0)
return;
const auto x = topLeft.x();
const auto y = topLeft.y();
const auto width = size.width();
const auto height = size.height();
if (style == PrimitiveStyle::Outline)
{
const auto topRight = Container::Size2{x + width - 1, y};
const auto bottomLeft = Container::Size2{x, y + height - 1};
const auto bottomRight = Container::Size2{x + width - 1, y + height - 1};
drawLine(view, topLeft, topRight, c);
drawLine(view, bottomLeft, bottomRight, c);
drawLine(view, topLeft, bottomLeft, c);
drawLine(view, topRight, bottomRight, c);
}
else
for (auto currentY = y; currentY < y + height; ++currentY)
for (auto currentX = x; currentX < x + width; ++currentX)
view.setChar({currentX, currentY}, c);
}
inline auto drawCircle(WritableAreaView& view, const Container::Size2& center, std::size_t radius, PrimitiveStyle style, const CharAttributes& c) -> void
{
// https://fr.wikipedia.org/wiki/Algorithme_de_trac%C3%A9_d%27arc_de_cercle_de_Bresenham
auto centerX = static_cast<int>(center.x());
auto centerY = static_cast<int>(center.y());
auto signedRadius = static_cast<int>(radius);
auto x = 0;
auto y = signedRadius;
auto drift = 3 - 2 * signedRadius;
auto drawPoint = [&](int px, int py) {
if (px >= 0 and py >= 0)
view.setChar({static_cast<std::size_t>(px), static_cast<std::size_t>(py)}, c);
};
while (y >= x)
{
if (style == PrimitiveStyle::Outline)
{
drawPoint(centerX + x, centerY + y);
drawPoint(centerX - x, centerY + y);
drawPoint(centerX + x, centerY - y);
drawPoint(centerX - x, centerY - y);
drawPoint(centerX + y, centerY + x);
drawPoint(centerX - y, centerY + x);
drawPoint(centerX + y, centerY - x);
drawPoint(centerX - y, centerY - x);
}
else
{
for (auto i = centerX - x; i <= centerX + x; ++i)
{
drawPoint(i, centerY + y);
drawPoint(i, centerY - y);
}
for (auto i = centerX - y; i <= centerX + y; ++i)
{
drawPoint(i, centerY + x);
drawPoint(i, centerY - x);
}
}
x++;
if (drift > 0)
{
y--;
drift = drift + 4 * (x - y) + 10;
}
else
drift = drift + 4 * x + 6;
}
}
inline auto drawEllipse(WritableAreaView& view, const Container::Size2& p1, const Container::Size2& p2, PrimitiveStyle style, const CharAttributes& c) -> void
{
// https://fr.wikipedia.org/wiki/Algorithme_de_trac%C3%A9_d%27arc_de_cercle_de_Bresenham
auto x0 = static_cast<int>(p1.x());
auto y0 = static_cast<int>(p1.y());
auto x1 = static_cast<int>(p2.x());
auto y1 = static_cast<int>(p2.y());
if (x0 > x1)
std::swap(x0, x1);
if (y0 > y1)
std::swap(y0, y1);
auto width = std::abs(x1 - x0);
auto height = std::abs(y1 - y0);
auto isHeightOdd = height & 1;
auto deltaX = 4 * (1.0 - width) * height * height;
auto deltaY = 4.0 * (isHeightOdd + 1) * width * width;
auto drift = deltaX + deltaY + isHeightOdd * width * width;
auto doubleDrift = 0.0;
y0 += (height + 1) / 2;
y1 = y0 - isHeightOdd;
width *= 8 * width;
isHeightOdd = 8 * height * height;
auto drawPoint = [&](int px, int py) {
if (px >= 0 and py >= 0)
view.setChar({static_cast<std::size_t>(px), static_cast<std::size_t>(py)}, c);
};
do
{
if (style == PrimitiveStyle::Outline)
{
drawPoint(x1, y0);
drawPoint(x0, y0);
drawPoint(x0, y1);
drawPoint(x1, y1);
}
else
for (auto x = x0; x <= x1; ++x)
{
drawPoint(x, y0);
drawPoint(x, y1);
}
doubleDrift = 2 * drift;
if (doubleDrift <= deltaY)
{
++y0;
--y1;
drift += deltaY += width;
}
if (doubleDrift >= deltaX || 2 * drift > deltaY)
{
++x0;
--x1;
drift += deltaX += isHeightOdd;
}
}
while (x0 <= x1);
while (y0 - y1 < height)
{
if (style == PrimitiveStyle::Outline)
{
drawPoint(x0 - 1, y0);
drawPoint(x1 + 1, y0++);
drawPoint(x0 - 1, y1);
drawPoint(x1 + 1, y1--);
}
else
{
for (auto x = x0 - 1; x <= x1 + 1; ++x)
{
drawPoint(x, y0);
drawPoint(x, y1);
}
++y0;
--y1;
}
}
}
}