-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample.cpp
211 lines (185 loc) · 4.75 KB
/
example.cpp
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
# include <cstdlib>
# include <iostream>
# include <cstring>
# include <cmath>
# include "gnuplot_i.hpp"
using namespace std;
# define SLEEP_LGTH 1
# define NPOINTS 50
int main ( int argc, char *argv[] );
void slow_print ( string s );
//****************************************************************************80
int main ( int argc, char *argv[] )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for ANIM.
//
// Modified:
//
// 24 June 2011
//
{
double d[NPOINTS];
dpoint dp[NPOINTS];
gnuplot_ctrl *h1;
gnuplot_ctrl *h2;
gnuplot_ctrl *h3;
gnuplot_ctrl *h4;
int i;
int j;
double x[NPOINTS];
double y[NPOINTS];
cout << "\n";
cout << "EXAMPLE:\n";
cout << " C++ version.\n";
cout << " Demonstrate how a running C++ program can produce plots\n";
cout << " through GNUPLOT, by invoking the GNUPLOT_I interface library.\n";
//
// Initialize the gnuplot handle
//
h1 = gnuplot_init ( );
if ( h1 == NULL )
{
cout << "\n";
cout << "EXAMPLE - Fatal error\n";
cout << " GNUPLOT is not available in your path.\n";
exit ( 1 );
}
//
// Slopes
//
gnuplot_setstyle ( h1, "lines" );
slow_print("*** plotting slopes\n");
slow_print("y = x\n");
gnuplot_plot_slope(h1, 1.0, 0.0, "unity slope");
sleep(SLEEP_LGTH);
slow_print("y = 2*x\n");
gnuplot_plot_slope(h1, 2.0, 0.0, "y=2x");
sleep(SLEEP_LGTH);
slow_print("y = -x\n");
gnuplot_plot_slope(h1, -1.0, 0.0, "y=-x");
sleep(SLEEP_LGTH);
//
// Equations
//
gnuplot_resetplot(h1);
cout << "\n\n";
slow_print("*** various equations\n");
slow_print("y = sin(x)\n");
gnuplot_plot_equation(h1, "sin(x)", "sine");
sleep(SLEEP_LGTH);
slow_print("y = log(x)\n");
gnuplot_plot_equation(h1, "log(x)", "logarithm");
sleep(SLEEP_LGTH);
slow_print("y = sin(x)*cos(2*x)\n");
gnuplot_plot_equation(h1, "sin(x)*cos(2*x)", "sine product");
sleep(SLEEP_LGTH);
//
// Styles
//
gnuplot_resetplot(h1);
cout << "\n\n";
slow_print("*** Showing plot style options:\n");
slow_print("sine(x) in points\n");
gnuplot_setstyle(h1, "points");
gnuplot_plot_equation(h1, "sin(x)", "sine");
sleep(SLEEP_LGTH);
slow_print("sine(x) in impulses\n");
gnuplot_setstyle(h1, "impulses");
gnuplot_plot_equation(h1, "sin(x)", "sine");
sleep(SLEEP_LGTH);
slow_print("sine(x) in steps\n");
gnuplot_setstyle(h1, "steps");
gnuplot_plot_equation(h1, "sin(x)", "sine");
sleep(SLEEP_LGTH);
//
// User defined 1d and 2d point sets
//
gnuplot_resetplot(h1);
gnuplot_setstyle(h1, "impulses");
cout <<"\n\n";
slow_print("*** user defined lists of points\n");
slow_print("random doubles\n");
srand48 ( getpid ( ) );
for ( i = 0; i < NPOINTS; i++ )
{
d[i] = drand48();
}
gnuplot_plot1d_var1(h1, d, NPOINTS, "random doubles");
sleep(SLEEP_LGTH);
gnuplot_resetplot(h1);
gnuplot_setstyle(h1, "points");
slow_print("random points\n");
for ( i = 0; i < NPOINTS; i++ )
{
dp[i].x = drand48();
dp[i].y = drand48();
}
gnuplot_plot1d_var2(h1, dp, NPOINTS, "random points");
sleep(SLEEP_LGTH);
gnuplot_resetplot(h1);
gnuplot_setstyle(h1, "points");
slow_print("cosine points with var2v\n");
for ( j = 0; j < NPOINTS; j++ )
{
x[j] = ( double ) j / 10.0;
y[j] = cos ( x[j] );
}
gnuplot_plot1d_var2v ( h1, x, y, NPOINTS, "cosine points" );
sleep(SLEEP_LGTH);
//
// Multiple output screens
//
cout << "\n\n";
slow_print("*** multiple output windows\n");
gnuplot_resetplot(h1);
gnuplot_setstyle(h1, "lines");
h2 = gnuplot_init();
gnuplot_setstyle(h2, "lines");
h3 = gnuplot_init();
gnuplot_setstyle(h3, "lines");
h4 = gnuplot_init();
gnuplot_setstyle(h4, "lines");
slow_print("window 1: sin(x)\n");
gnuplot_plot_equation(h1, "sin(x)", "sin(x)");
sleep(SLEEP_LGTH);
slow_print("window 2: cos(x)\n");
gnuplot_plot_equation(h2, "cos(x)", "cos(x)");
sleep(SLEEP_LGTH);
slow_print("window 3: asin(x)\n");
gnuplot_plot_equation(h3, "asin(x)", "arcsin(x)");
sleep(SLEEP_LGTH);
slow_print("window 4: acos(x)\n");
gnuplot_plot_equation(h4, "acos(x)", "arccos(x)");
sleep(SLEEP_LGTH);
//
// Close gnuplot handles.
//
cout << "\n";
slow_print ( "*** end of gnuplot example\n" );
gnuplot_close ( h1 );
gnuplot_close ( h2 );
gnuplot_close ( h3 );
gnuplot_close ( h4 );
//
// Terminate.
//
cout << "\n";
cout << "EXAMPLE:\n";
cout << " Normal end of execution.\n";
return 0;
}
//****************************************************************************80
void slow_print ( string s )
//****************************************************************************80
{
int i;
for ( i = 0; i < s.length ( ); i++ )
{
cout << s[i] << flush;
usleep ( 100000 );
}
return;
}