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
//! Incrementally computed processs for drawing onto a canvas.
use crateCanvas;
/// A process is something that is incrementally computed and drawn.
///
/// The `update` method mutates `self` and changes it slightly. Then `draw` is
/// called. Then the process repeats. This continues until `true` is returned by
/// `update`, after which `draw` is called a final time, and the loop finishes.
///
/// This is a useful framework for particle systems, recursive subdivision,
/// simulations, etc...
///
/// ## Example
///
/// This example implements a process that does stochastic rectangle packing.
///
/// ```
/// use fart::{aabb::AabbTree, prelude::*};
/// use std::cmp::{max, min};
///
/// #[derive(Default)]
/// struct RectanglePacking {
/// rects: AabbTree<i64, CanvasSpace, ()>,
/// }
///
/// impl Process for RectanglePacking {
/// fn update(&mut self, canvas: &Canvas) -> bool {
/// let mut i = 0;
///
/// let x_dist = Uniform::new(0, canvas.view().width());
/// let y_dist = Uniform::new(0, canvas.view().height());
///
/// let mut rng = fart::rng();
///
/// loop {
/// let xa = x_dist.sample(&mut rng);
/// let xb = x_dist.sample(&mut rng);
/// let xmin = min(xa, xb);
/// let xmax = max(xa, xb);
///
/// let ya = y_dist.sample(&mut rng);
/// let yb = y_dist.sample(&mut rng);
/// let ymin = min(ya, yb);
/// let ymax = max(ya, yb);
///
/// let rect = Aabb::new(point2(xmin, ymin), point2(xmax, ymax));
///
/// if self.rects.any_overlap(rect.clone()) {
/// i += 1;
/// continue;
/// }
///
/// return i > 100;
/// }
/// }
///
/// fn draw(&self, canvas: &mut Canvas, last_frame: bool) {
/// if !last_frame {
/// return;
/// }
///
/// for (rect, ()) in self.rects.iter_overlapping(canvas.view().clone()) {
/// canvas.draw(rect);
/// }
/// }
/// }
/// ```
/// Run a process to completion, drawing it to the given canvas.