Showing posts with label CG lab. Show all posts
Showing posts with label CG lab. Show all posts

Wednesday, 6 November 2013

[NEW] Window to ViewPort Mapping | Computer Graphics Lab Manual | IT department | Final Year




5(b)PROGRAM:
#include
#include
#include
#include
void main()
{float sx,sy;
 int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
 int gd=DETECT,gm;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n");
 scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
 cleardevice();
 w1=5;
 w2=5;
 w3=635;
 w4=465;
 rectangle(w1,w2,w3,w4);
 line(x1,y1,x2,y2);
 line(x2,y2,x3,y3);
 line(x3,y3,x1,y1);
 getch();
 v1=425;
 v2=75;
 v3=550;
 v4=250;
 sx=(float)(v3-v1)/(w3-w1);
 sy=(float)(v4-v2)/(w4-w2);
 rectangle(v1,v2,v3,v4);
 x1=v1+floor(((float)(x1-w1)*sx)+.5);
 x2=v1+floor(((float)(x2-w1)*sx)+.5);
 x3=v1+floor(((float)(x3-w1)*sx)+.5);
 y1=v2+floor(((float)(y1-w2)*sy)+.5);
 y2=v2+floor(((float)(y2-w2)*sy)+.5);
 y3=v2+floor(((float)(y3-w2)*sy)+.5);
 line(x1,y1,x2,y2);
 line(x2,y2,x3,y3);
 line(x3,y3,x1,y1);
 getch();
}

Cohen Sutherland 2D Line Clipping Aglorithm | Computer Graphics Lab Manual | IT department | Final Year

EX 5.a

5(a)PROGRAM:

