summary refs log tree commit diff
path: root/bin/brot.c
blob: 6b9a968ad7b37596d98104e7b4892fd10d5a81f1 (plain) (blame)
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
/* Copyright (C) 2018  June McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <complex.h>
#include <err.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>

#include "gfx/gfx.h"

static double absSq(double complex z) {
    return creal(z) * creal(z) + cimag(z) * cimag(z);
}

static uint32_t depth = 50;

static uint32_t mandelbrot(double complex c) {
    double complex z = 0;
    for (uint32_t i = 0; i < depth; ++i) {
        if (absSq(z) > 4.0) return i;
        z = z * z + c;
    }
    return 0;
}

static double complex origin = -0.75 + 0.0 * I;
static double complex scale = 3.5 + 2.0 * I;
static double complex rotate = 1;

void draw(uint32_t *buf, size_t width, size_t height) {
    for (size_t y = 0; y < height; ++y) {
        double complex zy = ((double)y / (double)height - 0.5) * cimag(scale) * I;
        for (size_t x = 0; x < width; ++x) {
            double complex zx = ((double)x / (double)width - 0.5) * creal(scale);
            uint64_t n = mandelbrot(origin + (zx + zy) * rotate);
            uint32_t g = (double)n / (double)depth * 255.0;
            buf[y * width + x] = g << 16 | g << 8 | g;
        }
    }
}

bool input(char in) {
    const double PI = acos(-1.0);
    double complex realTrans = 0.01 * creal(scale) * rotate;
    double complex imagTrans = 0.01 * cimag(scale) * I * rotate;
    double complex rotation = cexp(0.01 * PI * I);
    switch (in) {
        case 'q': return false;
        case '.': depth++; break;
        case ',': if (depth) depth--; break;
        case '+': scale *= 0.9; break;
        case '-': scale *= 1.1; break;
        case 'l': origin += realTrans; break;
        case 'h': origin -= realTrans; break;
        case 'j': origin += imagTrans; break;
        case 'k': origin -= imagTrans; break;
        case 'u': rotate *= rotation; break;
        case 'i': rotate /= rotation; break;
    }
    return true;
}

const char *status(void) {
    static char buf[256];
    snprintf(
        buf, sizeof(buf),
        "%u %g+%gi %g+%gi %g+%gi",
        depth,
        creal(origin), cimag(origin),
        creal(scale), cimag(scale),
        creal(rotate), cimag(rotate)
    );
    return buf;
}

int init(int argc, char *argv[]) {
    (void)argc;
    (void)argv;
    return EX_OK;
}