Wednesday, October 10, 2012

Julia sets :: OPENGL Program and C program

OPENGl Program and C program for Julia sets 

//JULIA sets    Ex. No 12 (b)              

#include <GL/glut.h>
int dwell(double sx,double sy)
{
      int iteration,itermax=100;
      double dx,dy,tmp,fsq,cx=-0.5,cy=0.5;
      dx = sx; dy = sy;
      fsq=sx*sx+sy*sy;
      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)
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 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);
}

void display()
{
      double sx,sy,tmp,zoom=1.5;
      int iteration,hx,hy;
      int itermax = 100;            /* how many iterations to do  */
      int hxres = 200;        /* horizonal resolution       */
      int hyres = 200;        /* vertical resolution        */
      for (hy=1;hy<=hyres;hy++)
      {
            sy=(hy-hyres/2)/(0.5*zoom*hyres);
            for (hx=1;hx<=hxres;hx++)
            {
                  sx=1.5*(hx-hxres/2)/(0.5*zoom*hxres);
                  iteration=dwell(sx,sy);

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

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 :

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<graphics.h>
int dwell(double sx,double sy)
{
int iteration,itermax=100;
double dx,dy,tmp,fsq,cx=-0.5,cy=0.5;
dx = sx; dy = sy;
fsq=sx*sx+sy*sy;
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 sx,sy,tmp,zoom=1.5;
int iteration,hx,hy;
int itermax = 100; /* how many iterations to do */
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++)
{
sy=(hy-hyres/2)/(0.5*zoom*hyres);
for (hx=1;hx<=hxres;hx++)
{
sx=1.5*(hx-hxres/2)/(0.5*zoom*hxres);
iteration=dwell(sx,sy);

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

No comments:

Post a Comment