Skip to content

Commit

Permalink
Add method to compose an arbitrary number of affine ops
Browse files Browse the repository at this point in the history
  • Loading branch information
urschrei committed Feb 8, 2024
1 parent 53ec973 commit ed057ff
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions geo/src/algorithm/affine_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,35 @@ impl<T: CoordNum> AffineTransform<T> {
],
])
}

/// Create a new affine transformation by composing an arbitrary number of `AffineTransform`s.
///
/// This is a **cumulative** operation; the new transform is *added* to the existing transform.
/// ```
/// use geo::AffineTransform;
/// let mut transform = AffineTransform::identity();
///
/// // create two transforms that cancel each other
/// let transform1 = AffineTransform::translate(1.0, 2.0);
/// let transform2 = AffineTransform::translate(-1.0, -2.0);
/// let transforms = vec![transform1, transform2];
///
/// // apply them
/// let outcome = transform.compose_many(&transforms);
/// // we should be back to square one
/// assert!(outcome.is_identity());
/// ```
#[must_use]
pub fn compose_many(&self, transforms: &[Self]) -> Self {
self.compose(
&transforms
.iter()
.fold(AffineTransform::default(), |acc: AffineTransform<T>, x| {
x.compose(&acc)
}),
)
}

/// Create the identity matrix
///
/// The matrix is:
Expand Down

0 comments on commit ed057ff

Please sign in to comment.