@@ -48,12 +48,30 @@ namespace matplot {
4848 x_data_(x_data), z_data_(z_data), u_data_(u_data), v_data_(v_data),
4949 w_data_(w_data) {}
5050
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+
5163 std::string vectors::plot_string () {
5264 maybe_update_line_spec ();
5365 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+
5775 if (use_y2_) {
5876 ss << " xlim x1y2" ;
5977 }
@@ -79,6 +97,7 @@ namespace matplot {
7997 double u_value = u_data_.size () > i ? u_data_[i] : 0 ;
8098 double v_value = v_data_.size () > i ? v_data_[i] : 0 ;
8199 double w_value = w_data_.size () > i ? w_data_[i] : 0 ;
100+ double m_value = m_data_.size () > i ? m_data_[i] : 0 ;
82101
83102 bool is_end_of_series =
84103 !std::isfinite (x_value) || !std::isfinite (y_value) ||
@@ -97,6 +116,15 @@ namespace matplot {
97116 ss << " " << 0.0 ;
98117 }
99118
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+
100128 ss << " " << u_value;
101129 ss << " " << v_value;
102130 if (is_3d ()) {
@@ -105,6 +133,8 @@ namespace matplot {
105133 ss << " " << 0.0 ;
106134 }
107135
136+ ss << " " << m_value;
137+
108138 ss << " \n " ;
109139 }
110140 // if polar, gnuplot uses the vector origin to determine
@@ -123,13 +153,8 @@ namespace matplot {
123153 }
124154
125155 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 ()) {
133158 auto c = parent_->get_color_and_bump ();
134159 line_spec_.marker_color (c);
135160 }
@@ -399,4 +424,12 @@ namespace matplot {
399424 return *this ;
400425 }
401426
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