Skip to content

Commit 267c944

Browse files
added scaling fix
1 parent c5563f2 commit 267c944

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

source/matplot/axes_objects/vectors.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ namespace matplot {
117117
}
118118

119119
if (normalize_ && m_value != 0) {
120-
double length = sqrt(u_value * u_value + v_value * v_value +
120+
double mag = sqrt(u_value * u_value + v_value * v_value +
121121
w_value * w_value);
122-
double scale = length / m_value;
123-
u_value *= scale / length;
124-
v_value *= scale / length;
125-
w_value *= scale / length;
122+
double scale = mag / m_value;
123+
u_value *= scale / mag;
124+
v_value *= scale / mag;
125+
w_value *= scale / mag;
126126
}
127127

128128
ss << " " << u_value;
@@ -432,4 +432,12 @@ namespace matplot {
432432
return *this;
433433
}
434434

435+
double vectors::scale() const { return scale_; }
436+
437+
class vectors &vectors::scale(double scale) {
438+
scale_ = scale;
439+
touch();
440+
return *this;
441+
}
442+
435443
} // namespace matplot

source/matplot/axes_objects/vectors.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ namespace matplot {
115115
bool normalize() const;
116116
class vectors &normalize(bool normalize);
117117

118+
double scale() const;
119+
class vectors &scale(double scale);
120+
118121
public /* getters and setters bypassing the line_spec */:
119122
float line_width() const;
120123
class vectors &line_width(float line_width);
@@ -214,6 +217,9 @@ namespace matplot {
214217
std::vector<float> marker_sizes_{};
215218
std::vector<double> marker_colors_{};
216219

220+
/// Scale of vectors
221+
double scale_{1};
222+
217223
/// Draw line as impulse
218224
bool impulse_{false};
219225

source/matplot/core/axes_type.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4631,14 +4631,14 @@ namespace matplot {
46314631
w, [&](double w) { return (w / w_max) * scale * z_max; })
46324632
: w;
46334633
std::vector<double> m;
4634-
m.reserve(u.size());
46354634
for (int i = 0; i < u.size(); i++) {
4636-
double mag = sqrt(u[i] * u[i] + v[i] * v[i] + w[i] * w[i]);
4635+
double mag = sqrt((u[i] * u[i]) + (v[i] * v[i]) + (w[i] * w[i]));
46374636
m.emplace_back(mag);
46384637
}
46394638

46404639
vectors_handle l = std::make_shared<class vectors>(
46414640
this, x, y, z, u_scaled, v_scaled, w_scaled, m, line_spec);
4641+
l->scale(scale);
46424642
this->emplace_object(l);
46434643

46444644
return l;

0 commit comments

Comments
 (0)