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
//! L-system fractal tree with seasonal variations.

// ---------------------------------------------------------------------------
// Data structures
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq)]
/// Season, which sets the branch colour gradient from trunk to tips.
pub enum Season {
    /// Brown trunk to light green tips.
    Spring,
    /// Brown trunk to dark green tips.
    Summer,
    /// Brown trunk to orange and red tips.
    Autumn,
    /// Dark brown trunk to pale grey tips.
    Winter,
}

#[derive(Debug, Clone)]
/// Settings for generating a fractal tree.
pub struct TreeConfig {
    /// Branching depth (the trunk is depth 0).
    pub iterations: usize,
    /// Angle in degrees between a branch and each of its two children.
    pub angle_deg: f64,
    /// Length of each child branch relative to its parent.
    pub branch_ratio: f64,
    /// Length of the trunk in pixels.
    pub trunk_length: f64,
    /// Season that picks the colour gradient.
    pub season: Season,
    /// Seed for the random angle jitter.
    pub seed: u64,
}

impl Default for TreeConfig {
    fn default() -> Self {
        Self {
            iterations: 6,
            angle_deg: 25.0,
            branch_ratio: 0.7,
            trunk_length: 100.0,
            season: Season::Summer,
            seed: 42,
        }
    }
}

#[derive(Debug, Clone)]
/// One straight branch segment of the tree, in image coordinates.
pub struct TreeSegment {
    /// Start x in pixels.
    pub x1: f32,
    /// Start y in pixels (y grows downward).
    pub y1: f32,
    /// End x in pixels.
    pub x2: f32,
    /// End y in pixels.
    pub y2: f32,
    /// Branching depth of this segment (0 is the trunk).
    pub depth: usize,
    /// Segment colour (RGB).
    pub color: [u8; 3],
}

// ---------------------------------------------------------------------------
// Color helpers
// ---------------------------------------------------------------------------

/// Determine branch color based on depth, max_depth, and season.
pub fn branch_color(depth: usize, max_depth: usize, season: &Season) -> [u8; 3] {
    let t = if max_depth == 0 {
        1.0
    } else {
        depth as f64 / max_depth as f64
    };

    match season {
        Season::Spring => {
            // Brown trunk → green tips
            let r = (139.0 * (1.0 - t) + 34.0 * t) as u8;
            let g = (69.0 * (1.0 - t) + 180.0 * t) as u8;
            let b = (19.0 * (1.0 - t) + 34.0 * t) as u8;
            [r, g, b]
        }
        Season::Summer => {
            // Brown trunk → dark green
            let r = (101.0 * (1.0 - t) + 0.0 * t) as u8;
            let g = (67.0 * (1.0 - t) + 128.0 * t) as u8;
            let b = (33.0 * (1.0 - t) + 0.0 * t) as u8;
            [r, g, b]
        }
        Season::Autumn => {
            // Brown trunk → orange/red tips
            let r = (101.0 * (1.0 - t) + 220.0 * t) as u8;
            let g = (67.0 * (1.0 - t) + 80.0 * t) as u8;
            let b = (33.0 * (1.0 - t) + 10.0 * t) as u8;
            [r, g, b]
        }
        Season::Winter => {
            // Dark brown trunk → white/gray tips
            let r = (80.0 * (1.0 - t) + 220.0 * t) as u8;
            let g = (50.0 * (1.0 - t) + 220.0 * t) as u8;
            let b = (30.0 * (1.0 - t) + 230.0 * t) as u8;
            [r, g, b]
        }
    }
}

// ---------------------------------------------------------------------------
// Tree generation
// ---------------------------------------------------------------------------

struct LcgRng {
    state: u64,
}

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

    fn next_float(&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 >> 33) as f64 / (u32::MAX as f64 + 1.0)
    }

    /// Return jitter in degrees: ±5°
    fn jitter_deg(&mut self) -> f64 {
        (self.next_float() - 0.5) * 10.0
    }
}

fn generate_branch(
    x: f32,
    y: f32,
    angle_deg: f64,
    length: f64,
    depth: usize,
    max_depth: usize,
    config: &TreeConfig,
    rng: &mut LcgRng,
    segments: &mut Vec<TreeSegment>,
) {
    if depth > max_depth {
        return;
    }

    let angle_rad = angle_deg.to_radians();
    let x2 = x + (length * angle_rad.sin()) as f32;
    let y2 = y - (length * angle_rad.cos()) as f32; // y increases downward in image coords

    let color = branch_color(depth, max_depth, &config.season);
    segments.push(TreeSegment {
        x1: x,
        y1: y,
        x2,
        y2,
        depth,
        color,
    });

    if depth < max_depth {
        let next_length = length * config.branch_ratio;
        let jitter_l = rng.jitter_deg();
        let jitter_r = rng.jitter_deg();
        generate_branch(
            x2,
            y2,
            angle_deg + config.angle_deg + jitter_l,
            next_length,
            depth + 1,
            max_depth,
            config,
            rng,
            segments,
        );
        generate_branch(
            x2,
            y2,
            angle_deg - config.angle_deg + jitter_r,
            next_length,
            depth + 1,
            max_depth,
            config,
            rng,
            segments,
        );
    }
}

/// Generate all tree segments for the given config.
pub fn generate_tree(config: &TreeConfig) -> Vec<TreeSegment> {
    let mut segments = Vec::new();
    let mut rng = LcgRng::new(config.seed);
    // Start from bottom-center; angle 0 = straight up
    generate_branch(
        0.0,
        0.0,
        0.0,
        config.trunk_length,
        0,
        config.iterations,
        config,
        &mut rng,
        &mut segments,
    );
    segments
}

// ---------------------------------------------------------------------------
// Rendering
// ---------------------------------------------------------------------------

