48 template <
unsigned COLS,
unsigned ROWS>
52 static_assert(COLS % 2 == 0,
"COLS must be a multiple of 2!");
54 static_assert(ROWS % 4 == 0,
"ROWS must be a multiple of 4!");
56 std::array<std::array<
unsigned char, COLS / 2>, ROWS / 4> masks;
68 void point(
unsigned x,
unsigned y,
bool on =
true)
70 if (x >= COLS || y >= ROWS)
72 unsigned braille_col = x / 2;
73 unsigned braille_row = y / 4;
74 unsigned sub_x = x % 2;
75 unsigned sub_y = y % 4;
76 static const unsigned char mapping[2][4] = {
79 unsigned char bit = 1 << mapping[sub_x][sub_y];
81 masks[braille_row][braille_col] |= bit;
83 masks[braille_row][braille_col] &= ~bit;
95 void line(
int x0,
int y0,
int x1,
int y1,
bool on =
true)
97 int dx = std::abs(x1 - x0);
98 int dy = -std::abs(y1 - y0);
99 int sx = x0 < x1 ? 1 : -1;
100 int sy = y0 < y1 ? 1 : -1;
105 if (x0 == x1 && y0 == y1)
124 void rect(
int x0,
int y0,
int x1,
int y1,
bool on =
true)
126 line(x0, y0, x1, y0, on);
127 line(x1, y0, x1, y1, on);
128 line(x1, y1, x0, y1, on);
129 line(x0, y1, x0, y0, on);
141 void fillRect(
int x0,
int y0,
int x1,
int y1,
bool on =
true)
147 for (
int y = y0; y <= y1; ++y)
148 for (
int x = x0; x <= x1; ++x)
160 void circle(
int cx,
int cy,
int r,
bool on =
true)
166 point(cx + x, cy + y, on);
167 point(cx - x, cy + y, on);
168 point(cx + x, cy - y, on);
169 point(cx - x, cy - y, on);
170 point(cx + y, cy + x, on);
171 point(cx - y, cy + x, on);
172 point(cx + y, cy - x, on);
173 point(cx - y, cy - x, on);
177 d = d + 4 * (x - y) + 10, y--;
196 for (
int i = cx - x; i <= cx + x; ++i)
198 point(i, cy + y, on);
199 point(i, cy - y, on);
201 for (
int i = cx - y; i <= cx + y; ++i)
203 point(i, cy + x, on);
204 point(i, cy - x, on);
213 d += 4 * (x - y) + 10;
223 for (
auto &row : masks)
224 for (
auto &cell : row)
234 void update(std::ostream &os = std::cout,
bool clear =
false)
const
241 out.reserve((ROWS / 4) * (COLS / 2 * 3 + 1));
242 for (
unsigned row = 0; row < ROWS / 4; ++row)
244 for (
unsigned col = 0; col < COLS / 2; ++col)
246 unsigned int code = 0x2800 + masks[row][col];
247 out +=
static_cast<char>(0xE0 | (code >> 12));
248 out +=
static_cast<char>(0x80 | ((code >> 6) & 0x3F));
249 out +=
static_cast<char>(0x80 | (code & 0x3F));
253 os <<
"\033[H" << out << std::flush;
void point(unsigned x, unsigned y, bool on=true)
在屏幕上绘制一个点。
Definition screen.h:68
void circle(int cx, int cy, int r, bool on=true)
在屏幕上绘制空心圆。
Definition screen.h:160
void fillRect(int x0, int y0, int x1, int y1, bool on=true)
在屏幕上绘制实心矩形。
Definition screen.h:141
void rect(int x0, int y0, int x1, int y1, bool on=true)
在屏幕上绘制空心矩形。
Definition screen.h:124
void line(int x0, int y0, int x1, int y1, bool on=true)
在屏幕上绘制一条直线。
Definition screen.h:95
void clear()
清空屏幕。
Definition screen.h:221
void update(std::ostream &os=std::cout, bool clear=false) const
将 Screen 存储的虚拟屏幕输出到指定流。
Definition screen.h:234
Screen()
默认构造函数,构造一个空白的 Screen
Definition screen.h:60
void fillCircle(int cx, int cy, int r, bool on=true)
在屏幕上绘制实心圆。
Definition screen.h:190