Implement basic variation support
This commit is contained in:
@@ -3,24 +3,43 @@
|
||||
//! Transforms are the "functions" in an iterated function system. They take in a point,
|
||||
//! and generate a new point. For fractal flames, transforms are always affine,
|
||||
//! but produce more interesting images once we add variations.
|
||||
use crate::variation::Variation;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use glam::{Affine2, Vec2};
|
||||
use rand::Rng;
|
||||
|
||||
/// Affine transform for use in the [`chaos_game`](crate::chaos_game).
|
||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct Transform {
|
||||
coefficients: Affine2,
|
||||
variation_range: [u16; 2],
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
/// Create a new transform from an affine transformation matrix
|
||||
pub fn new(coefficients: Affine2) -> Self {
|
||||
Transform { coefficients }
|
||||
pub fn new(coefficients: Affine2, variation_range: [u16; 2]) -> Self {
|
||||
Transform {
|
||||
coefficients,
|
||||
variation_range,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply this transform to a point in IFS coordinates, producing a new point
|
||||
pub fn transform_point(&self, point: Vec2) -> Vec2 {
|
||||
self.coefficients.transform_point2(point)
|
||||
pub fn transform_point<R: Rng>(
|
||||
&self,
|
||||
rng: &mut R,
|
||||
variations: &[Variation],
|
||||
point: Vec2,
|
||||
) -> Vec2 {
|
||||
let point = self.coefficients.transform_point2(point);
|
||||
|
||||
let mut point_output = Vec2::ZERO;
|
||||
let variation_range = self.variation_range[0] as usize..self.variation_range[1] as usize;
|
||||
for variation in variations[variation_range].iter() {
|
||||
point_output += variation.transform_point(point, rng, &self.coefficients)
|
||||
}
|
||||
|
||||
point_output
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user