Draw translated and rotated hierarchical models

/*
* robot.c
* This program shows how to composite modeling transformations
* to draw translated and rotated hierarchical models.
* Interaction: pressing the s and e keys (shoulder and elbow)
* alters the rotation of the robot arm.
*/

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

static int shoulder = 0, elbow = 0;

void init()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
//glShadeModel (GL_FLAT);

}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();

glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}

void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case ‘s’:
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
case ‘S’:
shoulder = (shoulder – 5) % 360;
glutPostRedisplay();
break;
case ‘e’:
elbow = (elbow + 5) % 360;
glutPostRedisplay();
break;
case ‘E’:
elbow = (elbow – 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Demonstrates modeling transformations

/*
* model.c
* This program demonstrates modeling transformations
*/
#include <GL/glut.h>
#include <stdlib.h>

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
//glShadeModel (GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void draw_triangle(void)
{
glBegin (GL_LINE_LOOP);
glVertex2f(0.0, 25.0);
glVertex2f(25.0, -25.0);
glVertex2f(-25.0, -25.0);
glEnd();
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);

glLoadIdentity ();
glColor3f (1.0, 1.0, 1.0);
draw_triangle ();

glEnable (GL_LINE_STIPPLE);
glLineStipple (1, 0xF0F0);
glLoadIdentity ();
glTranslatef (-20.0, 0.0, 0.0);
draw_triangle ();

glLineStipple (1, 0xF00F);
glLoadIdentity ();
glScalef (1.5, 0.5, 1.0);
draw_triangle ();

glLineStipple (1, 0x8888);
glLoadIdentity ();
glRotatef (90.0, 0.0, 0.0, 1.0);
draw_triangle ();
glDisable (GL_LINE_STIPPLE);

glFlush ();
}

/*void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}*/

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
//glutReshapeFunc(reshape);
// glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}

Line Triangle Transformation

#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
void triangle()
{
glColor3f(0.0, 1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2i (80, 150);
glVertex2i (80, 250);
glVertex2i (130,200);
glEnd();
}
void line(int sx,int sy,int dx,int dy)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2i (sx, sy);
glVertex2i (dx, dy);
glEnd();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
line(100,150,100,250);
triangle();
glTranslatef(70.0, 20.0,0.0);
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xF0F0);
glScalef (1.5, 0.5, 1.0);
    line(100,150,100,250);
triangle();
glFlush ();
}
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
gluOrtho2D(0.0, 350.0, 0.0, 350.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (350, 350);
glutInitWindowPosition (100, 100);
glutCreateWindow (“Transformation”);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Rectangle drawing program

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
//initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
  A = screenWidth/4.0; //Set values for scaling and shifting
  B= 0.0;
  C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);   //clear the screen
  // glRecti(10,10,150,60); //draw a rectangle with background color
glColor3f(0.4,0.0,0.0);
  glRecti(200,150,500,330);
 /* glBegin(GL_LINES); //draw lines
  {
  glVertex2i(200,150);
  glVertex2i(200,330);
  glVertex2i(500,330);
  glVertex2i(500,150);
  glVertex2i(500,330);
  glVertex2i(500,150);
  glVertex2i(200,150);
  glVertex2i(500,150);
  glVertex2i(200,330);
  glVertex2i(500,330);
  }
  glEnd();*/
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  //glutMouseFunc(myMouse);
  myInit();
  glutMainLoop();  //go into a perpetual loop
  //fprintf(stderr, “Hello!\n”);
  return 1;
}

Polygon

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
//initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
  A = screenWidth/4.0; //Set values for scaling and shifting
  B= 0.0;
  C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);   //clear the screen
  // glRecti(10,10,150,60); //draw a rectangle with background color
 /*glBegin(GL_POINTS); //draw three points
  {
    for(GLint x=0; x<50; x+=2){
      if(x<25)
      glVertex2i(100+x,x);
    else
       glVertex2i(x,x+100);
    }
    }
  glEnd();*/
    glBegin(GL_POLYGON); //draw lines
  {
   glColor3f(1.0,0.0,0.0);
  glVertex2i(100,50);
  glVertex2i(200,130);
  glVertex2i(200,70);
 // glVertex2i(200,130);
  glVertex2i(500,170);
  glVertex2i(300,130);
    }
  glEnd();
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  //glutMouseFunc(myMouse);
  myInit();
  glutMainLoop();  //go into a perpetual loop
  //fprintf(stderr, “Hello!\n”);
  return 1;
}

