@@ -48,12 +48,30 @@ namespace matplot {
48
48
x_data_(x_data), z_data_(z_data), u_data_(u_data), v_data_(v_data),
49
49
w_data_(w_data) {}
50
50
51
+ vectors::vectors (class axes_type *parent, const std::vector<double > &x_data,
52
+ const std::vector<double > &y_data,
53
+ const std::vector<double > &z_data,
54
+ const std::vector<double > &u_data,
55
+ const std::vector<double > &v_data,
56
+ const std::vector<double > &w_data,
57
+ const std::vector<double > &m_data,
58
+ std::string_view line_spec)
59
+ : axes_object(parent), line_spec_(this , line_spec), y_data_(y_data),
60
+ x_data_(x_data), z_data_(z_data), u_data_(u_data), v_data_(v_data),
61
+ w_data_(w_data), m_data_(m_data) {}
62
+
51
63
std::string vectors::plot_string () {
52
64
maybe_update_line_spec ();
53
65
std::stringstream ss;
54
- ss << " '-' with vectors " +
55
- line_spec_.plot_string (
56
- line_spec::style_to_plot::plot_line_only, false );
66
+ ss << " '-' with vectors" ;
67
+ if (!line_spec_.user_color ()) {
68
+ ss << " linecolor palette " ;
69
+ } else {
70
+ ss << " linecolor rgb \' " << to_string (line_spec_.color ()) << " \' " ;
71
+ }
72
+
73
+ ss << " linewidth " << line_spec_.line_width ();
74
+
57
75
if (use_y2_) {
58
76
ss << " xlim x1y2" ;
59
77
}
@@ -79,6 +97,7 @@ namespace matplot {
79
97
double u_value = u_data_.size () > i ? u_data_[i] : 0 ;
80
98
double v_value = v_data_.size () > i ? v_data_[i] : 0 ;
81
99
double w_value = w_data_.size () > i ? w_data_[i] : 0 ;
100
+ double m_value = m_data_.size () > i ? m_data_[i] : 0 ;
82
101
83
102
bool is_end_of_series =
84
103
!std::isfinite (x_value) || !std::isfinite (y_value) ||
@@ -97,6 +116,15 @@ namespace matplot {
97
116
ss << " " << 0.0 ;
98
117
}
99
118
119
+ if (normalize_ && m_value != 0 ) {
120
+ double length = sqrt (u_value * u_value + v_value * v_value +
121
+ 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;
126
+ }
127
+
100
128
ss << " " << u_value;
101
129
ss << " " << v_value;
102
130
if (is_3d ()) {
@@ -105,6 +133,8 @@ namespace matplot {
105
133
ss << " " << 0.0 ;
106
134
}
107
135
136
+ ss << " " << m_value;
137
+
108
138
ss << " \n " ;
109
139
}
110
140
// if polar, gnuplot uses the vector origin to determine
@@ -123,13 +153,8 @@ namespace matplot {
123
153
}
124
154
125
155
void vectors::maybe_update_line_spec () {
126
- if (line_spec_.has_line () && !line_spec_.user_color ()) {
127
- // if user didn't set the color, get color from xlim
128
- auto c = parent_->get_color_and_bump ();
129
- line_spec_.color (c);
130
- } else if (line_spec_.has_non_custom_marker () &&
131
- !line_spec_.user_color () &&
132
- !line_spec_.marker_user_color ()) {
156
+ if (line_spec_.has_non_custom_marker () && !line_spec_.user_color () &&
157
+ !line_spec_.marker_user_color ()) {
133
158
auto c = parent_->get_color_and_bump ();
134
159
line_spec_.marker_color (c);
135
160
}
@@ -399,4 +424,12 @@ namespace matplot {
399
424
return *this ;
400
425
}
401
426
402
- } // namespace matplot
427
+ bool vectors::norm () const { return normalize_; }
428
+
429
+ class vectors &vectors::norm (bool normalize) {
430
+ normalize_ = normalize;
431
+ touch ();
432
+ return *this ;
433
+ }
434
+
435
+ } // namespace matplot
0 commit comments