-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmandelbrot.cpp
More file actions
37 lines (29 loc) · 914 Bytes
/
Copy pathmandelbrot.cpp
File metadata and controls
37 lines (29 loc) · 914 Bytes
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
#include <cstdint>
struct Complex {
double real;
double imaginary;
Complex(double r, double i) : real(r), imaginary(i) {}
Complex add(const Complex& other) const {
return Complex(real + other.real, imaginary + other.imaginary);
}
Complex square() const {
double r = real * real - imaginary * imaginary;
double i = 2 * real * imaginary;
return Complex(r, i);
}
double abs_squared() const {
return real * real + imaginary * imaginary;
}
};
extern "C" {
int32_t mandelbrot(double xcoord, double ycoord, int32_t maxIterations) {
Complex c(xcoord, ycoord);
Complex z(0, 0);
int32_t iterations = 0;
while (z.abs_squared() < 4 && iterations < maxIterations) {
z = z.square().add(c);
iterations++;
}
return iterations;
}
}