myMouse program

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
//initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
  A = screenWidth/4.0; //Set values for scaling and shifting
  B= 0.0;
  C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myMouse(int button, int state, int x ,int y)
{
  if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
      glColor4f(0.0,1.0,0.0,1.0);
      glRecti(200,150,500,330);  // create a rectangle
      glFlush();
    }
  else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    {
    //glPointSize(2.0);
      glColor3f(1.0,0.0,0.0);
  GLint h = 350,k = 240, x, y, r, d;
  y = r = 60;
  d = 3 – 2*r;
  for(x=0; x<=y; x++)
    {
  glBegin(GL_POINTS);
      {
glVertex2i(x+h,y+k);
glVertex2i(x+h,k-y);
glVertex2i(y+h,x+k);
glVertex2i(y+h,k-x);
glVertex2i(-y+h,x+k);
glVertex2i(-y+h,k-x);
glVertex2i(-x+h,y+k);
glVertex2i(-x+h,k-y);
      }
      glEnd();
  if(d<0)
  d = d + 4*x + 6;
  else{
  d = d + 4*(x-y) + 10;
  y–;
  }
}
  glFlush();
  } //elseif
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  glutMouseFunc(myMouse);
  myInit();
  glutMainLoop();  //go into a perpetual loop
  //fprintf(stderr, “Hello!\n”);
  return 1;
}

Clipping program

