-
-
Save lutzee/2d0b5037b75763d4f6ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "../Lib110ct.h" | |
void fractalMode(Win110ct& win); | |
void fractal(SDL_Surface *surf, int width, int height, double x, double y, double w, double h, int type, int maxiter, int shader); | |
int main(int argc, char** argv){ | |
// Gets the window of size 800x600 | |
Win110ct win(800,600,16); //this was actually one of my uni assignments, had to use their lib | |
int mode = 0; | |
fractalMode(win); | |
return 0; | |
} | |
void fractalMode(Win110ct& win){ | |
//Gets the surface of the window and clear the text | |
SDL_Surface *surf = win.getGraphics(); | |
win.clear(); | |
win.setPosition(0,0); | |
//Continuously loop an wait for a new command to be pressed | |
while(true){ | |
static double width = 4; | |
static double height = 3; | |
static double x = 0; | |
static double y = 0; | |
static int iter = 8; | |
static int shader = 1; | |
static int type = 4; | |
fractal(surf, 800, 600, x, y, width, height,type, iter, shader);//Draws the fractal | |
win.render();//Renders the fractal | |
char cmd = win.getchar(); | |
win.setPosition(0,0); | |
win.setEcho(false); | |
//Exit the loops | |
if(cmd == 'x') break; | |
//Zooms in | |
else if(cmd == 'i'){ width *= 0.7; height *= 0.7;} | |
//Zooms out | |
else if(cmd == 'o'){ width /= 0.7; height /= 0.7; } | |
//Change the shader forward 1 | |
else if(cmd == 'n'){ shader++;} | |
//Changes the shader back 1 | |
else if(cmd == 'm'){shader--;} | |
//Add an iteration | |
else if(cmd == 'j'){iter++;} | |
//subtract an iteration | |
else if(cmd == 'k'){iter--;} | |
//Move the screen up | |
else if(cmd == 'w'){y -= height/5;} | |
//Move the screen down | |
else if(cmd == 's'){y += height/5;} | |
//Move the screen left | |
else if(cmd == 'a'){x -= width/5;} | |
//move the screen right | |
else if(cmd == 'd'){x += width/5;} | |
//Change the Julia values forward 1 type | |
else if(cmd == 't'){type++;} | |
//Change the julia values back 1 type | |
else if(cmd == 'y'){type--;} | |
}} | |
void fractal(SDL_Surface *surf, int width, int height, double x, double y, double w, double h, int type, int maxiter, int shader){ | |
SDL_Rect p; | |
p.x, p.y = 0;//sets the first pixel location | |
p.w, p.h = 1;//sets the size of the pixel | |
//sets the default calculation values for the Julia set, cRe if A real value, and cIm is an imaginary number | |
double cRe=-0.03515; | |
double cIm=-0.07467; | |
//switchs between the different julia set values | |
switch (type){ | |
case 1: cRe=-0.70176; cIm= -0.3842; break; | |
case 2: cRe= -0.835; cIm= -0.2321 ;break; | |
case 3: cRe= -0.4; cIm= 0.6; break; | |
case 4: cRe= 0.285; cIm= 0; break; | |
case 5: cRe= -0.8; cIm= 0.156; break; | |
case 6: cRe=0.233; cIm=0.5378; | |
default: cRe=-0.4; cIm=0.6; break; | |
} | |
//iterates through every pixel on the screen | |
for(p.y = 0; p.y < height; ++p.y) | |
for(p.x = 0; p.x < width; ++p.x){ | |
double beforeA = (double)p.x/(double)width * w - (w/2) + x; //initial calculation for the pixels | |
double beforeB = (double)p.y/(double)height * h - (h/2) + y; | |
int i = 0; //Is keeping track of iterations | |
while(beforeA*beforeA + beforeB*beforeB < 4 && i++ < maxiter){ //calculates the next value for the pixel | |
double afterA = beforeA*beforeA - beforeB*beforeB + cRe; | |
double afterB = 2 * beforeA * beforeB + cIm; | |
//stores the new value for next loop | |
beforeA = afterA; | |
beforeB = afterB; | |
} | |
unsigned char shade_colour; | |
switch(shader){//selects the shading type | |
case 1: shade_colour = (beforeB/8) * (beforeA/4) * 255; break; | |
case 2: shade_colour = 64/(beforeA*beforeA * beforeB/beforeB); break; | |
case 3: shade_colour = (beforeA + beforeB) * 16; break; | |
case 4: shade_colour = 255 - (double)i/maxiter * 255; break; | |
default: { | |
if(beforeA*beforeA + beforeB*beforeB < 4) | |
shade_colour = 255; | |
else | |
shade_colour = 0; | |
}} | |
SDL_FillRect(surf, &p, (shade_colour << 24 | shade_colour << 16 | shade_colour << 8 | shade_colour));//draws the pixel | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment