Wednesday, October 10, 2012

Mandelbort Set opengl and c program code

Mandelbort Set opengl and c program code 

Mandelbort Set

#include <GL/glut.h>
int dwell(double cx,double cy)
{
      int iteration,itermax=100;
      double dx,dy,tmp,fsq;
      dx = 0; dy = 0;
      fsq=cx*cx+cy*cy;
      for (iteration=1;iteration<=itermax && fsq<=4;iteration++)
      {
            tmp=dx;
            dx = dx*dx-dy*dy+cx;
            dy = 2.0*tmp*dy+cy;
            fsq=dx*dx+dy*dy;
      }
      return iteration;
}
void color(int x,int y,int f)            // inside one color and outside one color
{
      if(f==1)
      {
            glColor3f(1.0,0.0,0.0);
            glBegin(GL_POINTS);
            glVertex2i(x,y);
            glEnd();
      }
      else if(f==2)
      {
        glColor3f(0.0,1.0,0.0);
            glBegin(GL_POINTS);
            glVertex2i(x,y);
            glEnd();
      }
      glFlush();
}
void display()
{
      double dx,tmp,dy,cx,cy;
      int iteration,hx,hy;
      int itermax = 100;            /* how many iterations to do  */
      double zoom=0.7;        /* if 1, no magnification           */
      int hxres = 200;        /* horizonal resolution       */
      int hyres = 200;        /* vertical resolution        */
      for (hy=1;hy<=hyres;hy++)
      {
            cy = (((float)hy)/((float)hyres)-0.5)/zoom*3.0;
            for (hx=1;hx<=hxres;hx++)
            {
                  cx = (((float)hx)/((float)hxres)-0.5)/zoom*3.0-0.7;
                  iteration=dwell(cx,cy);

                  if (iteration<=itermax)
                        color(hx,hy,1);
                  else
                        color(hx,hy,2);
            }
      }
}
void init()
{
      //select clearing (background) color
    glClearColor(0.0, 0.0, 0.0, 0.0);

    //initialize viewing values
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 640.0, 0.0, 440.0);
}
int main(int argc, char** argv)
{
    //Initialise GLUT with command-line parameters.
    glutInit(&argc, argv);
   
    //Set Display Mode
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
   
    //Set the window size
    glutInitWindowSize(640,440);
   
    //Set the window position
    glutInitWindowPosition(100,100);
   
    //Create the window
    glutCreateWindow("A Simple OpenGL Windows Application with GLUT");
   
    //Call init (initialise GLUT
    init();
   
    //Call "display" function
    glutDisplayFunc(display);
   
    //Enter the GLUT event loop
    glutMainLoop();
      //exit(0);
    return 0;
}


output:






c program code :: Mandelbort Set


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<graphics.h>
int dwell(double cx,double cy)
{
int iteration,itermax=100;
double dx,dy,tmp,fsq;
dx = 0; dy = 0;
fsq=cx*cx+cy*cy;
for (iteration=1;iteration<=itermax && fsq<=4;iteration++)
{
tmp=dx;
dx = dx*dx-dy*dy+cx;
dy = 2.0*tmp*dy+cy;
fsq=dx*dx+dy*dy;
}
return iteration;
}

void color(int x,int y,int f)
{
putpixel(x,y,f);
}

void main()
{
double dx,tmp,dy,cx,cy;
int iteration,hx,hy;
int itermax = 100; /* how many iterations to do */
double zoom=0.7; /* if 1, no magnification */
int hxres = 200; /* horizonal resolution */
int hyres = 200; /* vertical resolution */
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"z:\\tc\\bgi");

for (hy=1;hy<=hyres;hy++)
{
cy = (((float)hy)/((float)hyres)-0.5)/zoom*3.0;
for (hx=1;hx<=hxres;hx++)
{
cx = (((float)hx)/((float)hxres)-0.5)/zoom*3.0-0.7;
iteration=dwell(cx,cy);

if (iteration<=itermax)
color(hx,hy,1);
else
color(hx,hy,2);
}
}
getch();
closegraph();
}



No comments:

Post a Comment