summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-11-20 15:51:07 +0100
committerLaurenz <laurmaedje@gmail.com>2021-11-20 15:51:07 +0100
commitcef46e6c40fed0089a20e44ff2f251c06878891c (patch)
treea4f12ced1441a014f0446f5b01e3f0f87bdd21b5 /src/geom
parent70c0dd767452772d29167e39b1c4f919519422ce (diff)
Strokes
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/paint.rs17
-rw-r--r--src/geom/path.rs25
2 files changed, 35 insertions, 7 deletions
diff --git a/src/geom/paint.rs b/src/geom/paint.rs
index 74d7d147..66bfb17c 100644
--- a/src/geom/paint.rs
+++ b/src/geom/paint.rs
@@ -7,7 +7,16 @@ use super::*;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum Paint {
/// A solid color.
- Color(Color),
+ Solid(Color),
+}
+
+impl<T> From<T> for Paint
+where
+ T: Into<Color>,
+{
+ fn from(t: T) -> Self {
+ Self::Solid(t.into())
+ }
}
/// A color in a dynamic format.
@@ -25,6 +34,12 @@ impl Debug for Color {
}
}
+impl From<RgbaColor> for Color {
+ fn from(rgba: RgbaColor) -> Self {
+ Self::Rgba(rgba)
+ }
+}
+
/// An 8-bit RGBA color.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct RgbaColor {
diff --git a/src/geom/path.rs b/src/geom/path.rs
index bc0d3f2d..39e75312 100644
--- a/src/geom/path.rs
+++ b/src/geom/path.rs
@@ -20,22 +20,35 @@ impl Path {
Self(vec![])
}
+ /// Create a path that describes a rectangle.
+ pub fn rect(size: Size) -> Self {
+ let z = Length::zero();
+ let point = Point::new;
+ let mut path = Self::new();
+ path.move_to(point(z, z));
+ path.line_to(point(size.w, z));
+ path.line_to(point(size.w, size.h));
+ path.line_to(point(z, size.h));
+ path.close_path();
+ path
+ }
+
/// Create a path that approximates an axis-aligned ellipse.
pub fn ellipse(size: Size) -> Self {
// https://stackoverflow.com/a/2007782
+ let z = Length::zero();
let rx = size.w / 2.0;
let ry = size.h / 2.0;
let m = 0.551784;
let mx = m * rx;
let my = m * ry;
- let z = Length::zero();
- let point = Point::new;
+ let point = |x, y| Point::new(x + rx, y + ry);
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.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(-rx, z));
path
}