noise/noise_fns/generators/
checkerboard.rs

1use crate::{core::checkerboard::*, noise_fns::NoiseFn};
2
3/// Noise function that outputs a checkerboard pattern.
4///
5/// This noise function can take one input, size, and outputs 2<sup>size</sup>-sized
6/// blocks of alternating values. The values of these blocks alternate between
7/// -1.0 and 1.0.
8///
9/// This noise function is not very useful by itself, but it can be used for
10/// debugging purposes.
11#[derive(Clone, Copy, Debug)]
12pub struct Checkerboard {
13    // Controls the size of the block in 2^(size).
14    size: usize,
15}
16
17impl Checkerboard {
18    const DEFAULT_SIZE: usize = 0;
19
20    /// Controls the size of the block in 2^(size) units.
21    pub fn new(size: usize) -> Self {
22        Self { size: 1 << size }
23    }
24
25    pub fn set_size(self, size: usize) -> Self {
26        Self { size: 1 << size }
27    }
28
29    pub fn size(self) -> usize {
30        self.size
31    }
32}
33
34impl Default for Checkerboard {
35    fn default() -> Self {
36        Self {
37            size: 1 << Checkerboard::DEFAULT_SIZE,
38        }
39    }
40}
41
42impl NoiseFn<f64, 2> for Checkerboard {
43    fn get(&self, point: [f64; 2]) -> f64 {
44        checkerboard_2d(point.into(), self.size as f64)
45    }
46}
47
48impl NoiseFn<f64, 3> for Checkerboard {
49    fn get(&self, point: [f64; 3]) -> f64 {
50        checkerboard_3d(point.into(), self.size as f64)
51    }
52}
53
54impl NoiseFn<f64, 4> for Checkerboard {
55    fn get(&self, point: [f64; 4]) -> f64 {
56        checkerboard_4d(point.into(), self.size as f64)
57    }
58}