aboutsummaryrefslogtreecommitdiff
path: root/src/fractals/mandelbrot.c
blob: 92079d14a859f8eaa6612d7c1be6e86e6ab69653 (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   mandelbrot.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/24 11:07:41 by cacharle          #+#    #+#             */
/*   Updated: 2020/02/25 07:33:35 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "fractol.h"

#define MANDEL_MAX_ITERATION 20
#define MANDEL_ESCAPE_RADIUS_SQUARED 100

int	mandelbrot(t_state *state, t_complex z)
{
	int		n;
	double	zr;
	double	zi;
	double	zr_square;
	double	zi_square;
	
	(void)state;
	zr = z.r;
	zi = z.i;
	n = -1;
	while (++n < state->iterations)
	{
		zi_square = zi * zi;
		zr_square = zr * zr;
		if (zr_square + zi_square > MANDEL_ESCAPE_RADIUS_SQUARED)
			break;
		zi = 2.0 * zr * zi;
		zr = zr_square - zi_square;
		zi += z.i;
		zr += z.r;
	}
	return (n);
}