geodesic-wallpaper 1.6.1

Animated Windows desktop wallpaper that draws glowing geodesics flowing over curved 3D surfaces (torus, Klein bottle, pseudosphere and more), rendered with wgpu. Also renders PNGs and GIFs headless.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! 2D Conway's Game of Life and variants.
//!
//! Implements a generic B/S (birth/survival) rule system that covers Conway,
//! HighLife, Day & Night, Seeds, and Mazectric.  All randomness uses a simple
//! LCG so there are zero external dependencies beyond `std`.

// ── CellState ─────────────────────────────────────────────────────────────────

/// The state of a single cell on the grid.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CellState {
    /// An empty cell.
    Dead,
    /// A live cell.
    Alive,
}

impl CellState {
    fn is_alive(self) -> bool {
        self == CellState::Alive
    }
}

// ── RuleSet ───────────────────────────────────────────────────────────────────

/// Birth/Survival rule in the B/S notation (B3/S23 is Conway).
#[derive(Debug, Clone)]
pub struct RuleSet {
    /// Neighbour counts (0 to 8) at which a dead cell is born, e.g. `[3]` for Conway.
    pub birth: Vec<u8>,
    /// Neighbour counts (0 to 8) at which a live cell survives, e.g. `[2, 3]` for Conway.
    pub survival: Vec<u8>,
}

impl RuleSet {
    fn birth_set(counts: &[u8]) -> Vec<u8> {
        counts.to_vec()
    }

    /// Conway's Game of Life — B3/S23
    pub fn conway() -> Self {
        RuleSet {
            birth: Self::birth_set(&[3]),
            survival: Self::birth_set(&[2, 3]),
        }
    }

    /// HighLife — B36/S23
    pub fn high_life() -> Self {
        RuleSet {
            birth: Self::birth_set(&[3, 6]),
            survival: Self::birth_set(&[2, 3]),
        }
    }

    /// Day and Night — B3678/S34678
    pub fn day_and_night() -> Self {
        RuleSet {
            birth: Self::birth_set(&[3, 6, 7, 8]),
            survival: Self::birth_set(&[3, 4, 6, 7, 8]),
        }
    }

    /// Seeds — B2/S (nothing survives)
    pub fn seeds() -> Self {
        RuleSet {
            birth: Self::birth_set(&[2]),
            survival: vec![],
        }
    }

    /// Mazectric — B3/S1234
    pub fn mazectric() -> Self {
        RuleSet {
            birth: Self::birth_set(&[3]),
            survival: Self::birth_set(&[1, 2, 3, 4]),
        }
    }

    fn births(&self, n: u8) -> bool {
        self.birth.contains(&n)
    }

    fn survives(&self, n: u8) -> bool {
        self.survival.contains(&n)
    }
}

// ── LCG helper ────────────────────────────────────────────────────────────────

struct Lcg {
    state: u64,
}

impl Lcg {
    fn new(seed: u64) -> Self {
        Lcg {
            state: seed.wrapping_add(1),
        }
    }

    fn next_f64(&mut self) -> f64 {
        self.state = self
            .state
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1_442_695_040_888_963_407);
        ((self.state >> 32) as f64) / (u32::MAX as f64 + 1.0)
    }
}

// ── CaGrid ────────────────────────────────────────────────────────────────────

/// A 2D cellular automaton grid.
#[derive(Debug, Clone)]
pub struct CaGrid {
    /// Number of columns.
    pub width: usize,
    /// Number of rows.
    pub height: usize,
    /// Row-major flat storage: cells[y * width + x]
    pub cells: Vec<CellState>,
    /// Whether to wrap at edges (toroidal topology) instead of treating outside as dead.
    pub wrap: bool,
}

impl CaGrid {
    /// Create an all-dead grid.
    pub fn new(width: usize, height: usize, wrap: bool) -> Self {
        CaGrid {
            width,
            height,
            cells: vec![CellState::Dead; width * height],
            wrap,
        }
    }

    /// Fill cells randomly using an LCG.
    ///
    /// `density` is the fraction of cells that will be alive (0.0–1.0).
    pub fn randomize(&mut self, density: f64, seed: u64) {
        let density = density.clamp(0.0, 1.0);
        let mut lcg = Lcg::new(seed);
        for cell in self.cells.iter_mut() {
            *cell = if lcg.next_f64() < density {
                CellState::Alive
            } else {
                CellState::Dead
            };
        }
    }

    /// Set the cell at `(x, y)`.
    pub fn set(&mut self, x: usize, y: usize, state: CellState) {
        if x < self.width && y < self.height {
            self.cells[y * self.width + x] = state;
        }
    }