#include
#include
#include
float cxa,cxb,cya,cyb;
code(float,float);
void clip(float,float,float,float);
void rect(float,float,float,float);
void main()
{
 float x1,x2,y1,y2;
 int g=0,d;
 initgraph(&g,&d," ");
 setbkcolor(9);
 settextstyle(3,0,1);
 outtextxy(40,15,"Before Clipping");
 printf("\n\n\n\nEnter left right top bottom of clipping window:\n");
 scanf("%f%f%f%f",&cxa,&cyb,&cxb,&cya);
 getch();
 printf("\n Enter the line x1,y1,x2,y2:\n");
 scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
 getch();
 rect(cxa,cyb,cxb,cya);
 line(x1,y1,x2,y2);
 getch();
 cleardevice();
 setbkcolor(9);
 settextstyle(3,0,1);
 outtextxy(40,15,"After Clipping");
 clip(x1,y1,x2,y2);
 getch();
 closegraph();
}
void clip(float x1,float y1,float x2,float y2)
{
 int c,c1,c2;
 float x,y;
 c1=code(x1,y1);
 c2=code(x2,y2);
 getch();
 while((c1!=0)||(c2!=0))
 {
  if((c1&c2)!=0)
   goto out;
  c=c1;
  if(c==0)
   c=c2;
  if((c&1)==1)
  {
   y=y1+((y2-y1)*(cxa-x1))/(x2-x1);
   x=cxa;
  }
  else if((c&2)==2)
  {
   y=y1+((y2-y1)*(cxb-x1))/(x2-x1);
   x=cxb;
  }
  else if((c&8)==8)
  {
   x=x1+((x2-x1)*(cyb-y1))/(y2-y1);
   y=cyb;
  }
  else if((c&4)==4)
  {
   x=x1+((x2-x1)*(cya-y1))/(y2-y1);
   y=cya;
  }
  if(c==c1)
  {
   x1=x;
   y1=y;
   c1=code(x,y);
  }
  else
  {
   x2=x;
   y2=y;
   c2=code(x,y);
   //goto out;
  }
 }
 line(x1,y1,x2,y2);
 out:
 rect(cxa,cyb,cxb,cya);
}
code(float x,float y)
{
 int c=0,a=0,b=0;
 if(x
  a=1;
 else if(x>cxb)
  a=2;
 if(y
  b=8;
 else if(y>cya)
  b=4;
 c=a+b;
 return c;
}
void rect(float x1,float yb,float xr,float yt)
{
 line(x1,yb,xr,yb);
 line(xr,yb,xr,yt);
 line(xr,yt,x1,yt);
 line(x1,yt,x1,yb);
}


IMPLEMENTATION OF ELLIPSE ATTRIBUTES in C Aglorithm | Computer Graphics Lab Manual | IT department | Final Year




2(c)PROGRAM:

#include
#include
#include
#include
#include
void main()
{
int gd=DETECT,gm;
int ch;
clrscr();
while(1)
{printf("******Ellipse Attributes*******\n");
printf("\n1.Empty Fill");
printf("\n2.Soild Fill");
printf("\n3.Line Fill");
printf("\n4.LTSlash Fill");
printf("\n5.Slash Fill");
printf("\n6.BKSlash Fill");
printf("\n7.LTBKSlash Fill");
printf("\n8.Hatch Fill");
printf("\n9.Xhatch Fill");
printf("\n10.Interleave Fill");
printf("\n11.Wide dot Fill");
printf("\n12.close dot Fill");
printf("\n13.User Fill");
printf("\n14.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{case 1:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(EMPTY_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 2:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(SOLID_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 3:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LINE_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 4:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LTSLASH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 5:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(SLASH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 6:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(BKSLASH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 7:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LTBKSLASH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 8:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(HATCH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 9:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(XHATCH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 10:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(INTERLEAVE_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 11:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(WIDE_DOT_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 12:clrscr();
initgraph(&gd,&gm," ");
setfillstyle(CLOSE_DOT_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 13:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(USER_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 14:
exit(0);
}
}

}

IMPLEMENTATION OF CIRCLE ATTRIBUTES in C Aglorithm | Computer Graphics Lab Manual | IT department | Final Year



2(b)PROGRAM:

#include
#include
#include
#include
#include
void main()
{
int gd=DETECT,gm;
int ch;
clrscr();
while(1)
{
printf("******Circle Attributes*******\n");
printf("\n1.Empty Fill");
printf("\n2.Soild Fill");
printf("\n3.Line Fill");
printf("\n4.LTSlash Fill");
printf("\n5.Slash Fill");
printf("\n6.BKSlash Fill");
printf("\n7.LTBKSlash Fill");
printf("\n8.Hatch Fill");
printf("\n9.Xhatch Fill");
printf("\n10.Interleave Fill");
printf("\n11.Wide dot Fill");
printf("\n12.close dot Fill");
printf("\n13.User Fill");
printf("\n14.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(EMPTY_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 2:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(SOLID_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 3:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LINE_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 4:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LTSLASH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 5:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(SLASH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 6:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(BKSLASH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 7:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LTBKSLASH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 8:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(HATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 9:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 10:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(INTERLEAVE_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 11:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(WIDE_DOT_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 12:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(CLOSE_DOT_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 13:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(USER_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 14:
exit(0);
}
}

}




IMPLEMENTATION OF LINE ATTRIBUTES in C Aglorithm | Computer Graphics Lab Manual | IT department | Final Year



#include
#include
#include
#include
#include
void main()
{
int gd=DETECT,gm;
int ch;
clrscr();
while(1)
{
printf("******Line Styles*******\n");
printf("\n1.Solid Line");
printf("\n2.Dotted Line");
printf("\n3.Center Line");
printf("\n4.Dashed Line");
printf("\n5.Userbit Line");
printf("\n6.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();
initgraph(&gd,&gm," ");
setlinestyle(0,1,3);
line(100,30,250,250);
getch();
cleardevice();closegraph();
break;
case 2:
clrscr();
initgraph(&gd,&gm," ");
clrscr();
setlinestyle(1,1,3);
line(100,30,250,250);
getch();
cleardevice();closegraph();
break;
case 3:
clrscr();
initgraph(&gd,&gm," ");
setlinestyle(2,1,3);
line(100,30,250,250);
getch();
cleardevice();closegraph();
break;
case 4:
clrscr();
initgraph(&gd,&gm," ");
setlinestyle(3,1,3);
line(100,30,250,250);
getch();
cleardevice();
closegraph();
break;
case 5:
clrscr();
initgraph(&gd,&gm," ");
setlinestyle(4,1,3);
line(100,30,250,250);
getch();
cleardevice();closegraph();
break;
case 6:
exit(0);
}
}

}

Bresenham's Line Drawing Algorithm using OpenGL | Computer Graphics Lab Manual | Anna University Chennai | Final Year IT Department



12:PROGRAM:

#include
#include
#include
void init(void)
{
            glClearColor(1.0,1.0,1.0,0.0);
            glMatrixMode(GL_PROJECTION);
            gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel(GLint xcoord,GLint ycoord)
{
            glBegin(GL_POINTS);
            glVertex2i(xcoord,ycoord);
            glEnd();
            glFlush();
}
void line(GLint x0,GLint y0,GLint xend,GLint yend)
{
            GLint dx=fabs(xend-x0);
            GLint dy=fabs(yend-y0);
            GLint p=2*dy-dx;
            GLint twody=2*dy;
            GLint twodyminusdx=2*(dy-dx);
            GLint x,y;
            if(x0>xend)
            {
                        x=xend;
                        y=yend;
                        xend=x;
            }
            else
            {
                        x=x0;
                        y=y0;
            }
            setPixel(x,y);
            while(x
            {
                        x++;
                        if(p<0 font="">
                                    p+=twody;
                        else
                        {
                                    y++;
                                    p+=twodyminusdx;
                        }
                        setPixel(x,y);
            }
}
void drawMyLine(void)
{
            glClear(GL_COLOR_BUFFER_BIT);
            glColor3f(0.0,1.0,0.0);
            glPointSize(4.0);
            GLint x0=10;
            GLint y0=15;
            GLint xend=250;
            GLint yend=100;
            line(x0,y0,xend,yend);
}
void main(int argc,char** argv)
{
            glutInit(&argc,argv);
            glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
            glutInitWindowSize(400,400);
            glutInitWindowPosition(0,0);
            glutCreateWindow("Bresenham's algorithm");
            init();
            glutDisplayFunc(drawMyLine);
            glutMainLoop();
}



DDA Line Drawing Algorithm using OPEN GL | Computer Graphics Lab Manual | Anna University Chennai | Final Year IT Department




11:PROGRAM:

#include
#include
#include
inline GLint round(const GLfloat a)
{
            return GLint (a+0.5);
}
void init(void)
{
            glClearColor(1.0,1.0,1.0,0.0);
            glMatrixMode(GL_PROJECTION);
            gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel(GLint xCoord,GLint yCoord)
{
            glBegin(GL_POINTS);
            glVertex2i(xCoord,yCoord);
            glEnd();
            glFlush();
}
void lineDDA(GLint x0,GLint y0,GLint xend,GLint yend)
{
            GLint dx=xend-x0;
            GLint dy=yend-y0;
            GLint steps,k;
            GLfloat xIncrement,yIncrement,x=x0,y=y0;
            if(fabs(dx)>fabs(dy))
                        steps=fabs(dx);
            else
                        steps=fabs(dy);
            xIncrement=GLfloat(dx)/GLfloat(steps);
            yIncrement=GLfloat(dy)/GLfloat(steps);
            setPixel(round(x),round(y));
            for(k=0;k
            {
                        x+=xIncrement;
                        y+=yIncrement;
                        setPixel(round(x),round(y));
            }
}
void drawMyLine(void)
{
            glClear(GL_COLOR_BUFFER_BIT);
            glColor3f(1.0,0.0,0.0);
            glPointSize(4.0);
            GLint x0=100;
            GLint y0=100;
            GLint xend=200;
            GLint yend=200;
            lineDDA(x0,y0,xend,yend);
}
void main(int argc,char** argv)
{
            glutInit(&argc,argv);
            glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
            glutInitWindowSize(500,500);
            glutInitWindowPosition(0,0);
            glutCreateWindow("DDA algorithm");
            init();
            glutDisplayFunc(drawMyLine);
            glutMainLoop();
}


Generating Fractal Images in Computer Graphics (CG) | Computer Graphics Lab Manual | Anna University Chennai | Final Year IT Department



10:PROGRAM:

#include
#include
#include
#include
#include
void DrawSierpinski(void);
void main(void)
{int gd=VGA;
int gm=VGAHI;
initgraph(&gd, &gm, "\\tc\\bgi");
DrawSierpinski();
getch();
}
void DrawSierpinski(void)
{char Direct;
int iterate;
unsigned int x1, y1, x2, y2;
x1 = x2 = 320;
y1 = y2 = 0;
for(iterate = 0; iterate < 10000; iterate++)
{Direct = random(3);
if(Direct == 0)
{
x1 = (x2 + 320) / 2;
y1 = (y2 + 0) / 2;        
}
else if(Direct == 1)
{x1 = (x2 + 0) / 2;
y1 = (y2 + 480) / 2;
}
else if(Direct == 2)
{
x1 = (x2 + 640) / 2;    
y1 = (y2 + 480) / 2;
}
putpixel(x1, y1, WHITE);
x2 = x1;
y2 = y1;

}}

Drawing 3D Object and Scenes | Computer Graphics Lab Manual | Anna University Chennai | Final Year IT Department



9:PROGRAM:

#include
#include
void drawCube(){
    glBegin(GL_QUADS);
    glColor3f(1,0,0);
    glVertex3f(1,1,1);
    glVertex3f(-1,1,1);
    glVertex3f(-1,-1,1);
    glVertex3f(1,-1,1);
    glColor3f(0,1,1);
    glVertex3f(1,1,-1);
    glVertex3f(-1,1,-1);
    glVertex3f(-1,-1,-1);
    glVertex3f(1,-1,-1);
    glColor3f(0,1,0);
    glVertex3f(1,1,1);
    glVertex3f(1,-1,1);
    glVertex3f(1,-1,-1);
    glVertex3f(1,1,-1);
    glColor3f(1,0,1);
    glVertex3f(-1,1,1);
    glVertex3f(-1,-1,1);
    glVertex3f(-1,-1,-1);
    glVertex3f(-1,1,-1);
    glColor3f(0,0,1);
    glVertex3f(1,1,1);
    glVertex3f(-1,1,1);
    glVertex3f(-1,1,-1);
    glVertex3f(1,1,-1);
    glColor3f(1,1,0);
    glVertex3f(1,-1,1);
    glVertex3f(-1,-1,1);
    glVertex3f(-1,-1,-1);
    glVertex3f(1,-1,-1);
    glEnd();
}
void idle(){
    glutPostRedisplay();}
float deg= 0.0;
void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0,0,-3);
    deg += 0.01;
    glRotatef(2*deg,0,0,1);
    glRotatef(deg,0,1,0);
    drawCube();
glutSwapBuffers();
}
void reshape(int width,int height){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,width,height);
    float fovy = 60;
    float aspect_ratio = float(width) / height;
    float near_clip = 0.01;
    float far_clip = 100;
   gluPerspective(fovy, aspect_ratio, near_clip, far_clip );
   glMatrixMode(GL_MODELVIEW);
}
void key(unsigned char key,int x,int y){
    if(key == 27)//esc
        exit(0);}
void init(){
    glutInitWindowSize(800,600);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Tutorial Basics");
   glutIdleFunc(idle);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(key);
   glEnable(GL_DEPTH_TEST);
}
int main(int argc, char ** argv){
    glutInit(&argc, argv);
    init();
    glutMainLoop();
    return 0;

}                           

Composite 3D Transformation | Computer Graphics Lab Manual | Anna University Chennai | Final Year IT Department

8:PROGRAM:

#include
#include
static int shoulder = 0, elbow = 0;
void init(void)
{
   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;
}






Back To Top