-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpp-trig.cpp
More file actions
134 lines (90 loc) · 2.4 KB
/
cpp-trig.cpp
File metadata and controls
134 lines (90 loc) · 2.4 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
/* vim: set noet sw=8 ts=8 tw=120: */
#include <glib.h>
#include <stdio.h>
#include <locale.h>
#include <math.h>
#include <vector>
#include <array>
#include "blot.hpp"
#define DATA_COUNT 1000
static std::vector<double> data_xs(DATA_COUNT);
static std::vector<double> sin_ys(DATA_COUNT);
static std::vector<double> cos_ys(DATA_COUNT);
static std::vector<double> tan_ys(DATA_COUNT);
#define X_MIN -7
#define X_MAX 7
#define Y_MIN -2
#define Y_MAX 2
blot_color data_color = 9;
int main(void)
{
setlocale(LC_CTYPE, "");
/* build a dummy dataset */
for (int i=0; i<DATA_COUNT; i++) {
double x, y;
/* number between -1 and 1 */
x = (double)(i-(DATA_COUNT/2)) / (DATA_COUNT/2);
/* number between -2π and 2π */
x = x * 2 * M_PI;
data_xs[i] = x;
y = sin(x);
sin_ys[i] = y;
y = cos(x);
cos_ys[i] = y;
y = tan(x);
tan_ys[i] = y;
}
/* configure the figure */
Blot::Figure fig;
fig.set_axis_color(8);
fig.set_screen_size(80, 40);
fig.set_x_limits(X_MIN, X_MAX);
fig.set_y_limits(Y_MIN, Y_MAX);
#if 1
/* hack for now to add origin lines */
/* plot X-axis origin */
std::array<std::vector<gint32>,5> xax;
std::array<std::vector<gint32>,5> xay;
for (int i=Y_MIN; i<=Y_MAX; i++) {
xax[Y_MAX+i] = { X_MIN, X_MAX };
xay[Y_MAX+i] = { i, i };
fig.line(xax[Y_MAX+i], xay[Y_MAX+i], i==0 ? 15 : 8, NULL);
}
/* plot Y-axis origin */
std::array<std::vector<gint32>,15> yax;
std::array<std::vector<gint32>,15> yay;
for (int i=X_MIN; i<=X_MAX; i++) {
yax[X_MAX+i] = { i, i };
yay[X_MAX+i] = { Y_MIN, Y_MAX };
fig.line(yax[X_MAX+i], yay[X_MAX+i], i==0 ? 15 : 8, NULL);
}
/* plot Y-axis origin */
std::array<std::vector<double>,15> ypx;
std::array<std::vector<gint32>,15> ypy;
for (int i=Y_MIN; i<=Y_MAX; i++) {
ypx[Y_MAX+i] = { i * M_PI, i * M_PI };
ypy[Y_MAX+i] = { Y_MIN, Y_MAX };
fig.line(ypx[Y_MAX+i], ypy[Y_MAX+i], 15, NULL);
}
#endif
/* add a scatter plot */
fig.scatter(data_xs, sin_ys, data_color, "sin");
/* add a scatter plot */
#if 1
fig.scatter(data_xs, cos_ys, data_color+1, "cos");
#endif
/* add a scatter plot */
#if 1
fig.scatter(data_xs, tan_ys, data_color+2, "tan");
#endif
/* render the plots */
blot_render_flags flags
= BLOT_RENDER_BRAILLE
| BLOT_RENDER_LEGEND_ABOVE;
Blot::Screen scr = fig.render(flags);
/* print it to screen */
gsize txt_size = 0;
const wchar_t *txt = scr.get_text(txt_size);
printf("%ls\n", txt);
return 0;
}