Skip to content

Commit f1e320a

Browse files
committed
Simplify quiver3_2.cpp example
1 parent 0ed629a commit f1e320a

File tree

1 file changed

+53
-83
lines changed

1 file changed

+53
-83
lines changed
Lines changed: 53 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
1-
#include <array>
21
#include <cmath>
32
#include <matplot/matplot.h>
43
#include <vector>
54

6-
std::vector<std::array<double, 3>> get_position();
7-
std::vector<std::array<double, 3>>
8-
get_vectors(const std::vector<std::array<double, 3>> &positions);
5+
std::vector<double> get_x();
6+
std::vector<double> get_y();
7+
std::vector<double> get_z();
98

109
int main() {
1110
using namespace matplot;
1211

13-
auto positions = get_position();
14-
auto vectors = get_vectors(positions);
15-
vector_1d x;
16-
vector_1d y;
17-
vector_1d z;
18-
vector_1d u;
19-
vector_1d v;
20-
vector_1d w;
21-
vector_1d m;
22-
for (const auto &p : positions) {
23-
x.emplace_back(p[0]);
24-
y.emplace_back(p[1]);
25-
z.emplace_back(p[2]);
26-
}
27-
for (const auto &vec : vectors) {
28-
u.emplace_back(vec[0]);
29-
v.emplace_back(vec[1]);
30-
w.emplace_back(vec[2]);
31-
m.emplace_back(
32-
sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]));
12+
// Positions
13+
vector_1d x = get_x();
14+
vector_1d y = get_y();
15+
vector_1d z = get_z();
16+
17+
// Vectors
18+
vector_1d u, v, w, m;
19+
for (size_t i = 0; i < x.size(); ++i) {
20+
double t = std::exp(-std::pow(x[i], 2) - std::pow(y[i], 2) -
21+
std::pow(z[i], 2));
22+
u.emplace_back(x[i] * t);
23+
v.emplace_back(y[i] * t);
24+
w.emplace_back(z[i] * t);
25+
m.emplace_back(std::sqrt(x[i] * x[i] + y[i] * y[i] + z[i] * z[i]));
3326
}
3427

3528
quiver3(x, y, z, u, v, w, m, 0.3)->normalize(true).line_width(2);
@@ -38,66 +31,43 @@ int main() {
3831
return 0;
3932
}
4033

