Social Icons

Sunday 14 December 2014

Reverse 3 Digit number

#include<iostream>
#include<conio.h>
using namespace std;
int fun(int n)
{
int sum=0;  
if(n==0)
return 1 ;
return (n*fun(n-1));
}

int main()
{
int z,k,sum=0;
int x=234;
int t=100;
int p=1;
int no=0;
int d=3;
while(d)
{
z=x/t;
x=x%t;
no=no+z*p;
p=p*10;
t=t/10;
d=d-1;
}
cout<<no;
return 0;
}

Sunday 14 September 2014

Insertion Deletion In Linked List

#include<iostream>
#include<conio.h>
using namespace std;
struct link
{
int data;
struct link *next;

};

void display();
void initial();
void deletion();

struct link *newn,*loc,*start=NULL,*ptr;


int main()
{
int i,item;
for(i=1;i<5;i++)
    {
    cout<<"Enter element";
cin>>item;
newn =new struct link[1];
newn->data=item;
newn->next=NULL;


if(start==NULL)
    {
    start=newn;
    }
    else
    {
    ptr=start;
    while(ptr->next!=NULL)
    {
    ptr=ptr->next;
   
    }
   
    ptr->next=newn;
    }

 }
   
    display();
    //for(i=1;i<=2;i++)
    //{
   
initial();
//}
}





void display()
{
ptr=start;
while(ptr!=NULL)
{
cout<<ptr->data;
cout<<"-->";
ptr=ptr->next;
}


}



void initial()
{
int item;
    cout<<"Enter the item which u want to insert in begining";
    cin>>item;
   
    struct link *p;
    p=new struct link[1];
    p->data=item;
    p->next=NULL;
    if(start==NULL)
    {
    start=p;
   
    }
else
    {
      p->next=start;
 start=p;
    }

display();
deletion();
}


void deletion()
{
char c;
cout<<"do u want to delete from begining";
cin>>c;
if(c=='y')
{
start=start->next;
display();
}

}

Sunday 4 May 2014

Gauss Elimination in Matlab

function [x] = g1(A, b)
%work out the number of equations
N = length(b)
%Gaussian elimination
for column=1:(N-1)
%work on all the rows below the diagonal element
for row = (column+1):N
%work out the value of d
d = A(row,column)/A(column,column);
%do the row operation
A(row,:) = A(row,:)-d*A(column,:)
b(row) = b(row)-d*b(column)
end%loop through rows
end %loop through columns
for row=N:-1:1
x(row) = b(row);
for i=(row+1):N
x(row) = x(row)-A(row,i)*x(i);
end
x(row) = x(row)/A(row,row);
end
return

Monte Carlo Simulation

N=input('enter no of dats');
r=4;
c=0;
for i=1:N
    x=r*rand(1);
    y=r*rand(1);
    if((x^2+y^2)<=r^2)
        c=c+1;
       
    end;
end;
n=c;
pi=(4*n)/N

Script For inverse of a matrix in Matlab

x=input('Enter a matrix 3*3')
y=input('Enter another 3*3 matrix')
s=x+y
 det=s(1,1)*(s(2,2)*s(3,3)-s(2,3)*s(3,2))-s(1,2)*(s(2,1)*s(3,3)-s(3,1)*s(2,3))+s(1,3)*(s(2,1)*s(3,2)-s(3,1)*s(2,2))
adj=[[(s(2,2)*s(3,3)-s(3,2)*s(2,3));-(s(2,1)*s(3,3)-s(3,1)*s(2,3));(s(2,1)*s(3,2)-s(3,1)*s(2,2))] [-(s(1,2)*s(3,3)-s(3,2)*s(1,3));(s(1,1)*s(3,3)-s(3,1)*s(1,3));-(s(1,1)*s(3,2)-s(3,1)*s(1,2))] [(s(2,2)*s(1,3)-s(1,2)*s(2,3));-(s(1,1)*s(2,3)-s(2,1)*s(1,3));(s(1,1)*s(2,2)-s(2,1)*s(1,2))]]
inverse=(1./det).*adj