/// Render tree segments onto a pixel buffer using Bresenham line drawing.
pub fn render_tree(segments: &[TreeSegment], width: u32, height: u32, bg: [u8; 3]) -> Vec<u8> {
    let mut pixels = vec![0u8; (width * height * 3) as usize];
    // Fill background
    for i in 0..(width * height) as usize {
        pixels[i * 3] = bg[0];
        pixels[i * 3 + 1] = bg[1];
        pixels[i * 3 + 2] = bg[2];
    }

    // Offset so trunk starts at bottom-center
    let ox = (width / 2) as f32;
    let oy = (height - 10) as f32;

    for seg in segments {
        bresenham(
            (seg.x1 + ox) as i32,
            (seg.y1 + oy) as i32,
            (seg.x2 + ox) as i32,
            (seg.y2 + oy) as i32,
            seg.color,
            width,
            height,
            &mut pixels,
        );
    }
    pixels
}

fn bresenham(
    x0: i32,
    y0: i32,
    x1: i32,
    y1: i32,
    color: [u8; 3],
    width: u32,
    height: u32,
    pixels: &mut Vec<u8>,
) {
    let dx = (x1 - x0).abs();
    let dy = -(y1 - y0).abs();
    let sx: i32 = if x0 < x1 { 1 } else { -1 };
    let sy: i32 = if y0 < y1 { 1 } else { -1 };
    let mut err = dx + dy;
    let mut x = x0;
    let mut y = y0;

    loop {
        if x >= 0 && x < width as i32 && y >= 0 && y < height as i32 {
            let idx = ((y as u32 * width + x as u32) * 3) as usize;
            pixels[idx] = color[0];
            pixels[idx + 1] = color[1];
            pixels[idx + 2] = color[2];
        }
        if x == x1 && y == y1 {
            break;
        }
        let e2 = 2 * err;
        if e2 >= dy {
            err += dy;
            x += sx;
        }
        if e2 <= dx {
            err += dx;
            y += sy;
        }
    }
}

/// Return the endpoints of the deepest segments (leaf positions).
pub fn leaf_positions(segments: &[TreeSegment], max_depth: usize) -> Vec<(f32, f32)> {
    segments
        .iter()
        .filter(|s| s.depth == max_depth)
        .map(|s| (s.x2, s.y2))
        .collect()
}

/// Draw circles at leaf positions on the image.
pub fn add_leaves(
    image: &mut Vec<u8>,
    width: u32,
    leaves: &[(f32, f32)],
    radius: u32,
    color: [u8; 3],
) {
    let height = image.len() as u32 / (width * 3);
    let ox = (width / 2) as f32;
    let oy = (height - 10) as f32;

    for &(lx, ly) in leaves {
        let cx = (lx + ox) as i32;
        let cy = (ly + oy) as i32;
        let r = radius as i32;
        for dy in -r..=r {
            for dx in -r..=r {
                if dx * dx + dy * dy <= r * r {
                    let px = cx + dx;
                    let py = cy + dy;
                    if px >= 0 && px < width as i32 && py >= 0 && py < height as i32 {
                        let idx = ((py as u32 * width + px as u32) * 3) as usize;
                        image[idx] = color[0];
                        image[idx + 1] = color[1];
                        image[idx + 2] = color[2];
                    }
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

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

    /// Number of segments for a binary tree of given depth:
    /// depth 0 → 1 segment (trunk)
    /// Each level doubles: total = 2^(depth+1) - 1
    fn expected_segments(iterations: usize) -> usize {
        (1usize << (iterations + 1)) - 1
    }

    #[test]
    fn test_generates_correct_segment_count() {
        for iters in [1, 2, 3, 4] {
            let config = TreeConfig {
                iterations: iters,
                angle_deg: 25.0,
                branch_ratio: 0.7,
                trunk_length: 100.0,
                season: Season::Summer,
                seed: 1,
            };
            let segments = generate_tree(&config);
            let expected = expected_segments(iters);
            assert_eq!(
                segments.len(),
                expected,
                "iterations={}: got {} segments, expected {}",
                iters,
                segments.len(),
                expected
            );
        }
    }

    #[test]
    fn test_render_returns_correct_size() {
        let config = TreeConfig {
            iterations: 3,
            ..TreeConfig::default()
        };
        let segments = generate_tree(&config);
        let pixels = render_tree(&segments, 200, 200, [255, 255, 255]);
        assert_eq!(pixels.len(), 200 * 200 * 3);
    }

    #[test]
    fn test_seasons_produce_different_colors() {
        let seasons = [
            Season::Spring,
            Season::Summer,
            Season::Autumn,
            Season::Winter,
        ];
        let colors: Vec<[u8; 3]> = seasons.iter().map(|s| branch_color(5, 5, s)).collect();
        // All four tip colors should differ
        for i in 0..colors.len() {
            for j in (i + 1)..colors.len() {
                assert_ne!(
                    colors[i], colors[j],
                    "Seasons {:?} and {:?} produce the same tip color",
                    seasons[i], seasons[j]
                );
            }
        }
    }

    #[test]
    fn test_leaf_positions_are_segment_endpoints() {
        let config = TreeConfig {
            iterations: 2,
            ..TreeConfig::default()
        };
        let segments = generate_tree(&config);
        let leaves = leaf_positions(&segments, config.iterations);
        // Each leaf position should match the x2,y2 of a depth=iterations segment
        for (lx, ly) in &leaves {
            let found = segments.iter().any(|s| {
                s.depth == config.iterations && (s.x2 - lx).abs() < 1e-4 && (s.y2 - ly).abs() < 1e-4
            });
            assert!(
                found,
                "Leaf position ({}, {}) not found in segments",
                lx, ly
            );
        }
    }
}