-
Notifications
You must be signed in to change notification settings - Fork 10
/
drawille.hpp
55 lines (45 loc) · 1.17 KB
/
drawille.hpp
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
#ifndef DRAWILLE_HPP
#define DRAWILLE_HPP
#include <vector>
#include <ostream>
namespace Drawille {
using std::vector;
using std::wostream;
constexpr size_t pixmap[4][2] = {
{0x01, 0x08},
{0x02, 0x10},
{0x04, 0x20},
{0x40, 0x80}
};
constexpr wchar_t braille = 0x2800;
class Canvas {
public:
Canvas(size_t width, size_t height) {
this->canvas.resize(height);
for(auto& v: this->canvas)
v.resize(width);
}
void set(size_t x, size_t y) {
if(x > (this->canvas[0].size() * 2) or x < 1) x = 0;
if(y > (this->canvas.size() * 4) or y < 1) y = 0;
this->canvas[y / 4][x / 2] |= pixmap[y % 4][x % 2];
}
void unset(size_t x, size_t y) {
if(x > (this->canvas[0].size() * 2) or x < 1) x = 0;
if(y > (this->canvas.size() * 4) or y < 1) y = 0;
this->canvas[y / 4][x / 2] &= ~pixmap[y % 4][x % 2];
}
void draw(wostream& strm) {
for(auto& v: this->canvas) {
for(auto& c: v) {
if(c == 0) strm << " ";
else strm << std::wstring{braille+c};
}
strm << std::endl;
}
}
protected:
vector<vector<wchar_t>> canvas;
};
}
#endif