Skip to content

Commit 3375904

Browse files
committed
Add method to compose an arbitrary number of affine ops
1 parent 4cd6d9f commit 3375904

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

geo/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
* Update rstar to v0.12.0
4040
* Implement `CoordsIter` for arrays and slices. This is useful when you'd like to use traits
4141
implemented for `CoordsIter` without re-allocating (e.g., creating a `MultiPoint`).
42+
* Add `compose_many` method to `AffineOps`
43+
* <https://github.com/georust/geo/pull/1148>
4244

4345
## 0.27.0
4446

geo/src/algorithm/affine_ops.rs

+26
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,32 @@ impl<T: CoordNum> AffineTransform<T> {
169169
],
170170
])
171171
}
172+
173+
/// Create a new affine transformation by composing an arbitrary number of `AffineTransform`s.
174+
///
175+
/// This is a **cumulative** operation; the new transform is *added* to the existing transform.
176+
/// ```
177+
/// use geo::AffineTransform;
178+
/// let mut transform = AffineTransform::identity();
179+
///
180+
/// // create two transforms that cancel each other
181+
/// let transform1 = AffineTransform::translate(1.0, 2.0);
182+
/// let transform2 = AffineTransform::translate(-1.0, -2.0);
183+
/// let transforms = vec![transform1, transform2];
184+
///
185+
/// // apply them
186+
/// let outcome = transform.compose_many(&transforms);
187+
/// // we should be back to square one
188+
/// assert!(outcome.is_identity());
189+
/// ```
190+
#[must_use]
191+
pub fn compose_many(&self, transforms: &[Self]) -> Self {
192+
self.compose(&transforms.iter().fold(
193+
AffineTransform::default(),
194+
|acc: AffineTransform<T>, transform| acc.compose(transform),
195+
))
196+
}
197+
172198
/// Create the identity matrix
173199
///
174200
/// The matrix is:

0 commit comments

Comments
 (0)