/*
Program : Clipping
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<sstream>
#include<stack>
#include<list>
#include<iterator>
#include <windows.h>
#include <gl/glut.h>
using namespace std;
#define REP(i,n) for(i=0; i<(n); i++)
#define FOR(i,a,b) for(i=(a); i<=(b); i++)
#define CLEAR(t) memset((t), 0, sizeof(t))
#define sz size()
#define pb push_back
#define pf push_front
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define WIDTH 640
#define HEIGHT 480
#define pi 2*acos(0.0)
#define Wxmin 0
#define Wxmax 200
#define Wymin 0
#define Wymax 200
struct wind
{
double x[4],y[4];
}W;
double XX = .2,YY=.3;
struct Point
{
double x;
double y;
double z;
double w;
}P[4],D,PP[4],QQ[4];
int cho,angle;
double M[4][4],R[4][4],Q,zp;
typedef unsigned int outcode;
enum{TOP = 0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8};
void reshape(int width, int height)
{
glViewport(0,0,width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-WIDTH/2, WIDTH/2+1, -HEIGHT/2, HEIGHT/2+1,-1000,1000);
}
void drawPixel(int x, int y, int op)
{
if(op == 0)glVertex2i(x,y);
else if(op == 3)glVertex2i(-x,y);
else if(op == 4)glVertex2i(-x,-y);
else if(op == 7)glVertex2i(x,-y);
else if(op == 1)glVertex2i(y,x);
else if(op == 2)glVertex2i(-y,x);
else if(op == 5)glVertex2i(-y, -x);
else if(op == 6)glVertex2i(y, -x);
}
void drawLine(int x0,int y0,int x1,int y1,int value)
{
int dx = x1 – x0;
int dy = y1 – y0;
int dinitial = 2*dy – dx;
int dE = 2*dy;
int dNE = 2*(dy – dx);
int x = x0;
int y = y0;
drawPixel(x,y,value);
while ( x < x1 )
{
if( dinitial < 0 )
{
x ++;
dinitial += dE;
}
else
{
x++;
y++;
dinitial += dNE;
}
drawPixel(x,y,value);
}
}
void drawSlop(int x0, int y0, int x1, int y1)
{
int dx = x1 – x0;
int dy = y1 – y0;
if(abs(dx)>=abs(dy))//groupe 0
{
if((x1>=x0)&&(y1>=y0))
drawLine(x0,y0,x1,y1,0);//op=0
else if((x1<x0)&&(y1>=y0))
drawLine(-x0,y0,-x1,y1,3);//op3
else if((x1<x0) && (y1<y0))
drawLine(-x0,-y0,-x1,-y1,4);//op = 4
else
drawLine(x0,-y0,x1,-y1,7);//op = 7
}
else//groupe = 1…
{
if((x1>=x0)&&(y1>=y0))
drawLine(y0,x0,y1,x1,1);//op=1
else if((x1<x0)&&(y1>=y0))
drawLine(y0,-x0,y1,-x1,2);//op=2
else if((x1<x0)&&(y1<y0))
drawLine(-y0,-x0,-y1,-x1,5);//op=5
else if((x1>=x0)&&(y1<=y0))
drawLine(-y0,x0,-y1,x1,6);//op=6
}
}
void getPoint()
{
FILE *fp;
int i;
fp = fopen(“input.txt”,”r”);
REP(i,4)
{
fscanf(fp,”%lf %lf %lf”,&P[i].x,&P[i].y,&P[i].z);
}
fclose(fp);
}
outcode CompOutCode(double x,double y,double xmin,double xmax,double ymin,double ymax)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;
}
void CohenSutherlandLineClipAndDraw(double x0,double y0,double x1,double y1,double xmin,double xmax,double ymin,double ymax)
{
outcode outcode0,outcode1,outcodeOut;
bool accept = false,done = false;
outcode0 = CompOutCode(x0,y0,xmin,xmax,ymin,ymax);
outcode1 = CompOutCode(x1,y1,xmin,xmax,ymin,ymax);
do{
if(!(outcode0|outcode1)){
accept = true,done = true;
}
else if(outcode0&outcode1)
done = true;
else
{
double x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut&TOP){
x = x0 + (x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else if(outcodeOut&BOTTOM){
x = x0 + (x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else if(outcodeOut&RIGHT){
y = y0 + (y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0 + (y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0= y;
outcode0 = CompOutCode(x0,y0,xmin,xmax,ymin,ymax);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1,xmin,xmax,ymin,ymax);
}
}
}while(done==false);
if(accept)
{
drawSlop(x0,y0,x1,y1);
}
}
void drawTetra()
{
int i,j;
REP(i,4)
{
j = (i + 1)%4;
drawSlop((int)P[i].x,(int)P[i].y,(int)P[j].x,(int)P[j].y);
}
REP(i,2)
{
j = (i + 2)%4;
drawSlop(P[i].x,P[i].y,P[j].x,P[j].y);
}
}
void drawTetraC()
{
int i,j;
REP(i,4)
{
j = (i + 1)%4;
CohenSutherlandLineClipAndDraw(P[i].x,P[i].y,P[j].x,P[j].y,W.x[0],W.x[1],W.y[0],W.y[2]);
}
REP(i,2)
{
j = (i + 2)%4;
CohenSutherlandLineClipAndDraw(P[i].x,P[i].y,P[j].x,P[j].y,W.x[0],W.x[1],W.y[0],W.y[2]);
}
}
void findQ()
{
Q = sqrt((D.x*D.x) + (D.y*D.y) + (D.z)*(D.z));
}
void getPos()
{
FILE *fp;
fp = fopen(“P.txt”,”r”);
fscanf(fp,”%lf %lf %lf”,&D.x,&D.y,&D.z);
fscanf(fp,”%lf”,&zp);
D.z=D.z-zp;
findQ();
D.x/=Q;
D.y/=Q;
D.z/=Q;
fclose(fp);
}
void setW()
{
W.x[0] = Wxmin;
W.y[0] = Wymin;
W.x[1] = Wxmax;
W.y[1] = Wymin;
W.x[2] = Wxmax;
W.y[2] = Wymax;
W.x[3] = Wxmin;
W.y[3] = Wymax;
}
void drawW()
{
int i,j;
REP(i,4)
{
j = (i + 1)%4;
drawSlop((int)W.x[i],(int)W.y[i],(int)W.x[j],(int)W.y[j]);
}
}
void resetW()
{
int i;
if(W.x[0]+XX<=-320||W.x[1]+XX>=320)
XX = -XX;
if(W.y[0]+YY<=-240||W.y[2]+YY>=240)
YY = -YY;
REP(i,4)
W.x[i]+=XX,W.y[i]+=YY;
}
void display(void)
{
glColor4f(1.0,0.0,0.0,1.0);
angle = 0;
getPoint();
getPos();
setW();
while(true)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POINTS);
resetW();
glColor4f(3.0,5.0,1.0,1.0);
drawW();
glColor4f(0.0,0.0,0.0,0.0);
drawTetra();
glColor4f(1.0,0.0,0.0,1.0);
drawTetraC();
glEnd();
glutSwapBuffers();
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(-1,-1);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow(“My Window”);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

ellipse bresenham

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640*2;
const int screenHeight = 480*2;
GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
//initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
  A = screenWidth/4.0; //Set values for scaling and shifting
  B= 0.0;
  C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);   //clear the screen
  // glRecti(10,10,150,60); //draw a rectangle with background color
  glPointSize(2.0);
  GLint h = 300, k = 300, x, x2, y, a = 150, b = 25;
  x = 0,y=b;
int aa=a*a,bb=b*b,aa2=aa*2,bb2=bb*2;
int fx=0, fy=aa2*b;
int p= bb-aa*b+0.25*aa;
while(fx<fy)
{
  glBegin(GL_POINTS);
      {
glVertex2i(x+h,y+k);
glVertex2i(-x+h,-y+k);
glVertex2i(-x+h,y+k);
glVertex2i(x+h,-y+k);
}
      glEnd();
x++;
fx=fx+bb2;
if(p<0)
p=p+fx+bb;
else{
y–;
fy=fy-aa2;
p=p+fx+bb-fy;
}
}
glBegin(GL_POINTS);
      {
glVertex2i(x+h,y+k);
glVertex2i(-x+h,-y+k);
glVertex2i(-x+h,y+k);
glVertex2i(x+h,-y+k);
}
      glEnd();
p=bb*(x+0.5)*(x+0.5)+aa*(y-1)*(y-1)-aa*bb;
while(y>0){
y–;
fy=fy-aa2;
if(p>=0)
p=p-fy+aa;
else{
x++;
fx=fx+bb2;
p=p+fx-fy+aa;
}
glBegin(GL_POINTS);
      {
glVertex2i(x+h,y+k);
glVertex2i(-x+h,-y+k);
glVertex2i(-x+h,y+k);
glVertex2i(x+h,-y+k);
}
      glEnd();
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  myInit();
  glutMainLoop();  //go into a perpetual loop
return 1;
}

Ellipse drawing program

#include<windows.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640*2;
const int screenHeight = 480*2;
GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
//initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
  A = screenWidth/4.0; //Set values for scaling and shifting
  B= 0.0;
  C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);   //clear the screen
  // glRecti(10,10,150,60); //draw a rectangle with background color
  glPointSize(2.0);
  GLint h = 300, k = 300, x, x2, y, a = 150, b = 25;
  x2 = a;
  for(x=1;x<=x2;x++)
    {
      y = b * sqrt(1 – (pow(x,2)/pow(a,2)));
  glBegin(GL_POINTS);
      {
glVertex2i(x+h,y+k);
glVertex2i(-x+h,-y+k);
glVertex2i(-x+h,y+k);
glVertex2i(x+h,-y+k);
}
      glEnd();
}
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  myInit();
  glutMainLoop();  //go into a perpetual loop
return 1;
}

Bresenham Circle drawing program

//#include<windows.h>
//#include<iostream.h>
//#include<stdio.h>
//#include<stdlib.h>
//#include<string.h>
//#include<math.h>
//#include<GL/gl.h>
//#include<GL/glu.h>
//#include<GL/glut.h>
#include<windows.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
//const int screenWidth = 640*2;
//const int screenHeight = 480*2;
//GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
  //initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  //glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
 // A = screenWidth/4.0; //Set values for scaling and shifting
 // B= 0.0;
 // C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);   //clear the screen
  // glRecti(10,10,150,60); //draw a rectangle with background color
  glPointSize(2.0);
  GLint h = 200,k = 200, x, y, r, d;
  y = r = 100;
  d = 3 – 2*r;
  for(x=0; x<=y; x++)
    {
  glBegin(GL_POINTS);
      {
glVertex2i(x+h,y+k);
glVertex2i(x+h,k-y);
glVertex2i(y+h,x+k);
glVertex2i(y+h,k-x);
glVertex2i(-y+h,x+k);
glVertex2i(-y+h,k-x);
glVertex2i(-x+h,y+k);
glVertex2i(-x+h,k-y);
      }
      glEnd();
  if(d<0)
  d = d + 4*x + 6;
  else{
  d = d + 4*(x-y) + 10;
  y–;
  }
}
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  myInit();
  glutMainLoop();  //go into a perpetual loop
return 1;
}