summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-05-17 22:55:31 +0200
committerLaurenz <laurmaedje@gmail.com>2021-05-17 22:55:31 +0200
commitc975d0d5e989cca6eff8e80ca8174f85eb4a3460 (patch)
tree7aaf68ee71d81a1d71d7dcb1f928659bc6fa975d /src/geom
parent24c4a746bc68874f2d1b0d1b726596930acaadcf (diff)
Tidy up layouting code
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/gen.rs12
-rw-r--r--src/geom/length.rs10
-rw-r--r--src/geom/path.rs38
-rw-r--r--src/geom/point.rs6
-rw-r--r--src/geom/sides.rs2
-rw-r--r--src/geom/size.rs6
-rw-r--r--src/geom/spec.rs6
7 files changed, 45 insertions, 35 deletions
diff --git a/src/geom/gen.rs b/src/geom/gen.rs
index 7e021412..91f7499c 100644
--- a/src/geom/gen.rs
+++ b/src/geom/gen.rs
@@ -3,24 +3,24 @@ use super::*;
/// A container with a main and cross component.
#[derive(Default, Copy, Clone, Eq, PartialEq)]
pub struct Gen<T> {
- /// The main component.
- pub main: T,
/// The cross component.
pub cross: T,
+ /// The main component.
+ pub main: T,
}
impl<T> Gen<T> {
/// Create a new instance from the two components.
- pub fn new(main: T, cross: T) -> Self {
- Self { main, cross }
+ pub fn new(cross: T, main: T) -> Self {
+ Self { cross, main }
}
/// Create a new instance with two equal components.
- pub fn uniform(value: T) -> Self
+ pub fn splat(value: T) -> Self
where
T: Clone,
{
- Self { main: value.clone(), cross: value }
+ Self { cross: value.clone(), main: value }
}
}
diff --git a/src/geom/length.rs b/src/geom/length.rs
index c75f79b5..8bc50e97 100644
--- a/src/geom/length.rs
+++ b/src/geom/length.rs
@@ -79,11 +79,21 @@ impl Length {
Self { raw: self.raw.min(other.raw) }
}
+ /// Set to the minimum of this and another length.
+ pub fn set_min(&mut self, other: Self) {
+ *self = self.min(other);
+ }
+
/// The maximum of this and another length.
pub fn max(self, other: Self) -> Self {
Self { raw: self.raw.max(other.raw) }
}
+ /// Set to the maximum of this and another length.
+ pub fn set_max(&mut self, other: Self) {
+ *self = self.max(other);
+ }
+
/// Whether the other length fits into this one (i.e. is smaller).
pub fn fits(self, other: Self) -> bool {
self.raw + 1e-6 >= other.raw
diff --git a/src/geom/path.rs b/src/geom/path.rs
index dcabb9cf..8878b6f1 100644
--- a/src/geom/path.rs
+++ b/src/geom/path.rs
@@ -22,6 +22,25 @@ impl Path {
Self(vec![])
}
+ /// Create a path that approximates an axis-aligned ellipse.
+ pub fn ellipse(size: Size) -> Self {
+ // https://stackoverflow.com/a/2007782
+ let rx = size.width / 2.0;
+ let ry = size.height / 2.0;
+ let m = 0.551784;
+ let mx = m * rx;
+ let my = m * ry;
+ let z = Length::ZERO;
+ let point = Point::new;
+ let mut path = Self::new();
+ path.move_to(point(-rx, z));
+ path.cubic_to(point(-rx, my), point(-mx, ry), point(z, ry));
+ path.cubic_to(point(mx, ry), point(rx, my), point(rx, z));
+ path.cubic_to(point(rx, -my), point(mx, -ry), point(z, -ry));
+ path.cubic_to(point(-mx, -ry), point(-rx, -my), point(z - rx, z));
+ path
+ }
+
/// Push a [`MoveTo`](PathElement::MoveTo) element.
pub fn move_to(&mut self, p: Point) {
self.0.push(PathElement::MoveTo(p));
@@ -42,22 +61,3 @@ impl Path {
self.0.push(PathElement::ClosePath);
}
}
-
-/// Create a path that approximates an axis-aligned ellipse.
-pub fn ellipse_path(size: Size) -> Path {
- // https://stackoverflow.com/a/2007782
- let rx = size.width / 2.0;
- let ry = size.height / 2.0;
- let m = 0.551784;
- let mx = m * rx;
- let my = m * ry;
- let z = Length::ZERO;
- let point = Point::new;
- let mut path = Path::new();
- path.move_to(point(-rx, z));
- path.cubic_to(point(-rx, my), point(-mx, ry), point(z, ry));
- path.cubic_to(point(mx, ry), point(rx, my), point(rx, z));
- path.cubic_to(point(rx, -my), point(mx, -ry), point(z, -ry));
- path.cubic_to(point(-mx, -ry), point(-rx, -my), point(z - rx, z));
- path
-}
diff --git a/src/geom/point.rs b/src/geom/point.rs
index 5ed8bf1d..479bb137 100644
--- a/src/geom/point.rs
+++ b/src/geom/point.rs
@@ -21,7 +21,7 @@ impl Point {
}
/// Create an instance with two equal components.
- pub fn uniform(value: Length) -> Self {
+ pub fn splat(value: Length) -> Self {
Self { x: value, y: value }
}
}
@@ -49,8 +49,8 @@ impl Switch for Point {
fn switch(self, main: SpecAxis) -> Self::Other {
match main {
- SpecAxis::Horizontal => Gen::new(self.x, self.y),
- SpecAxis::Vertical => Gen::new(self.y, self.x),
+ SpecAxis::Horizontal => Gen::new(self.y, self.x),
+ SpecAxis::Vertical => Gen::new(self.x, self.y),
}
}
}
diff --git a/src/geom/sides.rs b/src/geom/sides.rs
index deeced45..af728ed4 100644
--- a/src/geom/sides.rs
+++ b/src/geom/sides.rs
@@ -20,7 +20,7 @@ impl<T> Sides<T> {
}
/// Create an instance with four equal components.
- pub fn uniform(value: T) -> Self
+ pub fn splat(value: T) -> Self
where
T: Clone,
{
diff --git a/src/geom/size.rs b/src/geom/size.rs
index 0a64e6b9..d81525e6 100644
--- a/src/geom/size.rs
+++ b/src/geom/size.rs
@@ -24,7 +24,7 @@ impl Size {
}
/// Create an instance with two equal components.
- pub fn uniform(value: Length) -> Self {
+ pub fn splat(value: Length) -> Self {
Self { width: value, height: value }
}
@@ -77,8 +77,8 @@ impl Switch for Size {
fn switch(self, main: SpecAxis) -> Self::Other {
match main {
- SpecAxis::Horizontal => Gen::new(self.width, self.height),
- SpecAxis::Vertical => Gen::new(self.height, self.width),
+ SpecAxis::Horizontal => Gen::new(self.height, self.width),
+ SpecAxis::Vertical => Gen::new(self.width, self.height),
}
}
}
diff --git a/src/geom/spec.rs b/src/geom/spec.rs
index 546eac7b..e5479ba4 100644
--- a/src/geom/spec.rs
+++ b/src/geom/spec.rs
@@ -16,7 +16,7 @@ impl<T> Spec<T> {
}
/// Create a new instance with two equal components.
- pub fn uniform(value: T) -> Self
+ pub fn splat(value: T) -> Self
where
T: Clone,
{
@@ -68,8 +68,8 @@ impl<T> Switch for Spec<T> {
fn switch(self, main: SpecAxis) -> Self::Other {
match main {
- SpecAxis::Horizontal => Gen::new(self.horizontal, self.vertical),
- SpecAxis::Vertical => Gen::new(self.vertical, self.horizontal),
+ SpecAxis::Horizontal => Gen::new(self.vertical, self.horizontal),
+ SpecAxis::Vertical => Gen::new(self.horizontal, self.vertical),
}
}
}