2020-03-07 23:32:57 +01:00
|
|
|
/*
|
|
|
|
* fractal
|
|
|
|
* Written in 2020 by Lucas
|
|
|
|
* CC0 1.0 Universal/Public domain - No rights reserved
|
|
|
|
*
|
|
|
|
* To the extent possible under law, the author(s) have dedicated all
|
|
|
|
* copyright and related and neighboring rights to this software to the
|
|
|
|
* public domain worldwide. This software is distributed without any
|
|
|
|
* warranty. You should have received a copy of the CC0 Public Domain
|
|
|
|
* Dedication along with this software. If not, see
|
|
|
|
* <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
*/
|
2020-03-08 03:03:58 +01:00
|
|
|
/* gepopt visibility for GLIBC */
|
|
|
|
#if !defined(__OpenBSD__)
|
|
|
|
#define _DEFAULT_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2020-03-07 23:32:57 +01:00
|
|
|
#include <complex.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "criticals.h"
|
|
|
|
#include "palette.h"
|
|
|
|
#include "pnmout.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include "fun.h"
|
|
|
|
|
2020-03-09 01:03:26 +01:00
|
|
|
#define RITNUM(c, i) (c[i].it - c[i].rit)
|
|
|
|
|
2020-03-16 02:56:29 +01:00
|
|
|
#define OPTSTRING "a:B:b:c:D:d:e:h:Ll:N:n:w:x:y:z:"
|
2020-03-09 01:28:41 +01:00
|
|
|
#define RADIAN 3.14159265358979323846L
|
2020-03-08 23:33:18 +01:00
|
|
|
|
2020-03-07 23:32:57 +01:00
|
|
|
struct color contour_color = { 0 };
|
|
|
|
static long double bailout = 1e100,
|
|
|
|
epsilon = 1e-8,
|
|
|
|
sec_w = 4.0,
|
|
|
|
contour = 0.0005L;
|
2020-03-08 23:33:18 +01:00
|
|
|
static long niters = 250, newton_iters = 100;
|
2020-03-07 23:32:57 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
print_critical_points(void)
|
|
|
|
{
|
|
|
|
long double complex z;
|
|
|
|
size_t i, n;
|
|
|
|
|
2020-03-09 01:32:30 +01:00
|
|
|
n = criticals_size();
|
2020-03-07 23:32:57 +01:00
|
|
|
fprintf(stderr, "Critical points:\n");
|
|
|
|
for (i = 0; i < n; i++) {
|
2020-03-09 01:32:30 +01:00
|
|
|
z = criticals_get(i);
|
2020-03-07 23:32:57 +01:00
|
|
|
fprintf(stderr, "%4zu. (%.20Lf, %.20Lf)\n", i, creall(z),
|
|
|
|
cimagl(z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tends_to_critical(long double complex z, long double *t)
|
|
|
|
{
|
|
|
|
long double d;
|
|
|
|
size_t i, n;
|
|
|
|
|
2020-03-09 01:32:30 +01:00
|
|
|
n = criticals_size();
|
2020-03-07 23:32:57 +01:00
|
|
|
for (i = 0; i < n; i++) {
|
2020-03-09 01:32:30 +01:00
|
|
|
d = cabsl(z - criticals_get(i));
|
2020-03-07 23:32:57 +01:00
|
|
|
if (d < epsilon) {
|
|
|
|
*t = d;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
iterate(long double complex z, long double complex c, long double *rit)
|
|
|
|
{
|
|
|
|
long double complex dz = 1.0L;
|
|
|
|
long double t;
|
2020-03-08 22:34:51 +01:00
|
|
|
long i;
|
2020-03-07 23:32:57 +01:00
|
|
|
|
|
|
|
for (i = 0; i < niters && cabsl(z) < bailout; i++) {
|
|
|
|
dz = df(z) * dz;
|
|
|
|
z = f(z) + c;
|
|
|
|
|
|
|
|
if (tends_to_critical(z, &t)) {
|
|
|
|
*rit = logl(logl(t) / logl(epsilon)) / logl(2.0L);
|
|
|
|
return fabsl(logl(t)) * t < sec_w * contour * cabsl(dz)
|
|
|
|
? -1 : i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t = cabsl(z);
|
|
|
|
if (t >= bailout) {
|
|
|
|
*rit = logl(logl(t) / logl(bailout)) / logl(2.0L);
|
|
|
|
return fabsl(logl(t)) * t < sec_w * contour * cabsl(dz)
|
|
|
|
? -1 : i;
|
|
|
|
}
|
|
|
|
|
2020-03-08 22:37:10 +01:00
|
|
|
*rit = 0;
|
2020-03-07 23:32:57 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2020-03-16 02:56:29 +01:00
|
|
|
fprintf(stderr, "Usage: %s julia [-L] [-a center_x] [-B bailout]\n"
|
|
|
|
" [-b center_y] [-c contour] [-D displacement] [-d density]\n"
|
|
|
|
" [-e epsilon] [-h height] [-l light_intensity]\n"
|
|
|
|
" [-N newton_iters] [-n iters] [-w width] [-x julia_x]\n"
|
|
|
|
" [-y julia_y] [-z zoom]\n",
|
2020-03-07 23:32:57 +01:00
|
|
|
xgetprogname());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
julia_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct color *palette;
|
|
|
|
struct {
|
|
|
|
long long it;
|
|
|
|
long double rit;
|
|
|
|
} *rescache;
|
|
|
|
struct color color;
|
|
|
|
long double complex crit, julia, z;
|
|
|
|
long double julia_x = 0.0,
|
|
|
|
julia_y = 0.0,
|
|
|
|
cen_x = 0.0,
|
|
|
|
cen_y = 0.0,
|
|
|
|
density = 28.3L,
|
|
|
|
light_intensity = 84.3L,
|
|
|
|
displacement = 0,
|
|
|
|
zoom = 1.0,
|
2020-03-09 01:03:26 +01:00
|
|
|
light, light_x, light_y, light_z, rit, rn, sec_h, zx, zy;
|
2020-03-07 23:32:57 +01:00
|
|
|
long long it, idx, ritnum;
|
2020-03-09 01:28:41 +01:00
|
|
|
int palette_size = 1000,
|
2020-03-07 23:32:57 +01:00
|
|
|
width = 640,
|
|
|
|
height = 400,
|
2020-03-09 00:12:59 +01:00
|
|
|
rescache_width, rescache_height, i, j;
|
2020-03-07 23:32:57 +01:00
|
|
|
int ch, do_light = 0;
|
|
|
|
|
2020-03-08 23:33:18 +01:00
|
|
|
while ((ch = getopt(argc, argv, OPTSTRING)) != -1)
|
2020-03-07 23:32:57 +01:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
2020-03-08 22:24:10 +01:00
|
|
|
cen_x = parse_double(optarg);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
2020-03-16 02:56:29 +01:00
|
|
|
case 'B':
|
|
|
|
bailout = parse_double(optarg);
|
|
|
|
break;
|
2020-03-07 23:32:57 +01:00
|
|
|
case 'b':
|
2020-03-08 22:24:10 +01:00
|
|
|
cen_y = parse_double(optarg);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
|
|
|
case 'c':
|
2020-03-08 22:24:10 +01:00
|
|
|
contour = parse_double(optarg);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
2020-03-08 03:15:16 +01:00
|
|
|
case 'D':
|
2020-03-08 22:24:10 +01:00
|
|
|
displacement = parse_double(optarg);
|
2020-03-08 03:15:16 +01:00
|
|
|
break;
|
2020-03-07 23:32:57 +01:00
|
|
|
case 'd':
|
2020-03-08 22:24:10 +01:00
|
|
|
density = parse_double(optarg);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
2020-03-16 02:56:29 +01:00
|
|
|
case 'e':
|
|
|
|
epsilon = parse_double(optarg);
|
|
|
|
break;
|
2020-03-07 23:32:57 +01:00
|
|
|
case 'h':
|
2020-03-09 01:28:41 +01:00
|
|
|
height = parse_integer(optarg, 1, INT_MAX);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
do_light = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
2020-03-08 22:24:10 +01:00
|
|
|
light_intensity = parse_double(optarg);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
2020-03-08 23:33:18 +01:00
|
|
|
case 'N':
|
|
|
|
newton_iters = parse_integer(optarg, 1, LONG_MAX);
|
|
|
|
break;
|
2020-03-07 23:32:57 +01:00
|
|
|
case 'n':
|
2020-03-08 22:34:51 +01:00
|
|
|
niters = parse_integer(optarg, 1, LONG_MAX);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
|
|
|
case 'w':
|
2020-03-09 01:28:41 +01:00
|
|
|
width = parse_integer(optarg, 1, INT_MAX);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
|
|
|
case 'x':
|
2020-03-08 22:24:10 +01:00
|
|
|
julia_x = parse_double(optarg);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
|
|
|
case 'y':
|
2020-03-08 22:24:10 +01:00
|
|
|
julia_y = parse_double(optarg);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
|
|
|
case 'z':
|
2020-03-08 22:24:10 +01:00
|
|
|
zoom = parse_double(optarg);
|
2020-03-07 23:32:57 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
palette = palette_gen(palette_size);
|
|
|
|
|
2020-03-09 00:12:59 +01:00
|
|
|
rescache_width = width + (do_light ? 1 : 0);
|
|
|
|
rescache_height = height + (do_light ? 1 : 0);
|
|
|
|
|
|
|
|
if ((rescache = malloc(sizeof(*rescache) * rescache_width
|
|
|
|
* rescache_height)) == NULL)
|
2020-03-08 22:24:10 +01:00
|
|
|
errx(1, "out of memory");
|
2020-03-07 23:32:57 +01:00
|
|
|
|
|
|
|
if (do_light) {
|
|
|
|
long double angle_xy, angle_z;
|
2020-03-09 01:28:41 +01:00
|
|
|
angle_xy = drand48() * 2.0L * RADIAN;
|
2020-03-13 02:33:12 +01:00
|
|
|
do
|
|
|
|
angle_z = drand48();
|
|
|
|
while (angle_z == 0.0L || angle_z == 1.0L);
|
|
|
|
angle_z = angle_z * 0.5L * RADIAN;
|
2020-03-07 23:32:57 +01:00
|
|
|
light_x = cosl(angle_xy) * sinl(angle_z);
|
|
|
|
light_y = sinl(angle_xy) * sinl(angle_z);
|
|
|
|
light_z = cosl(angle_z);
|
|
|
|
}
|
|
|
|
|
|
|
|
sec_h = sec_w * height / (long double)width;
|
|
|
|
julia = julia_x + I * julia_y;
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
zy = cen_y + (0.5 - i / (long double)height) * sec_h;
|
|
|
|
for (j = 0; j < width; j++) {
|
|
|
|
zx = cen_x + (j / (long double)width - 0.5L) * sec_w;
|
|
|
|
z = zx + I * zy;
|
2020-03-09 01:32:30 +01:00
|
|
|
if (criticals_test(df, ddf, z, &crit, newton_iters,
|
2020-03-08 23:33:18 +01:00
|
|
|
bailout, epsilon))
|
2020-03-09 01:32:30 +01:00
|
|
|
criticals_add(crit);
|
2020-03-07 23:32:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print_critical_points();
|
|
|
|
|
|
|
|
sec_w /= zoom; sec_h /= zoom;
|
2020-03-09 00:12:59 +01:00
|
|
|
for (i = 0; i < rescache_height; i++) {
|
2020-03-07 23:32:57 +01:00
|
|
|
zy = cen_y + (0.5L - i / (long double)height) * sec_h;
|
2020-03-09 00:12:59 +01:00
|
|
|
for (j = 0; j < rescache_width; j++) {
|
2020-03-07 23:32:57 +01:00
|
|
|
zx = cen_x + (j / (long double)width - 0.5L) * sec_w;
|
|
|
|
z = zx + I * zy;
|
|
|
|
it = iterate(z, julia, &rit);
|
2020-03-09 00:12:59 +01:00
|
|
|
rescache[i * rescache_width + j].it = it;
|
|
|
|
rescache[i * rescache_width + j].rit = rit;
|
2020-03-07 23:32:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 00:12:59 +01:00
|
|
|
pnmout_header(stdout, width, height);
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
for (j = 0; j < width; j++) {
|
2020-03-09 01:03:26 +01:00
|
|
|
rn = RITNUM(rescache, i * rescache_width + j);
|
2020-03-07 23:32:57 +01:00
|
|
|
|
2020-03-09 01:03:26 +01:00
|
|
|
if (rescache[i * rescache_width + j].it < 0)
|
2020-03-07 23:32:57 +01:00
|
|
|
color = contour_color;
|
|
|
|
else {
|
2020-03-09 01:03:26 +01:00
|
|
|
ritnum = rn * density;
|
2020-03-07 23:32:57 +01:00
|
|
|
if (do_light) {
|
|
|
|
long double lxrn, lyrn, l, lx, ly, lz;
|
2020-03-09 01:03:26 +01:00
|
|
|
lxrn = RITNUM(rescache,
|
|
|
|
i * rescache_width + j + 1);
|
|
|
|
lyrn = RITNUM(rescache,
|
|
|
|
(i + 1) * rescache_width + j);
|
|
|
|
lx = -sec_h * (lxrn - rn);
|
|
|
|
ly = sec_w * (lyrn - rn);
|
2020-03-07 23:32:57 +01:00
|
|
|
lz = sec_w * sec_h;
|
|
|
|
l = sqrtl(lx * lx + ly * ly + lz * lz);
|
|
|
|
light = (light_x * lx + light_y * ly
|
|
|
|
+ light_z * lz) / l;
|
|
|
|
ritnum += light * light_intensity;
|
|
|
|
}
|
|
|
|
idx = (long long)ritnum
|
|
|
|
+ 1000000 * palette_size + displacement;
|
|
|
|
color = palette[idx % palette_size
|
|
|
|
+ (idx < 0 ? palette_size : 0)];
|
|
|
|
}
|
|
|
|
|
|
|
|
pnmout_pixel(stdout, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(rescache);
|
|
|
|
free(palette);
|
|
|
|
|
|
|
|
return !(fflush(stdout) == 0);
|
|
|
|
}
|