Saturday 3 May 2014

Symbol Table Creation


To generate tokens
%%
{letter}({letter}|{digit})*       printf(“id: %s\n”, yytext);
%%
--------------------

1.
%{
#include<string.h>
%}
%%
(int|float|double|long|short|return|include|char) { printf("\n< Keyword,%s >",yytext);func(yytext,"Keyword");}
[\"] { printf("\n< Operator,%s >",yytext);func(yytext,"Operator"); }
['"'][\\a-zA-Z0-9|' ']+['"'] { printf("\n< id,%s >",yytext); func(yytext,"String");}

\+\+ { printf("\n< %s >",yytext);func(yytext,"Operator"); }
\-\- { printf("\n< %s >",yytext); func(yytext,"Operator");}
\=\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\!\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\+\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\-\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
[/|'.']+ { printf("\n< %s >",yytext); func(yytext,"Operator");}
(printf|scanf) { printf("\n< function,%s >",yytext);func(yytext,"Function");}

[0-99]+ { printf("\n<Number,%s >",yytext);func(yytext,"Number"); }
[a-zA-Z|_][0-99|a-zA-Z|_]* { printf("\n< variable,%s>",yytext); func(yytext,"variable");}
['+'|\-|'='|':'|'%'|'&'|'('|')'|'{'|'}'|','|'#'|'!'|'@'|'$'|'*'|'<'|'>'|'?'] { printf("\n< %s >",yytext);func(yytext,"Operator");}

[';']+ { printf("\n< %s >",yytext);func(yytext,"Operator");}
\n func2();
%%
struct entry
{
char name[10];
char token[10];
}st[10];
int i=0;
func(char n[10],char t[10])
{
int j,flag=0;
for(j=0;j<i;j++)
{
if(strcmp(n,st[j].name)==0)
flag=1;
}
if(flag==0)
{
strcpy(st[i].name,n);
strcpy(st[i].token,t);
i++;
}
}
func2()
{
int k;
printf("\n===========Symbol Table==================\n");
for(k=0;k<=i;k++)
printf("%s   %s\n",st[k].name,st[k].token);
}



Friday 2 May 2014

Bouncing Sphere in Opengl

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
float x=2,y=0,d=0;
void display()
{      
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
   
   
     
      glPushMatrix();
      glTranslatef(0.0,-2.0+y,0.0);
      glRotatef(x,1.0,0.0,0.0);
      glutSolidSphere(0.5,30,40);
     
      glPopMatrix();
      glFlush();

 glutPostRedisplay();
 }
void reshape(GLint w, GLint h) {

  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
 
    glOrtho(-2.5, 2.5, -2.5/aspect, 2.5/aspect, -10.0, 10.0);
  } else {
 
    glOrtho(-2.5*aspect, 2.5*aspect, -2.5, 2.5, -10.0, 10.0);
  }

}



void timer(int v)
{
x=x+0.03;
     

 if(d==0)
  {glColor3f(1.0,0.0,0.0);
   y=y+0.03;
   if(y>4)
    d=1;
  }
   if(d==1)
  {
  glColor3f(1.0,1.0,0.0);
   y=y-0.03;
   if(y<-0.8)
    d=0;
  }


 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Cyan Shapes in Yellow Light");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);

  glutMainLoop();
}

Solar System Using Opengl IN Graphics


#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

//float x=0.0;
float a[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};//angle of Rotation
float speed[10]={0.0,0.06,0.05,0.04,0.03,0.02,0.01,0.008,0.007,0.006};//speed of Rotation
float radius[10]=   {5.0,1.5,1.2,1.0,2.0,1.3,1.0,1.0,1.0,1.0};//planet Size
float distancex[10]={0.0,7.0,10.0,13.0,17.0,22.0,26.0,29.0,32.0,35.0};//DIstance Between Planets

  GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
  GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
  GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
  GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
  GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
  GLfloat skyblue[] = { 0.2, 0.2, 1.0, 1.0 };
  GLfloat mitty[] = { 0.8, 0.8, 1.0, 1.0 };

