Skip to content

Instantly share code, notes, and snippets.

@terakun
Last active August 29, 2015 13:57
Show Gist options
  • Save terakun/9551583 to your computer and use it in GitHub Desktop.
Save terakun/9551583 to your computer and use it in GitHub Desktop.
#include <GL/glut.h>
#include <complex>
using namespace std;
constexpr int w_width=640,w_height=640,countnum=100;
double dx,dy;
int convergence(const complex<double>&);
void init();
void display();
void resize(int,int);
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(w_width,w_height);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(resize);
init();
glutMainLoop();
return 0;
}
int convergence(const complex<double>& c){
complex<double> z(0,0);
int count;
for(count=0;count<countnum;count++){
z=z*z+c;
if(norm(z)>4.0) break;
}
return count;
}
void init(){
glClearColor(1.0,1.0,1.0,1.0);
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.5,0,0);
glBegin(GL_POINTS);
for( double x=-1.5;x<=0.5;x+=dx){
for( double y=-1.0;y<=1.0;y+=dy){
int count=convergence(complex<double>(x,y));
float color=1.0-(float)count/countnum;
glColor3f(color,color,color);
glVertex2d(x,y);
}
}
glEnd();
glFlush();
}
void resize(int w,int h){
dx=1.0/w,dy=1.0/h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w/w_width,0,h/w_height);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment