Skip to content

Commit f2986bb

Browse files
committed
updated sinusoidal_animation.cpp
1 parent 940d9ce commit f2986bb

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,55 @@
11
#include "../../include/cppyplot.hpp"
22
#include <algorithm>
3+
#include <random>
34

45
int main()
56
{
67
Cppyplot::cppyplot pyp;
78

89
std::vector<float> vec(500, 0.0F);
9-
std::vector<float> sine;
10+
std::vector<float> sine, cosine;
1011
sine.reserve(500);
12+
cosine.reserve(500);
1113
std::iota(vec.begin(), vec.end(), 0.0F);
14+
15+
std::random_device seed;
16+
std::mt19937 gen(seed());
17+
std::uniform_real_distribution<float> uniform(0.0F, 1.0F);
18+
1219
std::transform(vec.begin(), vec.end(), std::back_inserter(sine),
13-
[](auto& elem){return std::sinf(elem * 3.14159265F/180.0F);});
20+
[&](auto& elem){return uniform(gen)*std::sinf(elem * 3.14159265F/180.0F);});
21+
22+
std::transform(vec.begin(), vec.end(), std::back_inserter(cosine),
23+
[&](auto& elem){return uniform(gen)*std::cosf(elem * 3.14159265F/180.0F);});
1424

1525
pyp.raw(R"pyp(
1626
1727
fig = plt.figure(figsize=(6,5))
1828
ax = plt.axes(xlim=(0, 500), ylim=(-1.5,1.5))
19-
line, = ax.plot([], [], lw=1.5)
29+
line1, = ax.plot([], [], 'r-o', markersize=1, linewidth=0.8)
30+
line2, = ax.plot([], [], 'k-o', markersize=1, linewidth=0.8)
2031
plt.grid(True)
2132
plt.xlabel("Angle (deg)", fontsize=12)
22-
plt.ylabel("Sine", fontsize=12)
23-
plt.title("Sinusoidal",fontsize=14)
33+
plt.title("Sine and Cosine",fontsize=14)
2434
2535
def anim_init():
26-
line.set_data([], [])
27-
return line,
36+
line1.set_data([], [])
37+
line2.set_data([], [])
38+
return (line1, line2,)
2839
2940
def anim_update(frame):
30-
x = vec[0:frame]
31-
y = sine[0:frame]
32-
line.set_data(x,y)
33-
return line,
41+
x1 = vec[0:frame]
42+
y1 = sine[0:frame]
43+
x2 = vec[-frame:-1]
44+
y2 = cosine[-frame:-1]
45+
line1.set_data(x1,y1)
46+
line2.set_data(x2,y2)
47+
return (line1,line2,)
3448
3549
anim = FuncAnimation(fig, anim_update, init_func=anim_init,
3650
frames=500, interval=2, blit=True)
37-
51+
anim.save("misc/sin.gif")
3852
plt.show()
39-
)pyp", _p(vec), _p(sine));
53+
)pyp", _p(vec), _p(sine), _p(cosine));
4054

4155
}

misc/sin.gif

3.62 MB
Loading

0 commit comments

Comments
 (0)