Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Tuesday, 19 November 2019

Point Program

#include<stdio.h>
#include<GL/glut.h>

void myInit(){
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(0.0,0.0,1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,10.0,0.0,10.0);
    glMatrixMode(GL_MODELVIEW);

}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(4.0);
    glBegin(GL_POLYGON);
    glVertex2f(3.0,6.0);
    //glColor3f(0.0,1.0,0.0);
    glVertex2f(7.0,6.0);
    //glColor3f(0.0,0.0,1.0);
    glVertex2f(5.0,8.0);
    //glColor3f(1.0,1.0,0.0);
    glEnd();
    glBegin(GL_POLYGON);
    glColor3f(1.0,0.0,0.0);
    glVertex2f(3.0,6.0);
    glVertex2f(3.0,2.0);
    glVertex2f(7.0,2.0);
    glVertex2f(7.0,6.0);
    glEnd();
    glBegin(GL_POLYGON);
    glColor3f(0.0,0.0,0.0);
    glVertex2f(3.5,5.5);
    glVertex2f(3.5,4.6);
    glVertex2f(4.5,4.6);
    glVertex2f(4.5,5.5);
    glEnd();
    glBegin(GL_POLYGON);
    glColor3f(0.0,0.0,0.0);
    glVertex2f(3.5+2.0,5.5);
    glVertex2f(3.5+2.0,4.6);
    glVertex2f(2.0+4.5,4.6);
    glVertex2f(4.5+2.0,5.5);
    glEnd();
    glBegin(GL_POLYGON);
    glColor3f(1.0,1.0,1.0);
    glVertex2f(1.5+3.0,4.0);
    glVertex2f(1.5+3.0,2.0+0.155);
    glVertex2f(1.5+4.0,2.0+0.155);
    glVertex2f(1.5+4.0,4.0);
    glEnd();
    glFlush();
    glEnd();


}
int main(int argc,char **argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("My First Example");
    glutDisplayFunc(display);
    myInit();
    glutMainLoop();
    return 0;
}

Mouse Event Program

nclude<stdio.h>
#include<math.h>
#include<GL/glut.h>
int width=400,height=400;
void myInit()
{
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,10.0,0.0,10.0);
    glMatrixMode(GL_MODELVIEW);
}
void drawSquare()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex2i(1,1);
    glVertex2i(9,1);
    glVertex2i(9,9);
    glVertex2i(1,9);
    glEnd();
    glutSwapBuffers();
    glFlush();
}
void drawTriangle()
{
    glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,1.0);
    glBegin(GL_POLYGON);
    glVertex2i(1,1);
    glVertex2i(5,9);
    glVertex2i(9,1);
    glEnd();
    glutSwapBuffers();
    glFlush();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glutSwapBuffers();
    glFlush();
}
void myMouse(int btn,int state,int x,int y)
{
     if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
    {
        drawTriangle();
    }
    if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
    {
        drawSquare();
    }
    if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
    {
        exit(0);
    }

}
void main(int argc,char **argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    //glutInitWindowSize(width,height);
  glutInitWindowSize(400,400);
   glutInitWindowPosition(100,100);
   glutCreateWindow("draw objects");
   myInit();
   glutDisplayFunc(display);
   glutMouseFunc(myMouse);
   glutMainLoop();
   return 0;
}

Simple Menu Program


#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
int width=400,height=400;
void myInit()
{
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,10.0,0.0,10.0);
    glMatrixMode(GL_MODELVIEW);
}

void square()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex2i(1,1);
    glVertex2i(9,1);
    glVertex2i(9,9);
    glVertex2i(1,9);
    glEnd();
    glutSwapBuffers();
    glFlush();
}



void triangle()
{
    glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,1.0);
    glBegin(GL_POLYGON);
    glVertex2i(1,1);
    glVertex2i(5,9);
    glVertex2i(9,1);
    glEnd();
    glutSwapBuffers();
    glFlush();
}
void circle()
{
    int r=4;
    float theta;
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(4.0);
    glColor3f(0.0,1.0,1.0);
    glBegin(GL_POINTS);
    for(theta=0;theta<=360;theta+=0.01){
    glVertex2f(r*cos(theta*3.142/180)+5,r*sin(theta*3.142/180)+5);
    }
    glEnd();
    glutSwapBuffers();
    glFlush();
}

void myDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glFlush();
}
void myMenu(int id){

    printf("MyMenu called with id: %d\n",id);
    switch(id){
        case 1: triangle(); break;
        case 2: circle(); break;
        case 3: square(); break;
        case 4: exit(0); break;
    }
}
void myKey(unsigned char key, int x,int yy){
    if(key=='q' || key == 'Q')
        exit(0);
}

int main(int argc, char **argv){
int menu_id;

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  glutInitWindowSize(400,400);
   glutInitWindowPosition(100,100);
   glutCreateWindow("Input Interaction");

   glutDisplayFunc(myDisplay);
   glutKeyboardFunc(myKey);


menu_id=glutCreateMenu(myMenu);
glutAddMenuEntry("Triangle",1);
glutAddMenuEntry("Circle",2);
glutAddMenuEntry("Square",3);
glutAddMenuEntry("exit",4);
glutAttachMenu(GLUT_RIGHT_BUTTON);

myInit();
glutMainLoop();
return 0;


}

First Program

#include<stdio.h>
#include<GL/glut.h>

void myInit(){
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,10.0,0.0,10.0);
    glMatrixMode(GL_MODELVIEW);

}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(4.0);
    glBegin(GL_POLYGON);
    glVertex2f(1.0,1.0);
    glColor3f(0.0,1.0,0.0);
    glVertex2f(6.0,1.0);
    glColor3f(0.0,0.0,1.0);
    glVertex2f(6.0,6.0);
    glColor3f(1.0,1.0,0.0);
    glVertex2f(1.0,6.0);
    glEnd();
    glFlush();

}

int main(int argc,char **argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("My First Example");
    glutDisplayFunc(display);
    myInit();
    glutMainLoop();
    return 0;
}

House Program

#include<stdio.h>
#include<GL/glut.h>
void myInit(){
  glClearColor(1.0,1.0,1.0,1.0);
  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0.0,10.0,0.0,10.0);
  glMatrixMode(GL_MODELVIEW);
  


}
void display(){
  glClear(GL_COLOR_BUFFER_BIT);
  glPointSize(15.0);
  //glLineSize(10.0);
  glBegin(GL_POLYGON);
  glColor3f(1.0,0.0,0.0);
    glVertex2f(2.0,1.0);
    glVertex2f(6.0,1.0);
    glVertex2f(6.0,6.0);
    glVertex2f(2.0,6.0);

  glEnd();
  glBegin(GL_POLYGON);
    glColor3f(0.6,0.0,1.0);
    glVertex2f(2.0,6.0);
    glVertex2f(6.0,6.0);
    glVertex2f(4.0,8.0);
  glEnd();
  glBegin(GL_POLYGON);
    glColor3f(0.0,0.0,0.0);
    glVertex2f(2.5,4.5);
    glVertex2f(3.5,4.5);
    glVertex2f(3.5,5.5);
    glVertex2f(2.5,5.5);
  glEnd();
   glBegin(GL_POLYGON);
    glColor3f(0.0,0.0,0.0);
    glVertex2f(4.5,4.5);
    glVertex2f(5.5,4.5);
    glVertex2f(5.5,5.5);
    glVertex2f(4.5,5.5);
  glEnd();
  glBegin(GL_POLYGON);
    glColor3f(1.0,1.0,1.0);
    glVertex2f(3.5,1.1);
    glVertex2f(4.5,1.1);
    glVertex2f(4.5,3.5);
    glVertex2f(3.5,3.5);
  glEnd();
  glBegin(GL_POLYGON);
    glColor3f(1.0,1.0,0.0);
    glVertex2f(4.0,1.1);
    glVertex2f(4.5,1.1);
    glVertex2f(4.5,3.5);
    glVertex2f(4.0,3.2);
  glEnd();
  glFlush();

}
int main(int argc,char **argv){
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  glutInitWindowSize(400,400);
  glutInitWindowPosition(100,100);
  glutCreateWindow("Program on Input Inraction");
  glutDisplayFunc(display);
  myInit();
  glutMainLoop();
  return 0;
}


