Implement a basic Sierpinski Gasket IFS
This commit is contained in:
@@ -5,6 +5,7 @@ use libm::powf;
|
||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct Camera {
|
||||
dimensions: UVec2,
|
||||
transform: Affine2,
|
||||
}
|
||||
|
||||
@@ -39,7 +40,10 @@ impl Camera {
|
||||
* zoom_transform
|
||||
* ifs_center_transform;
|
||||
|
||||
Camera { transform }
|
||||
Camera {
|
||||
dimensions,
|
||||
transform,
|
||||
}
|
||||
}
|
||||
|
||||
/// Map a point from IFS coordinates to pixel coordinates.
|
||||
@@ -62,6 +66,21 @@ impl Camera {
|
||||
pub fn transform_point(&self, point: Vec2) -> IVec2 {
|
||||
self.transform.transform_point2(point).as_ivec2()
|
||||
}
|
||||
|
||||
/// Map a point from IFS coordinates to pixel coordinates (like [`transform_point`]),
|
||||
/// and check that the result is within the provided image dimensions.
|
||||
pub fn transform_point_to_image(&self, point: Vec2) -> Option<UVec2> {
|
||||
let pixel_coordinates = self.transform_point(point);
|
||||
if pixel_coordinates.x < 0
|
||||
|| pixel_coordinates.y < 0
|
||||
|| (pixel_coordinates.x as u32) >= self.dimensions.x
|
||||
|| (pixel_coordinates.y as u32) >= self.dimensions.y
|
||||
{
|
||||
None
|
||||
} else {
|
||||
Some(pixel_coordinates.as_uvec2())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user