void display()
{
       int i;
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glMatrixMode(GL_MODELVIEW);

        for(i=0;i<10;i++)
        {
            glPushMatrix();
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,white);
             glutWireTorus(0.001,distancex[i],50,50);
            if(i==4)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,red);
            else if(i==3)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,blue);
            else if(i==0)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,yellow);
            else
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,mitty);
            glRotatef(a[i],0.0,0.0,1.0);
            glTranslatef(distancex[i],0.0,0.0);
             if(i==6)
            {
                glRotatef(-45,1.0,1.0,0.0);
                glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,green);
                glutWireTorus(0.2,1.5,20,20);
            }

            glPushMatrix();
            glRotatef(a[i],0.0,1.0,0.0);
            glutSolidSphere(radius[i],30,10);
            glPopMatrix();

              glPopMatrix();
          }

      glutSwapBuffers();
}

void timer(int vaulue)
{
    int i;
    for(i=1;i<10;i++)
       {
           a[i] = a[i]+speed[i];
       }
       glutPostRedisplay();
       glutTimerFunc(1000/500,timer,1);
}

void reshape(GLint w, GLint h) {
   glViewport(0, 0, w, h);
 // glClearColor(1.0,1.0,1.0,1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
gluLookAt(5.0,-5.0,10.0,.0,0.0,0.0,0.0,0.0,1.0);
  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
    // width is smaller, so stretch out the height
    glOrtho(-40.0, 40.0, -40.0/aspect, 40.0/aspect, -40.0, 40.0);
  } else {
    // height is smaller, so stretch out the width
    glOrtho(-40.0*aspect, 40.0*aspect, -40.0, 40.0, -40.0, 40.0);
  }
}

void init() {
 GLfloat direction[] = { 5.0, 0.0, 0.0, 0.0 };
 GLfloat direction1[] = { -5.0, 0.0, 0.0, 0.0 };
 glMaterialf(GL_FRONT, GL_SHININESS, 50);
  glLightfv(GL_LIGHT0, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glLightfv(GL_LIGHT1, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT1, GL_POSITION, direction1);

  glEnable(GL_LIGHTING);                // so the renderer considers light
  glEnable(GL_LIGHT0);                  // turn LIGHT0 on
  glEnable(GL_LIGHT1);
  glEnable(GL_DEPTH_TEST);              // so the renderer considers depth
}

// The usual application statup code.
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(600, 600);
  glutCreateWindow("Solar System Simulation");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
   glutTimerFunc(1000/10,timer,1);
  init();
  glutMainLoop();
}

Bouncing Ball In Opengl

#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;
float flag=1,u=0.0,d=0.0;
static GLfloat currentAngleOfRotation = 45.0;

void reshape(GLint w, GLint h) {

  GLfloat aspect = (GLfloat)w / (GLfloat)h;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w <= h) {
    glOrtho(-50.0, 50.0, -50.0/aspect, 50.0/aspect, -1.0, 1.0);
//     f1=1;
  } else {
    glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
 //    f2=1;
  }
}

void display() {
   float angle,x,y;
   int i;
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(currentAngleOfRotation, 0.0, 0.0, 1.0);
  glScalef(10.0,10.0,10.0);
//  printf("f1=%d f2=%d",f1,f2);
 
  glBegin(GL_POINTS);
   for(i=0;i<360;i++)
   {
    angle=2*3.14*i/360;
    x=cos(angle);
    y=sin(angle);
    glVertex2f(x+u,y+u);
   }

  glEnd();
  glFlush();
  glutSwapBuffers();
}

