Skip to content

Instantly share code, notes, and snippets.

@ikeating
Last active May 17, 2020 22:10
Show Gist options
  • Select an option

  • Save ikeating/bf48aeb3aae63cf4cbcadd8e09498918 to your computer and use it in GitHub Desktop.

Select an option

Save ikeating/bf48aeb3aae63cf4cbcadd8e09498918 to your computer and use it in GitHub Desktop.
/*
Isaiah Keating
csc250DT2
mandelbrot
*/
//https://www3.nd.edu/~dthain/courses/cse30341/spring2020/project3/
#include "gfx2.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
//#include <errno.h>
#include <string.h>
#include <complex.h> //Standard Lib of Complex Numbers
#define gfx_xsize
#define gfx_ysize
/*
Compute the number of iterations at point x, y
in the complex space, up to a maximum of maxiter.
Return the number of iterations at that point.
This example computes the Mandelbrot fractal:
z = z^2 + alpha
Where z is initially zero, and alpha is the location x + iy
in the complex plane. Note that we are using the "complex"
numeric type in C, which has the special functions cabs()
and cpow() to compute the absolute values and powers of
complex values.
*/
static int compute_point( double x, double y, int max )
{
double complex z = 0;
double complex alpha = x + I*y;
int iter = 0;
while( cabs(z)<4 && iter < max ) {
z = cpow(z,2) + alpha;
iter++;
}
return iter;
}
/*
Compute an entire image, writing each point to the given bitmap.
Scale the image to the range (xmin-xmax,ymin-ymax).
*/
void compute_image( double xmin, double xmax, double ymin, double ymax, int maxiter )
{
int i,j;
int width = gfx_xsize(1000);
int height = gfx_ysize(1000);
// For every pixel i,j, in the image...
for(j=0;j<width;j++) {
for(i=0;i<height;i++) {
// Scale from pixels i,j to coordinates x,y
double x = xmin + i*(xmax-xmin)/height;
double y = ymin + j*(ymax-ymin)/width;
// Compute the iterations at x,y
int iter = compute_point(x,y,maxiter);
// Convert a iteration number to an RGB color.
// (Change this bit to get more interesting colors.)
/*int red = 0 * iter / maxiter;
int green = 0 * iter / maxiter;
int blue = 255 * iter / maxiter;
gfx_color(red,green,blue);*/
// Plot the point on the screen.
//gfx_color(i % 255, 255, 255 * (iter < maxiter));
if(iter < maxiter)
{
gfx_color(0,0,255 * iter/20);
}
else
{
gfx_color(0, 0, 0); // black
}
// I switched height and width and j and i to rotate 90 degrees
gfx_point(j,i);
}
}
}
int main( int argc, char *argv[] )
{
// The initial boundaries of the fractal image in x,y space.
double xmin=-1.5;
double xmax= 0.5;
double ymin=-1.0;
double ymax= 1.0;
// Maximum number of iterations to compute.
// Higher values take longer but have more detail.
int maxiter;
sscanf( argv[1],"%i", &maxiter);
// Open a new window.
gfx_open(1000,1000,"Mandelbrot Fractal");
// Show the configuration, just in case you want to recreate it.
printf("coordinates: %lf %lf %lf %lf\n",xmin,xmax,ymin,ymax);
// Fill it with a dark blue initially.
gfx_clear_color(0,0,255);
gfx_clear();
// Display the fractal image
compute_image(xmin,xmax,ymin,ymax,maxiter);
while(1) {
// Wait for a key or mouse click.
int c = gfx_wait();
// Quit if q is pressed.
if(c=='q') exit(0);
}
return 0;
}
/*
Scott Graham
(note: you want X to go from -2.0 to 1.0, divided by # of horizontal pixels, likewise Y from -1.0 to 1.0 divided by # of vertical pixels)
for each pixel_X:
for each pixel_Y:
calculate the actual X,Y from the pixel position
initialize Zx, Zy based on X,Y (two components of a complex variable, but each by itself is just a normal real number)
repeat "enough" times:
calculate new Zx, Zy based on current Zx Zy
after doing the above enough times look at Zx, and Zy to decide if X,Y are in the set and, if NOT in the set what color to use
(note: leave pixel black for points in the set)
use gfx_point() function to color the pixel
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment