Implement basic variation support
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
//! This algorithm is also known as the ["chaos game"](https://en.wikipedia.org/wiki/Chaos_game),
|
||||
//! and it forms the basic system for producing images.
|
||||
use crate::transform::Transform;
|
||||
use crate::variation::Variation;
|
||||
use glam::{Vec2, vec2};
|
||||
use rand::distr::{Distribution, StandardUniform};
|
||||
use rand::{Rng, RngExt};
|
||||
@@ -37,6 +38,7 @@ pub fn step_chaos_game<R: Rng>(
|
||||
rng: &mut R,
|
||||
transforms: &[Transform],
|
||||
weights: &[f32],
|
||||
variations: &[Variation],
|
||||
) -> (Vec2, u32) {
|
||||
let mut choice_weight = rng.sample::<f32, _>(StandardUniform);
|
||||
let mut transform_index: u32 = 0;
|
||||
@@ -51,7 +53,7 @@ pub fn step_chaos_game<R: Rng>(
|
||||
}
|
||||
|
||||
(
|
||||
transforms[transform_index as usize].transform_point(point),
|
||||
transforms[transform_index as usize].transform_point(rng, variations, point),
|
||||
transform_index,
|
||||
)
|
||||
}
|
||||
@@ -65,17 +67,24 @@ pub struct ChaosGame<'a, R: Rng> {
|
||||
rng: &'a mut R,
|
||||
transforms: &'a [Transform],
|
||||
weights: &'a [f32],
|
||||
variations: &'a [Variation],
|
||||
}
|
||||
|
||||
impl<'a, R: Rng> ChaosGame<'a, R> {
|
||||
/// Create a new chaos game iterator
|
||||
pub fn new(rng: &'a mut R, transforms: &'a [Transform], weights: &'a [f32]) -> Self {
|
||||
pub fn new(
|
||||
rng: &'a mut R,
|
||||
transforms: &'a [Transform],
|
||||
weights: &'a [f32],
|
||||
variations: &'a [Variation],
|
||||
) -> Self {
|
||||
let current_point = vec2(rng.sample(BiUnit), rng.sample(BiUnit));
|
||||
ChaosGame {
|
||||
current_point,
|
||||
rng,
|
||||
transforms,
|
||||
weights,
|
||||
variations,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,8 +93,13 @@ impl<'a, R: Rng> Iterator for ChaosGame<'a, R> {
|
||||
type Item = Vec2;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let (next_point, _) =
|
||||
step_chaos_game(self.current_point, self.rng, self.transforms, self.weights);
|
||||
let (next_point, _) = step_chaos_game(
|
||||
self.current_point,
|
||||
self.rng,
|
||||
self.transforms,
|
||||
self.weights,
|
||||
self.variations,
|
||||
);
|
||||
self.current_point = next_point;
|
||||
|
||||
Some(next_point)
|
||||
|
||||
Reference in New Issue
Block a user