    /// Get the cell at `(x, y)`.
    ///
    /// Returns `Dead` if out-of-bounds when `wrap == false`; wraps otherwise.
    pub fn get(&self, x: usize, y: usize) -> CellState {
        if self.width == 0 || self.height == 0 {
            return CellState::Dead;
        }
        if self.wrap {
            let wx = x % self.width;
            let wy = y % self.height;
            self.cells[wy * self.width + wx]
        } else {
            if x < self.width && y < self.height {
                self.cells[y * self.width + x]
            } else {
                CellState::Dead
            }
        }
    }

    /// Count alive Moore (8-connected) neighbours of cell `(x, y)`.
    pub fn neighbor_count(&self, x: usize, y: usize) -> u8 {
        let mut count = 0u8;
        let (xi, yi) = (x as isize, y as isize);
        let (w, h) = (self.width as isize, self.height as isize);

        for dy in [-1isize, 0, 1] {
            for dx in [-1isize, 0, 1] {
                if dx == 0 && dy == 0 {
                    continue;
                }
                let (nx, ny) = (xi + dx, yi + dy);
                let state = if self.wrap {
                    let wx = ((nx % w) + w) as usize % self.width;
                    let wy = ((ny % h) + h) as usize % self.height;
                    self.cells[wy * self.width + wx]
                } else if nx >= 0 && nx < w && ny >= 0 && ny < h {
                    self.cells[ny as usize * self.width + nx as usize]
                } else {
                    CellState::Dead
                };
                if state.is_alive() {
                    count += 1;
                }
            }
        }
        count
    }

    /// Compute the next generation according to `rules`.
    pub fn step(&self, rules: &RuleSet) -> CaGrid {
        let mut next = CaGrid::new(self.width, self.height, self.wrap);
        for y in 0..self.height {
            for x in 0..self.width {
                let n = self.neighbor_count(x, y);
                let alive = self.get(x, y).is_alive();
                let new_state = if alive {
                    if rules.survives(n) {
                        CellState::Alive
                    } else {
                        CellState::Dead
                    }
                } else {
                    if rules.births(n) {
                        CellState::Alive
                    } else {
                        CellState::Dead
                    }
                };
                next.set(x, y, new_state);
            }
        }
        next
    }

    /// Count the number of alive cells.
    pub fn alive_count(&self) -> usize {
        self.cells.iter().filter(|&&c| c.is_alive()).count()
    }

    /// Fraction of cells that are alive.
    pub fn density(&self) -> f64 {
        if self.cells.is_empty() {
            return 0.0;
        }
        self.alive_count() as f64 / self.cells.len() as f64
    }

    /// Run the automaton for `generations` steps, collecting all grid states.
    pub fn run(&self, rules: &RuleSet, generations: usize) -> Vec<CaGrid> {
        let mut history = Vec::with_capacity(generations + 1);
        history.push(self.clone());
        let mut current = self.clone();
        for _ in 0..generations {
            let next = current.step(rules);
            history.push(next.clone());
            current = next;
        }
        history
    }

    /// Render as a flat RGB `u8` vector (row-major, 3 bytes per pixel).
    pub fn to_rgb(&self, alive_color: (u8, u8, u8), dead_color: (u8, u8, u8)) -> Vec<u8> {
        let mut buf = Vec::with_capacity(self.width * self.height * 3);
        for &cell in &self.cells {
            let (r, g, b) = if cell.is_alive() {
                alive_color
            } else {
                dead_color
            };
            buf.push(r);
            buf.push(g);
            buf.push(b);
        }
        buf
    }

    /// Render as ASCII art: `#` for alive, `.` for dead, `\n` between rows.
    pub fn to_ascii(&self) -> String {
        let mut s = String::with_capacity(self.height * (self.width + 1));
        for y in 0..self.height {
            for x in 0..self.width {
                s.push(if self.get(x, y).is_alive() { '#' } else { '.' });
            }
            if y + 1 < self.height {
                s.push('\n');
            }
        }
        s
    }
}

// ── GliderLibrary ─────────────────────────────────────────────────────────────

/// Common patterns expressed as lists of alive-cell relative offsets `(dx, dy)`.
pub struct GliderLibrary;

impl GliderLibrary {
    /// Classic 5-cell glider (moves diagonally in Conway).
    ///
    /// ```text
    ///  .#.
    ///  ..#
    ///  ###
    /// ```
    pub fn glider() -> Vec<(usize, usize)> {
        vec![(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)]
    }

    /// Horizontal 3-cell blinker (period-2 oscillator).
    pub fn blinker() -> Vec<(usize, usize)> {
        vec![(0, 1), (1, 1), (2, 1)]
    }

    /// 2×2 stable block.
    pub fn block() -> Vec<(usize, usize)> {
        vec![(0, 0), (1, 0), (0, 1), (1, 1)]
    }

