Skip to content

Commit ea526f2

Browse files
committed
clang format
1 parent aff6f64 commit ea526f2

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

examples/update.cpp

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,57 @@
11
#define _USE_MATH_DEFINES
2-
#include <cmath>
32
#include "../matplotlibcpp.h"
43
#include <chrono>
4+
#include <cmath>
55

66
namespace plt = matplotlibcpp;
77

88
void update_window(const double x, const double y, const double t,
9-
std::vector<double> &xt, std::vector<double> &yt)
10-
{
11-
const double target_length = 300;
12-
const double half_win = (target_length/(2.*sqrt(1.+t*t)));
13-
14-
xt[0] = x - half_win;
15-
xt[1] = x + half_win;
16-
yt[0] = y - half_win*t;
17-
yt[1] = y + half_win*t;
9+
std::vector<double> &xt, std::vector<double> &yt) {
10+
const double target_length = 300;
11+
const double half_win = (target_length / (2. * sqrt(1. + t * t)));
12+
13+
xt[0] = x - half_win;
14+
xt[1] = x + half_win;
15+
yt[0] = y - half_win * t;
16+
yt[1] = y + half_win * t;
1817
}
1918

19+
int main() {
20+
size_t n = 1000;
21+
std::vector<double> x, y;
2022

21-
int main()
22-
{
23-
size_t n = 1000;
24-
std::vector<double> x, y;
23+
const double w = 0.05;
24+
const double a = n / 2;
2525

26-
const double w = 0.05;
27-
const double a = n/2;
26+
for (size_t i = 0; i < n; i++) {
27+
x.push_back(i);
28+
y.push_back(a * sin(w * i));
29+
}
2830

29-
for (size_t i=0; i<n; i++) {
30-
x.push_back(i);
31-
y.push_back(a*sin(w*i));
32-
}
31+
std::vector<double> xt(2), yt(2);
3332

34-
std::vector<double> xt(2), yt(2);
33+
plt::title("Tangent of a sine curve");
34+
plt::xlim(x.front(), x.back());
35+
plt::ylim(-a, a);
36+
plt::axis("equal");
3537

36-
plt::title("Tangent of a sine curve");
37-
plt::xlim(x.front(), x.back());
38-
plt::ylim(-a, a);
39-
plt::axis("equal");
38+
// Plot sin once and for all.
39+
plt::named_plot("sin", x, y);
4040

41-
// Plot sin once and for all.
42-
plt::named_plot("sin", x, y);
41+
// Prepare plotting the tangent.
42+
plt::Plot plot("tangent");
4343

44-
// Prepare plotting the tangent.
45-
plt::Plot plot("tangent");
44+
plt::legend();
4645

47-
plt::legend();
46+
for (size_t i = 0; i < n; i++) {
47+
if (i % 10 == 0) {
48+
update_window(x[i], y[i], a * w * cos(w * x[i]), xt, yt);
4849

49-
for (size_t i=0; i<n; i++) {
50-
if (i % 10 == 0) {
51-
update_window(x[i], y[i], a*w*cos(w*x[i]), xt, yt);
50+
// Just update data for this plot.
51+
plot.update(xt, yt);
5252

53-
// Just update data for this plot.
54-
plot.update(xt, yt);
55-
56-
// Small pause so the viewer has a chance to enjoy the animation.
57-
plt::pause(0.1);
58-
}
59-
}
53+
// Small pause so the viewer has a chance to enjoy the animation.
54+
plt::pause(0.1);
55+
}
56+
}
6057
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy