-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.hpp
More file actions
184 lines (160 loc) · 5.71 KB
/
backend.hpp
File metadata and controls
184 lines (160 loc) · 5.71 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
/**
* @file backend.hpp
* @brief Abstract backend interface for rendering
*/
#ifndef CPPPLOT_BACKENDS_BACKEND_HPP
#define CPPPLOT_BACKENDS_BACKEND_HPP
// Define M_PI for Windows compatibility
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "../core/types.hpp"
#include "../core/color.hpp"
#include "../core/style.hpp"
#include <string>
#include <vector>
#include <cmath>
namespace cppplot {
/**
* @brief Abstract rendering backend
*/
class Backend {
public:
virtual ~Backend() = default;
// Canvas setup
virtual void setSize(int width, int height) = 0;
virtual int width() const = 0;
virtual int height() const = 0;
// Background
virtual void clear(const Color& color = Color::white()) = 0;
// Clipping
virtual void setClipRect(const Rect& rect) = 0;
virtual void clearClip() = 0;
// Primitives
virtual void drawLine(double x1, double y1, double x2, double y2,
const LineStyle& style) = 0;
virtual void drawPolyline(const std::vector<Point>& points,
const LineStyle& style) = 0;
virtual void drawRect(double x, double y, double width, double height,
const Color& fill = Color::transparent(),
const LineStyle& stroke = LineStyle()) = 0;
virtual void drawCircle(double cx, double cy, double radius,
const Color& fill = Color::transparent(),
const LineStyle& stroke = LineStyle()) = 0;
virtual void drawEllipse(double cx, double cy, double rx, double ry,
const Color& fill = Color::transparent(),
const LineStyle& stroke = LineStyle()) = 0;
virtual void drawPolygon(const std::vector<Point>& points,
const Color& fill = Color::transparent(),
const LineStyle& stroke = LineStyle()) = 0;
virtual void drawPath(const std::string& pathData,
const Color& fill = Color::transparent(),
const LineStyle& stroke = LineStyle()) = 0;
virtual void drawText(double x, double y, const std::string& text,
const TextStyle& style) = 0;
// Markers
virtual void drawMarker(double x, double y, const MarkerStyle& style);
// Output
virtual std::string render() const = 0;
virtual void save(const std::string& filename) const = 0;
};
// Default implementation for drawMarker
inline void Backend::drawMarker(double x, double y, const MarkerStyle& style) {
if (!style.isVisible()) return;
double s = style.size / 2;
Color fill = style.faceColor.withAlpha(style.alpha * style.faceColor.aNorm());
LineStyle stroke;
stroke.color = style.edgeColor;
stroke.width = style.edgeWidth;
stroke.alpha = style.alpha;
const std::string& m = style.marker;
if (m == "o" || m == "circle") {
drawCircle(x, y, s, fill, stroke);
}
else if (m == "s" || m == "square") {
drawRect(x - s, y - s, s * 2, s * 2, fill, stroke);
}
else if (m == "^" || m == "triangle_up") {
std::vector<Point> pts = {
{x, y - s},
{x - s, y + s},
{x + s, y + s}
};
drawPolygon(pts, fill, stroke);
}
else if (m == "v" || m == "triangle_down") {
std::vector<Point> pts = {
{x, y + s},
{x - s, y - s},
{x + s, y - s}
};
drawPolygon(pts, fill, stroke);
}
else if (m == "<" || m == "triangle_left") {
std::vector<Point> pts = {
{x - s, y},
{x + s, y - s},
{x + s, y + s}
};
drawPolygon(pts, fill, stroke);
}
else if (m == ">" || m == "triangle_right") {
std::vector<Point> pts = {
{x + s, y},
{x - s, y - s},
{x - s, y + s}
};
drawPolygon(pts, fill, stroke);
}
else if (m == "d" || m == "diamond") {
std::vector<Point> pts = {
{x, y - s},
{x + s, y},
{x, y + s},
{x - s, y}
};
drawPolygon(pts, fill, stroke);
}
else if (m == "+" || m == "plus") {
drawLine(x - s, y, x + s, y, stroke);
drawLine(x, y - s, x, y + s, stroke);
}
else if (m == "x" || m == "cross") {
double d = s * 0.707; // sqrt(2)/2
drawLine(x - d, y - d, x + d, y + d, stroke);
drawLine(x - d, y + d, x + d, y - d, stroke);
}
else if (m == "*" || m == "star") {
// 5-pointed star
for (int i = 0; i < 5; ++i) {
double angle = i * 72 * M_PI / 180 - M_PI / 2;
double x2 = x + s * std::cos(angle);
double y2 = y + s * std::sin(angle);
drawLine(x, y, x2, y2, stroke);
}
}
else if (m == "." || m == "point") {
drawCircle(x, y, style.size / 6, fill, stroke);
}
else if (m == "," || m == "pixel") {
drawRect(x - 0.5, y - 0.5, 1, 1, fill, stroke);
}
else if (m == "h" || m == "hexagon") {
std::vector<Point> pts;
for (int i = 0; i < 6; ++i) {
double angle = i * 60 * M_PI / 180;
pts.push_back({x + s * std::cos(angle), y + s * std::sin(angle)});
}
drawPolygon(pts, fill, stroke);
}
else if (m == "p" || m == "pentagon") {
std::vector<Point> pts;
for (int i = 0; i < 5; ++i) {
double angle = i * 72 * M_PI / 180 - M_PI / 2;
pts.push_back({x + s * std::cos(angle), y + s * std::sin(angle)});
}
drawPolygon(pts, fill, stroke);
}
}
} // namespace cppplot
#endif // CPPPLOT_BACKENDS_BACKEND_HPP