41-
std::vector<std::array<double, 3>>
42-
get_vectors(const std::vector<std::array<double, 3>> &positions) {
43-
std::vector<std::array<double, 3>> vectors;
44-
for (const auto &pos : positions) {
45-
std::array<double, 3> result{0, 0, 0};
46-
result[0] =
47-
pos[0] * exp(-pow(pos[0], 2) - pow(pos[1], 2) - pow(pos[2], 2));
48-
result[1] =
49-
pos[1] * exp(-pow(pos[0], 2) - pow(pos[1], 2) - pow(pos[2], 2));
50-
result[2] =
51-
pos[2] * exp(-pow(pos[0], 2) - pow(pos[1], 2) - pow(pos[2], 2));
52-
vectors.emplace_back(result);
53-
}
54-
return vectors;
34+
std::vector<double> get_x() {
35+
return {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37+
-1, -1, -1, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5,
38+
-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5,
39+
-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0, 0, 0, 0, 0,
40+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0.5,
42+
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
43+
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
44+
0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46+
1, 1, 1, 1};
5547
}
5648

57-
std::vector<std::array<double, 3>> get_position() {
58-
std::vector<std::array<double, 3>> p = {
59-
{-1.0, -1.0, -1.0}, {-1.0, -1.0, -0.5}, {-1.0, -1.0, 0.0},
60-
{-1.0, -1.0, 0.5}, {-1.0, -1.0, 1.0}, {-1.0, -0.5, -1.0},
61-
{-1.0, -0.5, -0.5}, {-1.0, -0.5, 0.0}, {-1.0, -0.5, 0.5},
62-
{-1.0, -0.5, 1.0}, {-1.0, 0.0, -1.0}, {-1.0, 0.0, -0.5},
63-
{-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.5}, {-1.0, 0.0, 1.0},
64-
{-1.0, 0.5, -1.0}, {-1.0, 0.5, -0.5}, {-1.0, 0.5, 0.0},
65-
{-1.0, 0.5, 0.5}, {-1.0, 0.5, 1.0}, {-1.0, 1.0, -1.0},
66-
{-1.0, 1.0, -0.5}, {-1.0, 1.0, 0.0}, {-1.0, 1.0, 0.5},
67-
{-1.0, 1.0, 1.0}, {-0.5, -1.0, -1.0}, {-0.5, -1.0, -0.5},
68-
{-0.5, -1.0, 0.0}, {-0.5, -1.0, 0.5}, {-0.5, -1.0, 1.0},
69-
{-0.5, -0.5, -1.0}, {-0.5, -0.5, -0.5}, {-0.5, -0.5, 0.0},
70-
{-0.5, -0.5, 0.5}, {-0.5, -0.5, 1.0}, {-0.5, 0.0, -1.0},
71-
{-0.5, 0.0, -0.5}, {-0.5, 0.0, 0.0}, {-0.5, 0.0, 0.5},
72-
{-0.5, 0.0, 1.0}, {-0.5, 0.5, -1.0}, {-0.5, 0.5, -0.5},
73-
{-0.5, 0.5, 0.0}, {-0.5, 0.5, 0.5}, {-0.5, 0.5, 1.0},
74-
{-0.5, 1.0, -1.0}, {-0.5, 1.0, -0.5}, {-0.5, 1.0, 0.0},
75-
{-0.5, 1.0, 0.5}, {-0.5, 1.0, 1.0}, {0.0, -1.0, -1.0},
76-
{0.0, -1.0, -0.5}, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.5},
77-
{0.0, -1.0, 1.0}, {0.0, -0.5, -1.0}, {0.0, -0.5, -0.5},
78-
{0.0, -0.5, 0.0}, {0.0, -0.5, 0.5}, {0.0, -0.5, 1.0},
79-
{0.0, 0.0, -1.0}, {0.0, 0.0, -0.5}, {0.0, 0.0, 0.0},
80-
{0.0, 0.0, 0.5}, {0.0, 0.0, 1.0}, {0.0, 0.5, -1.0},
81-
{0.0, 0.5, -0.5}, {0.0, 0.5, 0.0}, {0.0, 0.5, 0.5},
82-
{0.0, 0.5, 1.0}, {0.0, 1.0, -1.0}, {0.0, 1.0, -0.5},
83-
{0.0, 1.0, 0.0}, {0.0, 1.0, 0.5}, {0.0, 1.0, 1.0},
84-
{0.5, -1.0, -1.0}, {0.5, -1.0, -0.5}, {0.5, -1.0, 0.0},
85-
{0.5, -1.0, 0.5}, {0.5, -1.0, 1.0}, {0.5, -0.5, -1.0},
86-
{0.5, -0.5, -0.5}, {0.5, -0.5, 0.0}, {0.5, -0.5, 0.5},
87-
{0.5, -0.5, 1.0}, {0.5, 0.0, -1.0}, {0.5, 0.0, -0.5},
88-
{0.5, 0.0, 0.0}, {0.5, 0.0, 0.5}, {0.5, 0.0, 1.0},
89-
{0.5, 0.5, -1.0}, {0.5, 0.5, -0.5}, {0.5, 0.5, 0.0},
90-
{0.5, 0.5, 0.5}, {0.5, 0.5, 1.0}, {0.5, 1.0, -1.0},
91-
{0.5, 1.0, -0.5}, {0.5, 1.0, 0.0}, {0.5, 1.0, 0.5},
92-
{0.5, 1.0, 1.0}, {1.0, -1.0, -1.0}, {1.0, -1.0, -0.5},
93-
{1.0, -1.0, 0.0}, {1.0, -1.0, 0.5}, {1.0, -1.0, 1.0},
94-
{1.0, -0.5, -1.0}, {1.0, -0.5, -0.5}, {1.0, -0.5, 0.0},
95-
{1.0, -0.5, 0.5}, {1.0, -0.5, 1.0}, {1.0, 0.0, -1.0},
96-
{1.0, 0.0, -0.5}, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.5},
97-
{1.0, 0.0, 1.0}, {1.0, 0.5, -1.0}, {1.0, 0.5, -0.5},
98-
{1.0, 0.5, 0.0}, {1.0, 0.5, 0.5}, {1.0, 0.5, 1.0},
99-
{1.0, 1.0, -1.0}, {1.0, 1.0, -0.5}, {1.0, 1.0, 0.0},
100-
{1.0, 1.0, 0.5}, {1.0, 1.0, 1.0}};
101-
return p;
49+
std::vector<double> get_y() {
50+
return {-1, -1, -1, -1, -1, -0.5, -0.5, -0.5, -0.5, -0.5, 0, 0,
51+
0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1,
52+
1, -1, -1, -1, -1, -1, -0.5, -0.5, -0.5, -0.5, -0.5, 0,
53+
0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1,
54+
1, 1, -1, -1, -1, -1, -1, -0.5, -0.5, -0.5, -0.5, -0.5,
55+
0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1,
56+
1, 1, 1, -1, -1, -1, -1, -1, -0.5, -0.5, -0.5, -0.5,
57+
-0.5, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 1,
58+
1, 1, 1, 1, -1, -1, -1, -1, -1, -0.5, -0.5, -0.5,
59+
-0.5, -0.5, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5,
60+
1, 1, 1, 1, 1};
10261
}
10362

63+
std::vector<double> get_z() {
64+
return {-1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1,
65+
-1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1,
66+
-1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1,
67+
-1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1,
68+
-1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1,
69+
-1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1,
70+
-1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1,
71+
-1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1, -1, -0.5, 0, 0.5, 1,
72+
-1, -0.5, 0, 0.5, 1};
73+
}

0 commit comments

Comments
 (0)