1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
use crate::plot::colormap::ColorMap;
/// Where `(x, y)` sits relative to the rendered arrow.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum QuiverPivot {
/// `(x, y)` is the back of the arrow; it points *away* from that point. **(default)**
#[default]
Tail,
/// `(x, y)` is the center of the shaft; arrow extends half in each direction.
/// Reads naturally as "what the field is doing *at* this point."
Middle,
/// `(x, y)` is the arrow's tip; the shaft comes *into* the point.
Tip,
}
/// A single arrow in a quiver plot.
///
/// `(x, y)` is the arrow's tail in data coordinates; `(u, v)` is the vector
/// displacement (also in data coordinates). The rendered arrow length is
/// `(u, v)` multiplied by the plot-level [`QuiverPlot::scale`] and mapped
/// through the axis transform.
#[derive(Debug, Clone)]
pub struct QuiverArrow {
pub x: f64,
pub y: f64,
pub u: f64,
pub v: f64,
/// Per-arrow color override. `None` uses the plot-level color or colormap.
pub color: Option<String>,
}
impl QuiverArrow {
pub fn magnitude(&self) -> f64 {
(self.u * self.u + self.v * self.v).sqrt()
}
}
/// Builder for a quiver (2-D vector field) plot.
///
/// Each arrow has a tail at `(x, y)` and points in direction `(u, v)`. The
/// arrow is drawn as a line segment (the shaft) with a filled triangle
/// (the head) at the tip.
///
/// # Scaling
///
/// - [`QuiverPlot::scale`] is a multiplier applied to the `(u, v)` displacement
/// before mapping to pixel space. Default `1.0` draws each arrow at its
/// natural data-coordinate length.
/// - [`QuiverPlot::auto_scale`] computes `scale` so that the longest arrow
/// spans roughly `fraction` of the shorter axis of the data bounding box.
/// Useful when `(u, v)` have very different magnitudes from `(x, y)`.
///
/// # Coloring
///
/// Three modes, in order of priority:
/// 1. Per-arrow color via [`QuiverArrow::color`].
/// 2. Magnitude-driven colormap via [`QuiverPlot::with_color_map`]. The plot
/// reports its magnitude range so a colorbar is rendered automatically.
/// 3. Single color via [`QuiverPlot::with_color`]. Default `"steelblue"`.
///
/// # Example
///
/// ```rust,no_run
/// use kuva::plot::QuiverPlot;
/// use kuva::backend::svg::SvgBackend;
/// use kuva::render::render::render_multiple;
/// use kuva::render::layout::Layout;
/// use kuva::render::plots::Plot;
///
/// let mut arrows = Vec::new();
/// for i in 0..10 {
/// for j in 0..10 {
/// let x = i as f64;
/// let y = j as f64;
/// arrows.push((x, y, (y - 5.0) * 0.2, -(x - 5.0) * 0.2));
/// }
/// }
///
/// let plot = QuiverPlot::new()
/// .with_arrows(arrows)
/// .with_color("steelblue");
///
/// let plots = vec![Plot::Quiver(plot)];
/// let layout = Layout::auto_from_plots(&plots);
/// let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
/// ```
#[derive(Debug, Clone)]
pub struct QuiverPlot {
pub arrows: Vec<QuiverArrow>,
/// Default arrow color (used when no colormap and no per-arrow color).
/// Default `"steelblue"`.
pub color: String,
/// Multiplier applied to `(u, v)` before axis mapping.
///
/// - `Some(s)` — use `s` directly (set via [`QuiverPlot::with_scale`]).
/// - `None` — auto-compute so the longest arrow is roughly one grid
/// cell long (`≈ span / √n`). This prevents arrows from overlapping
/// each other in dense fields.
pub scale: Option<f64>,
/// Fraction of the nearest-neighbor distance used for the longest
/// arrow when `scale` is `None`. Default `0.9`.
///
/// Values near `1.0` pack arrows tip-to-tail; smaller values leave
/// more breathing room. Values above `1.0` allow overlap.
pub auto_scale_fraction: f64,
/// Shaft stroke width in pixels. Default `1.2`.
pub shaft_width: f64,
/// Explicit head length in pixels. `None` → proportional to shaft length
/// (see [`QuiverPlot::head_ratio`]). Set via [`QuiverPlot::with_head`].
pub head_length: Option<f64>,
/// Explicit head half-width in pixels. `None` → `head_aspect * head_length`.
pub head_width: Option<f64>,
/// Fraction of shaft length used for the arrow head when `head_length`
/// is `None`. Default `0.28`.
pub head_ratio: f64,
/// Head half-width as a fraction of head length when `head_width` is
/// `None`. Default `0.45`.
pub head_aspect: f64,
/// Minimum head length in pixels (prevents tiny arrows from losing their head).
/// Only applies when `head_length` is `None`. Default `4.0`.
pub head_min_px: f64,
/// Maximum head length in pixels (prevents long arrows from growing gigantic heads).
/// Only applies when `head_length` is `None`. Default `14.0`.
pub head_max_px: f64,
/// Optional colormap applied to arrow magnitude. When set, overrides
/// [`QuiverPlot::color`] for arrows without a per-arrow override.
pub color_map: Option<ColorMap>,
/// Optional explicit magnitude range `(min, max)` for colormap normalization.
/// When `None`, derived from the data.
pub color_range: Option<(f64, f64)>,
/// Label shown next to the colorbar (when a colormap is active).
pub color_legend_label: Option<String>,
pub legend_label: Option<String>,
/// When `true`, axis bounds come from arrow *tails* only — arrows may
/// extend past the plot box. Default `false` (bounds include arrow tips,
/// so nothing clips). Tight bounds produce a denser-looking field that
/// better fills the plot area.
pub tight_bounds: bool,
/// Clip arrows to the plot rectangle when `Some(b)`. When `None`, clipping
/// is auto-derived from `tight_bounds` (clipping implied when tight bounds
/// are on, since overflowing arrows would hit the axis labels). Set
/// explicitly via [`QuiverPlot::with_clip_to_plot_area`] or `with_no_clip`.
pub clip_to_plot_area: Option<bool>,
/// Where `(x, y)` sits relative to the rendered arrow. Default `Tail`.
pub pivot: QuiverPivot,
}
impl Default for QuiverPlot {
fn default() -> Self {
Self::new()
}
}
impl QuiverPlot {
/// Sample a vector-field closure on a regular `nx × ny` grid.
///
/// `x_range` and `y_range` are `(min, max, n)` tuples where `n` is the
/// number of samples along that axis (endpoints inclusive, matching
/// `numpy.linspace`). The closure receives `(x, y)` and returns `(u, v)`.
///
/// # Example
///
/// ```rust
/// use kuva::plot::QuiverPlot;
/// let plot = QuiverPlot::from_function(
/// (-2.0, 2.0, 10),
/// (-2.0, 2.0, 10),
/// |x, y| (-y, x), // rotational field
/// );
/// assert_eq!(plot.arrows.len(), 100);
/// ```
pub fn from_function<F>(
x_range: (f64, f64, usize),
y_range: (f64, f64, usize),
mut f: F,
) -> Self
where
F: FnMut(f64, f64) -> (f64, f64),
{
let (x_min, x_max, nx) = x_range;
let (y_min, y_max, ny) = y_range;
let mut plot = Self::new();
plot.arrows.reserve(nx.saturating_mul(ny));
let step = |lo: f64, hi: f64, n: usize, i: usize| -> f64 {
if n <= 1 {
(lo + hi) * 0.5
} else {
lo + (hi - lo) * (i as f64) / (n - 1) as f64
}
};
for i in 0..nx {
let x = step(x_min, x_max, nx, i);
for j in 0..ny {
let y = step(y_min, y_max, ny, j);
let (u, v) = f(x, y);
plot.arrows.push(QuiverArrow {
x,
y,
u,
v,
color: None,
});
}
}
plot
}
/// Create an empty quiver plot with default styling.
pub fn new() -> Self {
Self {
arrows: vec![],
color: "steelblue".into(),
scale: None,
auto_scale_fraction: 0.9,
shaft_width: 1.2,
head_length: None,
head_width: None,
head_ratio: 0.28,
head_aspect: 0.45,
head_min_px: 4.0,
head_max_px: 14.0,
color_map: None,
color_range: None,
color_legend_label: None,
legend_label: None,
tight_bounds: false,
clip_to_plot_area: None,
pivot: QuiverPivot::Tail,
}
}
/// Add one arrow at `(x, y)` with vector `(u, v)`. Non-finite values
/// (NaN / infinity) are silently dropped.
pub fn with_arrow(
mut self,
x: impl Into<f64>,
y: impl Into<f64>,
u: impl Into<f64>,
v: impl Into<f64>,
) -> Self {
let (x, y, u, v) = (x.into(), y.into(), u.into(), v.into());
if x.is_finite() && y.is_finite() && u.is_finite() && v.is_finite() {
self.arrows.push(QuiverArrow {
x,
y,
u,
v,
color: None,
});
}
self
}
/// Add many arrows from an iterator of `(x, y, u, v)` tuples. Non-finite
/// rows are silently dropped.
///
/// All four tuple positions must use the *same* numeric type within the
/// iterator (e.g. all `f64` or all `i32`). For mixed types, map at the
/// call site: `data.iter().map(|(x, y, u, v)| (*x as f64, *y as f64, ...))`.
pub fn with_arrows<X, Y, U, V, I>(mut self, arrows: I) -> Self
where
X: Into<f64>,
Y: Into<f64>,
U: Into<f64>,
V: Into<f64>,
I: IntoIterator<Item = (X, Y, U, V)>,
{
for (x, y, u, v) in arrows {
let (x, y, u, v) = (x.into(), y.into(), u.into(), v.into());
if x.is_finite() && y.is_finite() && u.is_finite() && v.is_finite() {
self.arrows.push(QuiverArrow {
x,
y,
u,
v,
color: None,
});
}
}
self
}
/// Add an arrow with a per-arrow color override. Non-finite values are
/// silently dropped.
pub fn with_colored_arrow(
mut self,
x: impl Into<f64>,
y: impl Into<f64>,
u: impl Into<f64>,
v: impl Into<f64>,
color: impl Into<String>,
) -> Self {
let (x, y, u, v) = (x.into(), y.into(), u.into(), v.into());
if x.is_finite() && y.is_finite() && u.is_finite() && v.is_finite() {
self.arrows.push(QuiverArrow {
x,
y,
u,
v,
color: Some(color.into()),
});
}
self
}
/// Set the default arrow color.
pub fn with_color(mut self, color: impl Into<String>) -> Self {
self.color = color.into();
self
}
/// Pin the scale multiplier to an explicit value. Overrides the default
/// auto-scaling behavior.
///
/// # Panics
/// Panics in debug builds if `s` is not finite. Pass a positive, finite
/// value; zero is allowed but produces zero-length arrows.
pub fn with_scale(mut self, s: impl Into<f64>) -> Self {
let s = s.into();
debug_assert!(
s.is_finite(),
"QuiverPlot::with_scale: scale must be finite, got {s}"
);
self.scale = Some(s);
self
}
/// Override the fraction of the nearest-neighbor distance used for the
/// longest arrow when auto-scaling (i.e. when [`QuiverPlot::with_scale`]
/// hasn't been called). Default `0.9`.
///
/// Values near `1.0` pack arrows tip-to-tail; smaller values leave more
/// breathing room. Values above `1.0` allow arrows to overlap each other.
///
/// # Panics
/// In debug builds, panics if `fraction` is not finite.
pub fn with_auto_scale(mut self, fraction: impl Into<f64>) -> Self {
let fraction = fraction.into();
debug_assert!(
fraction.is_finite(),
"QuiverPlot::with_auto_scale: fraction must be finite, got {fraction}"
);
self.scale = None;
self.auto_scale_fraction = fraction;
self
}
/// Resolve the scale multiplier, auto-computing when `scale` is `None`.
///
/// The auto-scale target is: longest arrow ≈ `fraction × nearest-neighbor
/// distance`, where the nearest-neighbor distance for `n` arrows on an
/// `R`-wide span is approximated as `R / √n`. This prevents arrows from
/// overlapping each other in dense fields.
pub(crate) fn effective_scale(&self) -> f64 {
if let Some(s) = self.scale {
return s;
}
let n = self.arrows.len();
if n < 2 {
return 1.0;
}
let mut x_min = f64::INFINITY;
let mut x_max = f64::NEG_INFINITY;
let mut y_min = f64::INFINITY;
let mut y_max = f64::NEG_INFINITY;
let mut max_mag = 0.0_f64;
for a in &self.arrows {
x_min = x_min.min(a.x);
x_max = x_max.max(a.x);
y_min = y_min.min(a.y);
y_max = y_max.max(a.y);
max_mag = max_mag.max(a.magnitude());
}
let x_span = x_max - x_min;
let y_span = y_max - y_min;
// Pick the smaller non-zero span (or the non-zero one when arrows are
// collinear on one axis).
let span = match (x_span > 0.0, y_span > 0.0) {
(true, true) => x_span.min(y_span),
(true, false) => x_span,
(false, true) => y_span,
(false, false) => 0.0,
};
if max_mag > 0.0 && span.is_finite() && span > 0.0 {
// Grid-cell heuristic: for an n-arrow field on a span R, the
// typical nearest-neighbor distance is ~R/√n. Target the longest
// arrow to be `fraction` of that.
let cell = span / (n as f64).sqrt();
self.auto_scale_fraction * cell / max_mag
} else {
1.0
}
}
/// Compute effective scale **and** the data-coordinate origin extent in one
/// pass. Returns `(scale, x_min, x_max, y_min, y_max)`. Used by `bounds()`
/// so the arrow collection is only iterated once instead of twice when
/// auto-scaling is active.
pub(crate) fn effective_scale_and_data_extent(&self) -> (f64, f64, f64, f64, f64) {
let mut x_min = f64::INFINITY;
let mut x_max = f64::NEG_INFINITY;
let mut y_min = f64::INFINITY;
let mut y_max = f64::NEG_INFINITY;
if self.arrows.is_empty() {
return (1.0, x_min, x_max, y_min, y_max);
}
let mut max_mag = 0.0_f64;
for a in &self.arrows {
x_min = x_min.min(a.x);
x_max = x_max.max(a.x);
y_min = y_min.min(a.y);
y_max = y_max.max(a.y);
max_mag = max_mag.max(a.magnitude());
}
let scale = if let Some(s) = self.scale {
s
} else {
let n = self.arrows.len();
if n < 2 {
1.0
} else {
let x_span = x_max - x_min;
let y_span = y_max - y_min;
let span = match (x_span > 0.0, y_span > 0.0) {
(true, true) => x_span.min(y_span),
(true, false) => x_span,
(false, true) => y_span,
(false, false) => 0.0,
};
if max_mag > 0.0 && span.is_finite() && span > 0.0 {
let cell = span / (n as f64).sqrt();
self.auto_scale_fraction * cell / max_mag
} else {
1.0
}
}
};
(scale, x_min, x_max, y_min, y_max)
}
/// Set the shaft stroke width in pixels. Default `1.2`.
pub fn with_shaft_width(mut self, w: impl Into<f64>) -> Self {
let w = w.into();
debug_assert!(
w.is_finite() && w >= 0.0,
"QuiverPlot::with_shaft_width: width must be finite and non-negative, got {w}"
);
self.shaft_width = w;
self
}
/// Pin arrow head dimensions to explicit pixel values. `length` is along
/// the shaft, `half_width` is perpendicular to it. Overrides the default
/// proportional sizing. Heads are still capped at the shaft length.
pub fn with_head(mut self, length: impl Into<f64>, half_width: impl Into<f64>) -> Self {
let (length, half_width) = (length.into(), half_width.into());
debug_assert!(
length.is_finite() && half_width.is_finite(),
"QuiverPlot::with_head: length and half_width must be finite"
);
self.head_length = Some(length);
self.head_width = Some(half_width);
self
}
/// Pin just the head length in pixels. `with_head_width` remains
/// independently overridable; unset dimensions fall back to the
/// proportional default.
pub fn with_head_length(mut self, length: impl Into<f64>) -> Self {
let length = length.into();
debug_assert!(
length.is_finite(),
"QuiverPlot::with_head_length: must be finite"
);
self.head_length = Some(length);
self
}
/// Pin just the head half-width in pixels.
pub fn with_head_width(mut self, half_width: impl Into<f64>) -> Self {
let half_width = half_width.into();
debug_assert!(
half_width.is_finite(),
"QuiverPlot::with_head_width: must be finite"
);
self.head_width = Some(half_width);
self
}
/// Set the head length as a fraction of the shaft length (used when no
/// explicit `with_head` is set). Default `0.28`.
pub fn with_head_ratio(mut self, ratio: impl Into<f64>) -> Self {
let ratio = ratio.into();
debug_assert!(
ratio.is_finite(),
"QuiverPlot::with_head_ratio: must be finite"
);
self.head_ratio = ratio;
self
}
/// Minimum head length in pixels when proportional sizing is in effect.
/// Prevents tiny arrows from losing their head entirely. Default `4.0`.
pub fn with_head_min_px(mut self, px: impl Into<f64>) -> Self {
let px = px.into();
debug_assert!(
px.is_finite(),
"QuiverPlot::with_head_min_px: must be finite"
);
self.head_min_px = px;
self
}
/// Maximum head length in pixels when proportional sizing is in effect.
/// Prevents long arrows from growing gigantic heads. Default `14.0`.
pub fn with_head_max_px(mut self, px: impl Into<f64>) -> Self {
let px = px.into();
debug_assert!(
px.is_finite(),
"QuiverPlot::with_head_max_px: must be finite"
);
self.head_max_px = px;
self
}
/// Resolve `(head_length, half_width)` in pixels for a shaft of length
/// `shaft_px`. Honors explicit pixel overrides, else falls back to
/// proportional sizing clamped by `head_min_px` / `head_max_px`.
pub(crate) fn resolve_head(&self, shaft_px: f64) -> (f64, f64) {
let length = match self.head_length {
Some(px) => px.min(shaft_px),
None => {
let target = shaft_px * self.head_ratio;
let lo = self.head_min_px.min(shaft_px);
let hi = self.head_max_px.min(shaft_px);
target.clamp(lo, hi)
}
};
let half_w = match self.head_width {
Some(px) => px,
None => length * self.head_aspect,
};
(length, half_w)
}
/// Color arrows by magnitude using the given colormap. Overrides
/// [`QuiverPlot::color`] for arrows without a per-arrow override.
pub fn with_color_map(mut self, cmap: ColorMap) -> Self {
self.color_map = Some(cmap);
self
}
/// Override the magnitude range used for colormap normalization.
/// Default: derived from the data.
pub fn with_color_range(mut self, lo: impl Into<f64>, hi: impl Into<f64>) -> Self {
let (lo, hi) = (lo.into(), hi.into());
debug_assert!(
lo.is_finite() && hi.is_finite(),
"QuiverPlot::with_color_range: lo and hi must be finite"
);
self.color_range = Some((lo, hi));
self
}
/// Label rendered next to the colorbar (when a colormap is active).
pub fn with_color_legend_label(mut self, label: impl Into<String>) -> Self {
self.color_legend_label = Some(label.into());
self
}
/// Shorthand for the common "color by magnitude with a labeled colorbar"
/// pattern. Equivalent to
/// `self.with_color_map(cmap).with_color_legend_label(label)`.
pub fn with_magnitude_colormap(mut self, cmap: ColorMap, label: impl Into<String>) -> Self {
self.color_map = Some(cmap);
self.color_legend_label = Some(label.into());
self
}
/// Attach a legend entry for this series.
pub fn with_legend(mut self, label: impl Into<String>) -> Self {
self.legend_label = Some(label.into());
self
}
/// Derive axis bounds from arrow tails only. Arrows may extend past the
/// plot box. Useful for dense grids where tip-inclusive bounds produce
/// too much whitespace around the field. When clipping hasn't been
/// explicitly pinned, this also turns clipping on.
pub fn with_tight_bounds(mut self) -> Self {
self.tight_bounds = true;
self
}
/// Force arrows to be clipped to the plot rectangle regardless of bounds
/// mode. Useful when you want tip-inclusive bounds but still don't want
/// the occasional outlier arrow to overflow into an axis label.
pub fn with_clip_to_plot_area(mut self) -> Self {
self.clip_to_plot_area = Some(true);
self
}
/// Disable arrow clipping even when `tight_bounds` is on. Arrows will be
/// drawn whole and may extend past the axis box into the margins.
pub fn with_no_clip(mut self) -> Self {
self.clip_to_plot_area = Some(false);
self
}
/// Resolve whether arrows should be clipped to the plot rectangle,
/// falling back to `tight_bounds` when no explicit override is set.
pub(crate) fn should_clip(&self) -> bool {
self.clip_to_plot_area.unwrap_or(self.tight_bounds)
}
/// Where `(x, y)` sits relative to the rendered arrow. Default `Tail`.
pub fn with_pivot(mut self, pivot: QuiverPivot) -> Self {
self.pivot = pivot;
self
}
/// Resolve an arrow's tail and tip endpoints in data coordinates,
/// given a precomputed scale multiplier.
pub(crate) fn endpoints_with_scale(
&self,
arrow: &QuiverArrow,
scale: f64,
) -> ((f64, f64), (f64, f64)) {
let du = arrow.u * scale;
let dv = arrow.v * scale;
let (sx, sy) = match self.pivot {
QuiverPivot::Tail => (0.0, 0.0),
QuiverPivot::Middle => (-du * 0.5, -dv * 0.5),
QuiverPivot::Tip => (-du, -dv),
};
let tail = (arrow.x + sx, arrow.y + sy);
let tip = (tail.0 + du, tail.1 + dv);
(tail, tip)
}
/// Min and max arrow magnitudes in the current data.
pub(crate) fn magnitude_extent(&self) -> (f64, f64) {
let mut lo = f64::INFINITY;
let mut hi = f64::NEG_INFINITY;
for a in &self.arrows {
let m = a.magnitude();
lo = lo.min(m);
hi = hi.max(m);
}
if !lo.is_finite() {
return (0.0, 0.0);
}
(lo, hi)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pivot_middle_vs_tail_shifts_endpoints() {
let tail = QuiverPlot::new()
.with_arrow(0.0, 0.0, 2.0, 0.0)
.with_scale(1.0)
.with_pivot(QuiverPivot::Tail);
let mid = QuiverPlot::new()
.with_arrow(0.0, 0.0, 2.0, 0.0)
.with_scale(1.0)
.with_pivot(QuiverPivot::Middle);
let (tail_t, tip_t) = tail.endpoints_with_scale(&tail.arrows[0], 1.0);
let (tail_m, tip_m) = mid.endpoints_with_scale(&mid.arrows[0], 1.0);
assert_eq!(tail_t, (0.0, 0.0));
assert_eq!(tip_t, (2.0, 0.0));
assert_eq!(tail_m, (-1.0, 0.0));
assert_eq!(tip_m, (1.0, 0.0));
}
#[test]
fn pivot_tip_places_tip_at_data_point() {
let q = QuiverPlot::new()
.with_arrow(5.0, 5.0, 1.0, 1.0)
.with_scale(1.0)
.with_pivot(QuiverPivot::Tip);
let (_tail, tip) = q.endpoints_with_scale(&q.arrows[0], 1.0);
assert_eq!(tip, (5.0, 5.0));
}
#[test]
fn auto_scale_shrinks_huge_vectors() {
let q = QuiverPlot::new()
.with_arrow(0.0, 0.0, 10.0, 0.0)
.with_arrow(1.0, 0.0, 10.0, 0.0);
let s = q.effective_scale();
assert!(
s > 0.0 && s < 0.1,
"auto-scale should shrink huge vectors; got {s}"
);
}
#[test]
fn auto_scale_matches_grid_cell_formula() {
// Pin the exact formula: fraction * span / (√n * max_mag).
// 2 arrows, span 1.0 (x: 0..1, y=0 everywhere — falls back to x_span),
// max_mag 10, fraction 0.9 → 0.9 * 1.0 / (√2 * 10) ≈ 0.06364.
let q = QuiverPlot::new()
.with_arrow(0.0, 0.0, 10.0, 0.0)
.with_arrow(1.0, 0.0, 10.0, 0.0);
let expected = 0.9 * 1.0 / ((2.0_f64).sqrt() * 10.0);
let actual = q.effective_scale();
assert!(
(actual - expected).abs() < 1e-9,
"auto-scale formula drift: expected {expected}, got {actual}"
);
}
#[test]
fn auto_scale_honors_custom_fraction() {
// Changing auto_scale_fraction should linearly scale the result.
let a = QuiverPlot::new()
.with_arrow(0.0, 0.0, 10.0, 0.0)
.with_arrow(1.0, 0.0, 10.0, 0.0);
let b = a.clone().with_auto_scale(0.45);
// 0.45 is half of the 0.9 default → scale should be half.
assert!(
(b.effective_scale() - a.effective_scale() * 0.5).abs() < 1e-9,
"with_auto_scale(0.45) should halve the 0.9-default scale"
);
}
#[test]
fn with_scale_pins_exact_value() {
let q = QuiverPlot::new()
.with_arrow(0.0, 0.0, 10.0, 0.0)
.with_arrow(1.0, 0.0, 10.0, 0.0)
.with_scale(0.5);
assert_eq!(q.effective_scale(), 0.5);
}
#[test]
fn effective_scale_fallback_when_empty() {
assert_eq!(QuiverPlot::new().effective_scale(), 1.0);
}
#[test]
fn effective_scale_fallback_when_all_magnitudes_zero() {
let q = QuiverPlot::new()
.with_arrow(0.0, 0.0, 0.0, 0.0)
.with_arrow(1.0, 1.0, 0.0, 0.0);
assert_eq!(q.effective_scale(), 1.0);
}
}