Skip to content

Commit e17c934

Browse files
authored
Fix quiver scaling to respect user input (#280)
* change scaling to be consistant across all directional vectors * increase scaling to keep the same plot
1 parent 8dbea7d commit e17c934

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

examples/vector_fields/quiver3/quiver3_1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int main() {
1616
auto V = get_v();
1717
auto W = get_w();
1818

19-
quiver3(Z, U, V, W);
19+
quiver3(Z, U, V, W, 5);
2020
view(-35, 45);
2121

2222
show();

source/matplot/core/axes_type.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4551,15 +4551,23 @@ namespace matplot {
45514551
double v_max = *std::max_element(v.begin(), v.end());
45524552
double y_max = ydiffmin != y_diff.end() ? *ydiffmin : v_max;
45534553

4554+
// scale u, v with same ratio to retain direction
4555+
double mag_max = 1;
4556+
for (size_t i = 0; i < u.size(); ++i) {
4557+
double mag = sqrt(u[i] * u[i] + v[i] * v[i]);
4558+
mag_max = mag > mag_max ? mag : mag_max;
4559+
}
4560+
double val_max = std::min(x_max, y_max);
4561+
45544562
auto u_scaled =
45554563
(scale != 0.)
45564564
? transform(
4557-
u, [&](double u) { return (u / u_max) * scale * x_max; })
4565+
u, [&](double u) { return (u / mag_max) * scale * val_max; })
45584566
: u;
45594567
auto v_scaled =
45604568
(scale != 0.)
45614569
? transform(
4562-
v, [&](double v) { return (v / v_max) * scale * y_max; })
4570+
v, [&](double v) { return (v / mag_max) * scale * val_max; })
45634571
: v;
45644572

45654573
vectors_handle l = std::make_shared<class vectors>(
@@ -4640,20 +4648,28 @@ namespace matplot {
46404648
double w_max = *std::max_element(w.begin(), w.end());
46414649
double z_max = zdiffmin != z_diff.end() ? *zdiffmin : w_max;
46424650

4651+
// scale u, v, w with same ratio to retain direction
4652+
double mag_max = 1;
4653+
for (size_t i = 0; i < u.size(); ++i) {
4654+
double mag = sqrt(u[i] * u[i] + v[i] * v[i] + w[i] * w[i]);
4655+
mag_max = mag > mag_max ? mag : mag_max;
4656+
}
4657+
double val_max = std::min(std::min(x_max, y_max), z_max);
4658+
46434659
auto u_scaled =
46444660
(scale != 0.)
46454661
? transform(
4646-
u, [&](double u) { return (u / u_max) * scale * x_max; })
4662+
u, [&](double u) { return (u / mag_max) * scale * val_max; })
46474663
: u;
46484664
auto v_scaled =
46494665
(scale != 0.)
46504666
? transform(
4651-
v, [&](double v) { return (v / v_max) * scale * y_max; })
4667+
v, [&](double v) { return (v / mag_max) * scale * val_max; })
46524668
: v;
46534669
auto w_scaled =
46544670
(scale != 0.)
46554671
? transform(
4656-
w, [&](double w) { return (w / w_max) * scale * z_max; })
4672+
w, [&](double w) { return (w / mag_max) * scale * val_max; })
46574673
: w;
46584674

46594675
vectors_handle l = std::make_shared<class vectors>(

0 commit comments

Comments
 (0)