Skip to content

Commit

Permalink
feat(path): add .scale(scale) method to Path/Paths
Browse files Browse the repository at this point in the history
Adds .scale(scale) method to Path/Paths structs
that will scale a path using its bounds center as
the origin.
  • Loading branch information
tirithen committed May 7, 2024
1 parent d87993e commit 447ed8d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ impl<P: PointScaler> Path<P> {
)
}

/// Construct a scaled clone of the path with the origin at the path center
pub fn scale(&self, scale: f64) -> Self {
let bounds = self.bounds();
let center = bounds.center();

Self::new(
self.0
.iter()
.map(|p| {
Point::<P>::new(
(center.x() - p.x()) * scale + center.x(),
(center.y() - p.y()) * scale + center.y(),
)
})
.collect(),
)
}

/// Construct a clone with each point x value flipped
pub fn flip_x(&self) -> Self {
let bounds = self.bounds();
Expand Down
5 changes: 5 additions & 0 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ impl<P: PointScaler> Paths<P> {
Self::new(self.0.iter().map(|p| p.translate(x, y)).collect())
}

/// Construct a scaled clone of the path with the origin at the path center
pub fn scale(&self, scale: f64) -> Self {
Self::new(self.0.iter().map(|p| p.scale(scale)).collect())
}

/// Construct a clone with each point x value flipped
pub fn flip_x(&self) -> Self {
Self::new(self.0.iter().map(|p| p.flip_x()).collect())
Expand Down

0 comments on commit 447ed8d

Please sign in to comment.