fractal/criticals.c

70 lines
1.4 KiB
C

/*
* 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/>.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "criticals.h"
#include "util.h"
#define MAX_CRITICALS 100
static size_t idx = 0;
static long double complex criticals[MAX_CRITICALS];
int
criticals_test(long double complex (*df)(long double complex),
long double complex (*ddf)(long double complex), long double complex z,
long double complex *crit, long niters, long double bailout,
long double epsilon)
{
long i;
for (i = 0; i < niters && cabsl(z) < bailout; i++)
z = z - df(z) / ddf(z);
if (cabsl(df(z)) < epsilon) {
*crit = z;
return 1;
}
return 0;
}
void
criticals_add(long double complex z)
{
size_t i;
if (idx >= MAX_CRITICALS)
return;
for (i = 0; i < idx; i++)
if (cabsl(z - criticals[i]) < 1.0e-8)
return;
criticals[idx++] = z;
}
size_t
criticals_size(void)
{
return idx;
}
long double complex
criticals_get(size_t i)
{
if (i >= idx)
errx(1, "get_critical: index %zu out of range", i);
return criticals[i];
}