void timer(int v)
{

 if(d==0)
  {
  u=u+0.03;
  if(u>2.9)
  d=1;
  }
   if(d==1)
  {
  u=u-0.03;
  if(u<-2.9)
  d=0;
  }
 

 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 500);
  glutCreateWindow("Spinning Square");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);
 
  glutMainLoop();
}

Moving Torus in opengl

#include<GL/glut.h>

int x=0,y=0,z=0;
float radius=7.0;
float u=0.0;
float p=2.0;
void display()
{   p=p+0.03;

glClear(GL_COLOR_BUFFER_BIT);
// glColor3f(1.0,0.0,1.0);
    glPushMatrix();
     
      glRotatef(p,1.0,0.0,0.0);
      glutWireTorus(3,5,30,40);
     
      glPopMatrix();
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);glVertex3f(0,0,0);glVertex3f(20,0,0);
glColor3f(1.0,1.0,0.0);glVertex3f(0,0,0);glVertex3f(0,20,0);
glColor3f(1.0,0.0,1.0);glVertex3f(0,0,0);glVertex3f(0,0,20);

glEnd();

   glFlush();
   glutPostRedisplay();
   //glutSwapBuffers();
}
/*

void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  GLfloat direction[] = {-1.0, -1.0, -1.0, 0.0};
  glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glutSolidSphere(1.0, 25, 25);
  glMatrixMode(GL_MODELVIEW);
//  glPushMatrix();
  glLoadIdentity();

   x = radius * cos(u);
    y = 0;
    z = radius * sin(u);
  gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  //glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

*/
void init(void)
{

 glClearColor(0.0,0.0,0.0,1.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(60.0,4/3,1,40);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(7,7,20,0,0,0,0,1,0);



 /*glLightfv(GL_LIGHT0,GL_DIFFUSE,yellow);
 glEnable(GL_LIGHT0);
 glEnable(GL_LIGHTING);
 glEnable(GL_DEPTH_TEST);*/

}



/*
void timer(int v)
{
 u=u+0.1;
 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



void reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40,((GLfloat)w/(GLfloat)(h)),1,10.0);
   
}


*/

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
 
   glutDisplayFunc(display);
  // glutReshapeFunc(reshape);
  // glutTimerFunc(100,timer,0);
    init();
    glutMainLoop();
}

Moon in Opengl


#include<GL/glut.h>

int x=0,y=0,z=0;
float radius=7.0;
float u=0.0;

void display()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// glColor3f(1.0,0.0,1.0);

glutSolidSphere(1.0,30,30);
GLfloat direction[]={1.0,1.0,1.0,0.0};
glLightfv(GL_LIGHT0,GL_POSITION,direction);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
x=radius* cos(u);
y=0;
z=radius* sin(u);

gluLookAt(x,y,z,0,0,0,0,1,0);




   glFlush();
   glutSwapBuffers();
}
/*

void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  GLfloat direction[] = {-1.0, -1.0, -1.0, 0.0};
  glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glutSolidSphere(1.0, 25, 25);
  glMatrixMode(GL_MODELVIEW);
//  glPushMatrix();
  glLoadIdentity();

   x = radius * cos(u);
    y = 0;
    z = radius * sin(u);
  gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  //glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

*/
void init(void)
{

 GLfloat yellow[]={1.0,1.0,0.0};  
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 //gluOrtho2D(0.0,640.0,0.0,480.0);
 glLightfv(GL_LIGHT0,GL_DIFFUSE,yellow);
 glEnable(GL_LIGHT0);
 glEnable(GL_LIGHTING);
 glEnable(GL_DEPTH_TEST);

}




void timer(int v)
{
 u=u+0.1;
 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



void reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40,((GLfloat)w/(GLfloat)(h)),1,10.0);
   
}




int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Going for a SMILEY :-)");
 
 glutDisplayFunc(display);
 glutReshapeFunc(reshape);
   glutTimerFunc(100,timer,0);
    init();
    glutMainLoop();
}

