-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.cc
269 lines (209 loc) · 8.32 KB
/
example.cc
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
// Example for C++ Interface to Gnuplot
// requirements:
// * gnuplot has to be installed (http://www.gnuplot.info/download.html)
// * for Windows: set Path-Variable for Gnuplot path (e.g. C:/program files/gnuplot/bin)
// or set Gnuplot path with: Gnuplot::set_GNUPlotPath(const std::string &path);
#include <iostream>
#include "gnuplot_i.hpp" //Gnuplot class handles POSIX-Pipe-communication with Gnuplot
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
#include <conio.h> //for getch(), needed in wait_for_key()
#include <windows.h> //for Sleep()
#endif
#define NPOINTS 50 // length of array
static void wait_for_key(void); // Program halts until keypress
using std::cout;
using std::endl;
int main(void)
{
// if path-variable for gnuplot is not set, do it with:
// Gnuplot::set_GNUPlotPath("C:/program files/gnuplot/bin/");
#if defined(__APPLE__)
// set a special standard terminal for showonscreen (normally not needed),
// e.g. Mac users who want to use x11 instead of aqua terminal:
Gnuplot::set_terminal_std("x11");
#endif
cout << "*** example of gnuplot control through C++ ***" << endl << endl;
//
// Using the GnuplotException class
//
try
{
Gnuplot g1("lines");
//
// Slopes
//
cout << "*** plotting slopes" << endl;
g1.set_title("Slopes\\nNew Line");
cout << "y = x" << endl;
g1.plot_slope(1.0, 0.0, "y=x");
cout << "y = 2*x" << endl;
g1.plot_slope(2.0, 0.0, "y=2x");
cout << "y = -x" << endl;
g1.plot_slope(-1.0, 0.0, "y=-x");
g1.unset_title();
//
// Equations
//
g1.reset_plot();
cout << endl << endl << "*** various equations" << endl;
cout << "y = sin(x)" << endl;
g1.plot_equation("sin(x)", "sine");
cout << "y = log(x)" << endl;
g1.plot_equation("log(x)", "logarithm");
cout << "y = sin(x) * cos(2*x)" << endl;
g1.plot_equation("sin(x)*cos(2*x)", "sine product");
//
// Styles
//
g1.reset_plot();
cout << endl << endl << "*** showing styles" << endl;
cout << "sine in points" << endl;
g1.set_pointsize(0.8).set_style("points");
g1.plot_equation("sin(x)", "points");
cout << "sine in impulses" << endl;
g1.set_style("impulses");
g1.plot_equation("sin(x)", "impulses");
cout << "sine in steps" << endl;
g1.set_style("steps");
g1.plot_equation("sin(x)", "steps");
//
// Save to ps
//
g1.reset_all();
cout << endl << endl << "*** save to ps " << endl;
cout << "y = sin(x) saved to test_output.ps in working directory" << endl;
// g1.savetops("test_output");
g1.savetofigure("test_output.ps", "postscript color");
g1.set_style("lines").set_samples(300).set_xrange(0, 5);
g1.plot_equation("sin(12*x)*exp(-x)").plot_equation("exp(-x)");
g1.showonscreen(); // window output
//
// User defined 1d, 2d and 3d point sets
//
std::vector<double> x;
std::vector<double> y;
std::vector<double> y2;
std::vector<double> dy;
std::vector<double> z;
for (unsigned int i = 0; i < NPOINTS; ++i) // fill double arrays x, y, z
{
x.push_back((double)i); // x[i] = i
y.push_back((double)i * (double)i); // y[i] = i^2
z.push_back( x[i]*y[i] ); // z[i] = x[i]*y[i] = i^3
dy.push_back((double)i * (double)i / (double) 10); // dy[i] = i^2 / 10
}
y2.push_back(0.00);
y2.push_back(0.78);
y2.push_back(0.97);
y2.push_back(0.43);
y2.push_back(-0.44);
y2.push_back(-0.98);
y2.push_back(-0.77);
y2.push_back(0.02);
g1.reset_all();
cout << endl << endl << "*** user-defined lists of doubles" << endl;
g1.set_style("impulses").plot_x(y, "user-defined doubles");
g1.reset_plot();
cout << endl << endl << "*** user-defined lists of points (x,y)" << endl;
g1.set_grid();
g1.set_style("points").plot_xy(x, y, "user-defined points 2d");
g1.reset_plot();
cout << endl << endl << "*** user-defined lists of points (x,y,z)" << endl;
g1.unset_grid();
g1.plot_xyz(x, y, z, "user-defined points 3d");
g1.reset_plot();
cout << endl << endl << "*** user-defined lists of points (x,y,dy)" << endl;
g1.plot_xy_err(x, y, dy, "user-defined points 2d with errorbars");
//
// Multiple output screens
//
cout << endl << endl;
cout << "*** multiple output windows" << endl;
g1.reset_plot();
g1.set_style("lines");
cout << "window 1: sin(x)" << endl;
g1.set_grid().set_samples(600).set_xrange(0, 300);
g1.plot_equation("sin(x)+sin(x*1.1)");
(void)g1.set_xautoscale().replot();
Gnuplot g2;
cout << "window 2: user defined points" << endl;
g2.plot_x(y2, "points");
g2.set_smooth().plot_x(y2, "cspline");
g2.set_smooth("bezier").plot_x(y2, "bezier");
g2.unset_smooth();
Gnuplot g3("lines");
cout << "window 3: log(x)/x" << endl;
g3.set_grid();
g3.plot_equation("log(x)/x", "log(x)/x");
Gnuplot g4("lines");
cout << "window 4: splot x*x+y*y" << endl;
g4.set_zrange(0, 100);
g4.set_xlabel("x-axis").set_ylabel("y-axis").set_zlabel("z-axis");
g4.plot_equation3d("x*x+y*y");
Gnuplot g5("lines");
cout << "window 5: splot with hidden3d" << endl;
(void)g5.set_isosamples(25).set_hidden3d();
g5.plot_equation3d("x*y*y");
Gnuplot g6("lines");
cout << "window 6: splot with contour" << endl;
g6.set_isosamples(60).set_contour();
g6.unset_surface().plot_equation3d("sin(x)*sin(y)+4");
(void)g6.set_surface().replot();
Gnuplot g7("lines");
cout << "window 7: set_samples" << endl;
g7.set_xrange(-30, 20).set_samples(40);
g7.plot_equation("besj0(x)*0.12e1").plot_equation("(x**besj0(x))-2.5");
(void)g7.set_samples(400).replot();
Gnuplot g8("filledcurves");
cout << "window 8: filledcurves" << endl;
g8.set_legend("outside right top").set_xrange(-5, 5);
g8.plot_equation("x*x").plot_equation("-x*x+4");
//
// Plot an image
//
Gnuplot g9;
cout << "window 9: plot_image" << endl;
constexpr const int unsigned uiWidth = 255U;
constexpr const int unsigned uiHeight = 255U;
g9.set_xrange(0, uiWidth).set_yrange(0, uiHeight).set_cbrange(0, 255);
g9.cmd("set palette gray");
unsigned char ucPicBuf[uiWidth * uiHeight];
// generate a greyscale image
for(unsigned int uiIndex = 0; uiIndex < uiHeight * uiWidth; ++uiIndex)
{
ucPicBuf[uiIndex] = static_cast<unsigned char>(uiIndex % 255U);
}
g9.plot_image(ucPicBuf, uiWidth, uiHeight, "greyscale");
g9.set_pointsize(0.6).unset_legend().plot_slope(0.8, 20);
//
// manual control
//
Gnuplot g10;
cout << "window 10: manual control" << endl;
g10.cmd("set samples 400").cmd("plot abs(x)/2"); // either with cmd()
g10 << "replot sqrt(x)";
g10 << "replot sqrt(-x)";
wait_for_key();
}
catch (GnuplotException &ge)
{
cout << ge.what() << endl;
}
cout << endl << "*** end of gnuplot example" << endl;
return 0;
}
static void wait_for_key (void)
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) // every keypress registered, also arrow keys
std::cout << std::endl << "Press any key to continue..." << std::endl;
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
(void)_getch();
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
std::cout << std::endl << "Press ENTER to continue..." << std::endl;
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
(void)std::cin.get();
#endif
}