    /// Place a pattern on the grid at the given top-left position `(x, y)`.
    pub fn place_pattern(grid: &mut CaGrid, pattern: &[(usize, usize)], x: usize, y: usize) {
        for &(dx, dy) in pattern {
            let px = x + dx;
            let py = y + dy;
            if px < grid.width && py < grid.height {
                grid.set(px, py, CellState::Alive);
            }
        }
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_grid_stays_empty() {
        let grid = CaGrid::new(10, 10, false);
        let rules = RuleSet::conway();
        let next = grid.step(&rules);
        assert_eq!(next.alive_count(), 0);
    }

    #[test]
    fn block_is_stable_in_conway() {
        let mut grid = CaGrid::new(6, 6, false);
        GliderLibrary::place_pattern(&mut grid, &GliderLibrary::block(), 1, 1);
        let rules = RuleSet::conway();
        let next = grid.step(&rules);
        assert_eq!(
            grid.alive_count(),
            next.alive_count(),
            "Block should be stable"
        );
        // Same positions alive
        for y in 0..6 {
            for x in 0..6 {
                assert_eq!(grid.get(x, y), next.get(x, y), "cell ({},{}) changed", x, y);
            }
        }
    }

    #[test]
    fn blinker_oscillates() {
        let mut grid = CaGrid::new(5, 5, false);
        GliderLibrary::place_pattern(&mut grid, &GliderLibrary::blinker(), 1, 2);
        let rules = RuleSet::conway();
        let gen1 = grid.step(&rules);
        let gen2 = gen1.step(&rules);
        // After 2 steps it should return to original configuration
        assert_eq!(grid.alive_count(), gen2.alive_count());
    }

    #[test]
    fn glider_moves_after_four_steps() {
        let mut grid = CaGrid::new(20, 20, false);
        GliderLibrary::place_pattern(&mut grid, &GliderLibrary::glider(), 1, 1);
        let rules = RuleSet::conway();
        let history = grid.run(&rules, 4);
        // After 4 steps the glider has moved; alive count stays at 5
        assert_eq!(history[0].alive_count(), 5);
        assert_eq!(history[4].alive_count(), 5);
        // At least one cell differs between step 0 and step 4
        let differs =
            (0..20).any(|y| (0..20).any(|x| history[0].get(x, y) != history[4].get(x, y)));
        assert!(differs, "Glider should have moved after 4 steps");
    }

    #[test]
    fn density_in_range() {
        let mut grid = CaGrid::new(20, 20, false);
        grid.randomize(0.5, 12345);
        let d = grid.density();
        assert!(d >= 0.0 && d <= 1.0, "density out of range: {}", d);
    }

    #[test]
    fn density_zero_for_empty() {
        let grid = CaGrid::new(10, 10, false);
        assert_eq!(grid.density(), 0.0);
    }

    #[test]
    fn to_rgb_correct_length() {
        let grid = CaGrid::new(4, 4, false);
        let rgb = grid.to_rgb((255, 255, 255), (0, 0, 0));
        assert_eq!(rgb.len(), 4 * 4 * 3);
    }

    #[test]
    fn to_ascii_correct_format() {
        let mut grid = CaGrid::new(3, 2, false);
        grid.set(0, 0, CellState::Alive);
        let ascii = grid.to_ascii();
        let lines: Vec<&str> = ascii.lines().collect();
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0], "#..");
        assert_eq!(lines[1], "...");
    }

    #[test]
    fn ruleset_bs_notation_correct() {
        let conway = RuleSet::conway();
        assert!(conway.births(3));
        assert!(!conway.births(2));
        assert!(conway.survives(2));
        assert!(conway.survives(3));
        assert!(!conway.survives(4));

        let hl = RuleSet::high_life();
        assert!(hl.births(6));
        assert!(!hl.births(4));

        let seeds = RuleSet::seeds();
        assert!(seeds.births(2));
        assert!(!seeds.survives(2)); // nothing survives
    }

    #[test]
    fn wrap_get_works() {
        let mut grid = CaGrid::new(5, 5, true);
        grid.set(0, 0, CellState::Alive);
        // With wrap, accessing (5, 5) should give (0, 0)
        assert_eq!(grid.get(5, 5), CellState::Alive);
    }

    #[test]
    fn neighbor_count_corners() {
        let mut grid = CaGrid::new(3, 3, false);
        // Fill all cells alive
        for y in 0..3 {
            for x in 0..3 {
                grid.set(x, y, CellState::Alive);
            }
        }
        // Corner cell (0,0): 3 alive neighbours (1,0), (0,1), (1,1)
        assert_eq!(grid.neighbor_count(0, 0), 3);
        // Centre cell (1,1): 8 alive neighbours
        assert_eq!(grid.neighbor_count(1, 1), 8);
    }
}