Sunday 23 March 2014

MOVING CIRCLE IN OPENGL


#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;
float flag=1;
static GLfloat currentAngleOfRotation = 45.0;

void reshape(GLint w, GLint h) {

  GLfloat aspect = (GLfloat)w / (GLfloat)h;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w <= h) {
    glOrtho(-50.0, 50.0, -50.0/aspect, 50.0/aspect, -1.0, 1.0);
//     f1=1;
  } else {
    glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
 //    f2=1;
  }
}

void display() {
   float angle,x,y;
   int i;
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(currentAngleOfRotation, 0.0, 0.0, 1.0);
  glScalef(10.0,10.0,10.0);
//  printf("f1=%d f2=%d",f1,f2);
    glBegin(GL_TRIANGLE_STRIP);
  glVertex2f(1.0,0.0);
  glVertex2f(2.0,0.0);
  glVertex2f(2,0.5);
  glEnd();
   glBegin(GL_TRIANGLE_STRIP);
  glVertex2f(-1.0,0.0);
  glVertex2f(-2.0,0.0);
  glVertex2f(-2,-0.5);
  glEnd();
   glBegin(GL_TRIANGLE_STRIP);
  glVertex2f(0.0,-1.0);
  glVertex2f(0.0,-2.0);
  glVertex2f(0.5,-2.0);
  glEnd();
   glBegin(GL_TRIANGLE_STRIP);
  glVertex2f(0.0,1.0);
  glVertex2f(0.0,2.0);
  glVertex2f(-0.5,2.0);
  glEnd();
  glBegin(GL_POINTS);
   for(i=0;i<360;i++)
   {
    angle=2*3.14*i/360;
    x=cos(angle);
    y=sin(angle);
    glVertex2f(x,y);
   }

  glEnd();
  glFlush();
  glutSwapBuffers();
}

void timer(int v)
 {
  if (spinning)
   {
 
 
    currentAngleOfRotation += 2.0;
      if(currentAngleOfRotation==360.0)
      {
   
  currentAngleOfRotation = -360.0;
     
 }
     glutPostRedisplay();
  }
  glutTimerFunc(1000/FPS, timer, v);
}

void mouse(int button, int state, int x, int y) {
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
    spinning = true;
  } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
    spinning = false;
  }
}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 500);
  glutCreateWindow("Spinning Square");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);
  glutMouseFunc(mouse);
  glutMainLoop();
}

CIRCLE USING OPENGL

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>
#include<math.h>
void display()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
    // glColor3f(1.0,0.0,0.0);
   
     glPointSize(6.0);
     //glPointSize(6.0);
 
   
     int i;
     double angle,x,y;
     float PI=3.14;
     glBegin(GL_LINES);  
                            for(i=0;i<=300;i++)
                            {
                              angle=2*PI*i/300;
                              x=0.5*cos(angle);
                              y=0.5*sin(angle);
                              glVertex2d(x,y);
                            }
     glEnd();
                         
     glFlush();
}


int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    glutDisplayFunc(display);
 
    glutMainLoop();
}

Saturday 22 February 2014

spining cube in opengl

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;

static GLfloat currentAngleOfRotation = 0.0;

