#include <math.h>
#include <helianthus.h>
Animation anim = 0;
unsigned char pixels[512][512][4];
double r = 0.4, i = 0.6;
double mx, my, zmx, zmy;
double z = 1/128.0;
double cx = -2;
double cy = -2;
void setColor(double x, unsigned char *pixel) {
double h0 = 240;
double h1 = 0;
double h = h0 + (h1 - h0)*x;
pixelSet(pixel, colorByHSV(h, 1, 1));
}
void julia(double r, double i) {
for(int y = 0; y < 512; ++y) {
for(int x = 0; x < 512; ++x) {
double rr = x*z + cx;
double ii = y*z + cy;
int b;
for(b = 0; b < 64; ++b) {
if (rr*rr + ii*ii > 4) break;
double x = rr*rr - ii*ii + r;
ii = 2*rr*ii + i;
rr = x;
}
setColor(b/64.0, pixels[y][x]);
}
}
}
void fractal() {
julia(r, i);
imageToExistingGLTexture(animationGetGLTexId(anim), 512, 512, pixels);
}
void init() {
anim = createAnimationFromImageEx(512, 512, pixels, FALSE, FALSE, FALSE, FALSE);
fractal();
}
void draw() {
double dt = worldGetFrameTime();
int changed = 0;
if (mouseDown("middle")) {
r += (mouseX() - mx)*z/10;
i += (mouseY() - my)*z/10;
changed = 1;
}
if (mouseDown("left") || mouseDown("right")) {
double k = pow(4, dt);
if (mouseDown("left")) k = 1/k;
cx += mouseX()*z*(1 - k);
cy += mouseY()*z*(1 - k);
z *= k;
changed = 1;
}
mx = mouseX();
my = mouseY();
if (changed) fractal();
fill(colorByName("white"));
rectTextured(anim, 0, 0, 512, 512);
}
int main() {
worldSetInit(&init);
worldSetDraw(&draw);
worldSetFrameRateEx(1, 100);
worldRun();
}