//gcc -o foo new.c -lglut -lGLU -lGL -lm

Sub-Menu Program

#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
int width=400,height=400;
void myInit()
{
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,10.0,0.0,10.0);
    glMatrixMode(GL_MODELVIEW);
}

void square()
{
    glClear(GL_COLOR_BUFFER_BIT);
    //glColor3f(1.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex2i(1,1);
    glVertex2i(9,1);
    glVertex2i(9,9);
    glVertex2i(1,9);
    glEnd();
    glutSwapBuffers();
    glFlush();
}



void triangle()
{
    glClear(GL_COLOR_BUFFER_BIT);
  //   glColor3f(1.0,0.0,1.0);
    glBegin(GL_POLYGON);
    glVertex2i(1,1);
    glVertex2i(5,9);
    glVertex2i(9,1);
    glEnd();
    glutSwapBuffers();
    glFlush();
}
void circle()
{
    int r=4;
    float theta;
   glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(4.0);
  //  glColor3f(0.0,1.0,1.0);
    glBegin(GL_POLYGON);
    for(theta=0;theta<=360;theta+=0.01){
    glVertex2f(r*cos(theta*3.142/180)+5,r*sin(theta*3.142/180)+5);
    }
    glEnd();
    glutSwapBuffers();
    glFlush();
}

void myDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glFlush();
}
void subMenu(int id){
//glClear(GL_COLOR_BUFFER_BIT);
    printf("subMenu called with id: %d\n",id);
    switch(id){
        case 1: glColor3f(1.0,0.0,0.0); break;
        case 2: glColor3f(0.0,1.0,0.0); break;
        case 3: glColor3f(0.0,0.0,1.0); break;
        case 4: glColor3f(0.0,1.0,1.0); break;
       // case 4: exit(0); break;
    }
}

void myMenu(int id){

    printf("MyMenu called with id: %d\n",id);
    switch(id){
        case 1: triangle(); break;
        case 2: circle(); break;
        case 3: square(); break;
        case 4: exit(0); break;
    }
}
void myKey(unsigned char key, int x,int yy){
    if(key=='q' || key == 'Q')
        exit(0);
}

int main(int argc, char **argv){
int menu_id;

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  glutInitWindowSize(400,400);
   glutInitWindowPosition(100,100);
   glutCreateWindow("Input Interaction");

   glutDisplayFunc(myDisplay);
   glutKeyboardFunc(myKey);


   int sub_menu=glutCreateMenu(subMenu);
   glutAddMenuEntry("red",1);
   glutAddMenuEntry("blue",3);
   glutAddMenuEntry("green",2);
   glutAddMenuEntry("yellow",4);




menu_id=glutCreateMenu(myMenu);
glutAddMenuEntry("Triangle",1);
glutAddMenuEntry("Circle",2);
glutAddMenuEntry("Square",3);
glutAddSubMenu("Colours",sub_menu);
glutAddMenuEntry("exit",4);
glutAttachMenu(GLUT_RIGHT_BUTTON);

myInit();
glutMainLoop();
return 0;


}

Sierpinski Triangle

#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
#include <stdlib.h>
struct Point {
  float x, y;
  Point(float x = 0, float y = 0): x(x), y(y) {}
  Point midpoint(Point p) {return Point((x + p.x) / 2.0, (y + p.y) / 2.0);}
};
void display() {
  glClear(GL_COLOR_BUFFER_BIT);
  static Point vertices[] = {Point(0.0, 0.0), Point(200.0, 500.0), Point(500.0, 0.0)};
  static Point p = vertices[0];
  glBegin(GL_POINTS);
  for (int k = 0; k < 100000; k++) {
    p = p.midpoint(vertices[rand() % 3]);
    glVertex2f(p.x, p.y);
  }
  glEnd();
  glFlush();
}
void init() {
  glClearColor(0.25, 0.0, 0.2, 1.0);
  glColor3f(0.6, 1.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, 500.0, 0.0, 500.0, 0.0, 1.0);
}
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(500, 500);
  glutInitWindowPosition(40, 40);
  glutCreateWindow("Sierpinski Triangle");
  glutDisplayFunc(display);
  init();
  glutMainLoop();
}