void reshape(GLint w, GLint h) {

  GLfloat aspect = (GLfloat)w / (GLfloat)h;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w <= h) {
    glOrtho(-2.0, 2.0, -2.0/aspect, 2.0/aspect, -1.0, 1.0);
  } else {
 
    glOrtho(-2.0*aspect, 2.0*aspect, -2.0, 2.0, -1.0, 1.0);
  }
}
void display() {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(currentAngleOfRotation, 0.0, 10.0, 1.0);
 // glRectf(-25.0, -25.0, 25.0, 25.0);
//  printf("f1=%d f2=%d",f1,f2);
 // glScalef(5.0,3.0,5.0);
 glTranslatef(0.3,0.2,0.0);
  glScalef(currentAngleOfRotation/120,currentAngleOfRotation/120,2);

glBegin(GL_QUADS);
    glColor3f(1.0, 1.0, 0.0);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
// glColor3f(0.0, 1.0, 1.0);
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(0.5,-0.5,0.5);
glVertex3f(0.5,0.5,0.5);
glEnd();

// glColor3f(1.0, 1.0, 0.0);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.5,-0.5,0.5);
glVertex3f(-0.5,-0.5,0.5);
glVertex3f(-0.5,0.5,0.5);
glVertex3f(0.5,0.5,0.5);
glEnd();

// glColor3f(0.0, 1.0, 0.0);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 1.0);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,0.5);
glVertex3f(-0.5,0.5,0.5);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
// glColor3f(1.0, 1.0, 0.0);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 1.0);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(0.5,0.5,0.5);
glVertex3f(-0.5,0.5,0.5);
glVertex3f(-0.5,0.5,-0.5);
glEnd();

//glColor3f(1.0, 0.0, 1.0);

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(0.5,-0.5,0.5);
glVertex3f(-0.5,-0.5,0.5);
glEnd();
  glFlush();
  glutSwapBuffers();
}

void timer(int v) {
  if (spinning) {
    currentAngleOfRotation += 2.0;
    if (currentAngleOfRotation > 360.0) {
      currentAngleOfRotation -= 360.0;
    }
    glutPostRedisplay();
  }
  glutTimerFunc(1000/FPS, timer, v);
}
void mouse(int button, int state, int x, int y) {
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
    spinning = true;
  } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
    spinning = false;
  }
}
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 500);
  glutCreateWindow("Spinning Square");
  glutReshapeFunc(reshape);
     glEnable(GL_DEPTH_TEST);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);
  glutMouseFunc(mouse);
  glutMainLoop();
}




Thursday 13 February 2014

QUAD IN OPENGL

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_QUADS);
                       
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(200,150);
                        glColor3f(1.0,0.0,1.0);
                        glVertex2f(140,150);
                     
   //glVertex2f(140,200);
                        //glVertex2f(180,200);
                     
                     
        glColor3f(1.0,1.0,0.0);
                       
    //glVertex2f(250,140);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

QUAD STRIP IN OPENGL

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_QUAD_STRIP);
                       
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                     
                        glColor3f(1.0,0.0,1.0);
                        glVertex2f(140,150);
                         glVertex2f(200,150);
   glVertex2f(140,200);
                        glVertex2f(180,200);
                     
                     
        glColor3f(1.0,1.0,0.0);
                       
    //glVertex2f(250,140);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

TRIANGLE FAN IN OPENGL

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_TRIANGLE_FAN);
                         glVertex2f(140,150);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glColor3f(1.0,0.0,1.0);
                       
                      glVertex2f(200,120);
                       glColor3f(1.0,1.0,0.0);
                        glVertex2f(250,140);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

TRIANGLE STRIP IN OPENGL

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_TRIANGLE_STRIP);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                       glVertex2f(200,150);
                        glVertex2f(150,200);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

Drawing figure using Line Strip

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
 
   
     glPointSize(15);
     glBegin(GL_LINE_STRIP);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                       glVertex2f(200,150);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    init();
glutDisplayFunc(display);
 
    glutMainLoop();
}

DRAWING TRIANGLE USING LINE LOOP

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_LINE_LOOP);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                       // glVertex2f(200,150);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

DRAWING POINTS IN OPENGL

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
 
   
     glPointSize(15);
     glBegin(GL_POINTS);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                        glVertex2f(200,150);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    init();
glutDisplayFunc(display);
 
    glutMainLoop();
}

CREATING A TRIANGLE IN OPENGL

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_TRIANGLES);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                        glVertex2f(200,150);
     glEnd();
     glFlush();
}

void init(void)
{


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management