summaryrefslogtreecommitdiffstats
path: root/examples/lib-dfscq-log/src/explode.rs
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2022-12-22 12:19:59 +0100
committerMinijackson <minijackson@riseup.net>2022-12-22 12:19:59 +0100
commit92a02c34628343153b33602eae00cef46e28d191 (patch)
tree8622ec528d24e456be22d984d93aa9bcafc97399 /examples/lib-dfscq-log/src/explode.rs
downloaddiaphragm-92a02c34628343153b33602eae00cef46e28d191.tar.gz
diaphragm-92a02c34628343153b33602eae00cef46e28d191.zip
WIP
Diffstat (limited to 'examples/lib-dfscq-log/src/explode.rs')
-rw-r--r--examples/lib-dfscq-log/src/explode.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/examples/lib-dfscq-log/src/explode.rs b/examples/lib-dfscq-log/src/explode.rs
new file mode 100644
index 0000000..aeb85bd
--- /dev/null
+++ b/examples/lib-dfscq-log/src/explode.rs
@@ -0,0 +1,86 @@
1use diaphragm_core::{
2 core_shapes::StraightPath,
3 types::{Float, Point2D, ShapeContext},
4 ComplexShape, DrawResult, Drawable, SolverContext,
5};
6
7#[derive(Clone)]
8pub struct Explode {
9 pub top_left: Point2D,
10 pub top_right: Point2D,
11 pub bottom_left: Point2D,
12 pub bottom_right: Point2D,
13
14 pub arm_length: Float,
15}
16
17impl ComplexShape for Explode {
18 fn draw(&self, context: &ShapeContext, solver: &mut dyn SolverContext) -> DrawResult {
19 let mut result = DrawResult::new();
20
21 let wanted_top = solver.float_min(&[self.top_left.y(), self.top_right.y()]);
22 let wanted_left = solver.float_min(&[self.top_left.x(), self.bottom_left.x()]);
23 let wanted_bottom = solver.float_max(&[self.bottom_left.x(), self.bottom_right.x()]);
24 let wanted_right = solver.float_max(&[self.top_right.y(), self.bottom_right.y()]);
25
26 let bounds_top = context.bounds().top(solver);
27 let bounds_left = context.bounds().left(solver);
28 let bounds_bottom = context.bounds().bottom(solver);
29 let bounds_right = context.bounds().right(solver);
30
31 // TODO: add a facility to help this?
32 let bounds_top_constraint = solver.float_eq(bounds_top, wanted_top);
33 solver.constrain(bounds_top_constraint);
34
35 let bounds_left_constraint = solver.float_eq(bounds_left, wanted_left);
36 solver.constrain(bounds_left_constraint);
37
38 let bounds_bottom_constraint = solver.float_eq(bounds_bottom, wanted_bottom);
39 solver.constrain(bounds_bottom_constraint);
40
41 let bounds_right_constraint = solver.float_eq(bounds_right, wanted_right);
42 solver.constrain(bounds_right_constraint);
43
44 let top_left_arm_bottom = Point2D::new(
45 self.top_left.x(),
46 solver.float_add(&[self.top_left.y(), self.arm_length]),
47 );
48
49 let bottom_left_arm_top = Point2D::new(
50 self.bottom_left.x(),
51 solver.float_sub(&[self.bottom_left.y(), self.arm_length]),
52 );
53
54 result.push(Drawable::new(
55 StraightPath::new(vec![
56 self.top_left.clone(),
57 top_left_arm_bottom,
58 bottom_left_arm_top,
59 self.bottom_left.clone(),
60 ]),
61 context.clone(),
62 ));
63
64 let top_right_arm_bottom = Point2D::new(
65 self.top_right.x(),
66 solver.float_add(&[self.top_right.y(), self.arm_length]),
67 );
68
69 let bottom_right_arm_top = Point2D::new(
70 self.bottom_right.x(),
71 solver.float_sub(&[self.bottom_right.y(), self.arm_length]),
72 );
73
74 result.push(Drawable::new(
75 StraightPath::new(vec![
76 self.top_right.clone(),
77 top_right_arm_bottom,
78 bottom_right_arm_top,
79 self.bottom_right.clone(),
80 ]),
81 context.clone(),
82 ));
83
84 result
85 }
86}