75
EX.NO: 1(a) BRESENHAM’S LINE DRAWING ALGORITHM DATE: Aim: To implement Bresenham’s line drawing Algorithm for drawing lines. ALGORITHM: STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: get line coordinates x1,y1 and x2,y2. STEP 4: calculate dx from x1,x2 and dy from y1,y2 coordinates. STEP 5: calculate e (error) value. STEP 6: initialize i=1. STEP 7: putpixel(x,y,15). STEP 8: check e>=0 is true then calculate y=y+1 and e=e-2*dx else calculate x=x+1 and e=e+2*dy and i=i+1. STEP 9: repeat steps 7 and 8 until i<=dx. STEP 10: Display a line.

Cg Lab Manual

Embed Size (px)

DESCRIPTION

Easy manual for students

Citation preview

Page 1: Cg Lab Manual

EX.NO: 1(a) BRESENHAM’S LINE DRAWING ALGORITHMDATE:

Aim:               To implement Bresenham’s line drawing Algorithm for drawing lines.

ALGORITHM:

STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: get line coordinates x1,y1 and x2,y2. STEP 4: calculate dx from x1,x2 and dy from y1,y2 coordinates. STEP 5: calculate e (error) value. STEP 6: initialize i=1. STEP 7: putpixel(x,y,15). STEP 8: check e>=0 is true then calculate y=y+1 and e=e-2*dx else calculate x=x+1 and e=e+2*dy and i=i+1. STEP 9: repeat steps 7 and 8 until i<=dx. STEP 10: Display a line.

Page 2: Cg Lab Manual

PROGRAM:#include<stdio.h> #include<graphics.h> #include<math.h> #include<conio.h> void main() { float x,y,x1,y1,x2,y2,dx,dy,e; int i,gd,gm; clrscr(); printf("Enter the (x1,y1) coordinate:"); scanf("%f%f",&x1,&y1); printf("Enter the (x2,y2) coordinate:"); scanf("%f%f",&x2,&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI\\"); dx=abs(x2-x1); dy=abs(y2-y1); x=x1; y=y1; e=2*dy-dx; i=1; do { putpixel(x,y,15); while(e>=0) { y=y+1; e=e-2*dx; } x=x+1; e=e+2*dy; i=i+1; } while(i<=dx); getch(); closegraph(); }

Page 3: Cg Lab Manual

INPUT: Enter the (x1, y1) coordinates: 200 100 Enter the (x2, y2) coordinates: 500 100

RESULT: Thus the above program for implementation of Bresenham’s line drawing algorithm was

successfully executed.

Page 4: Cg Lab Manual

EX.NO: 1(B) BRESENHAM’S CIRCLE DRAWING ALGORITHMDATE:

Aim:To implement the Bresenham’s circle drawing algorithm using c language.

ALGORITHM: [

STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: get the radius of a circle. STEP 4: initialize the variable x=0,y=r and d=3-2*r. STEP 5: putpixel(x,y,15). STEP 6: If d<=0 then calculate d=d+4*x+6 else calculate d=d+4*(x-y)+10 and y=y-1. STEP 7: increment x=x+1. STEP 8: repeat steps 5,6 and 7 until x<y. STEP 9: Display a circle.

Page 5: Cg Lab Manual

PROGRAM:#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { float d; int gd,gm,x,y,r; clrscr(); printf("Enter the radius of a circle:"); scanf("%d",&r); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI\\"); x=0; y=r; d=3-2*r; do { putpixel(200+x,200+y,15); putpixel(200+y,200+x,15); putpixel(200+y,200-x,15); putpixel(200+x,200-y,15); putpixel(200-x,200-y,15); putpixel(200-y,200-x,15); putpixel(200-y,200+x,15); putpixel(200-x,200+y,15); if(d<=0) { d=d+4*x+6; } else { d=d+4*(x-y)+10; y=y-1; } x=x+1; } while(x<y); getch(); closegraph(); }

Page 6: Cg Lab Manual

INPUT: Enter the radius of a circle: 100

RESULT: Thus the above program for implementation of Bresenham’s circle drawing algorithm was successfully executed.

Page 7: Cg Lab Manual

EX.NO: 1(C) BRESENHAM’S ELLIPSE DRAWING ALGORITHMDATE:

Aim:To write a C program for Circle Drawing Using Bresenham Algorithm.

ALGORITHM:

Step 1: Start the program.Step 2: Initialize the variables.Step 3: Call the initgraph() function.Step 4: Get the initialize points P1,P2.Step 5: Get the values of Co-Ordinates of the ellipse (x,y).Step 6: Enter the coordinates a,b of the ellipse .Step 7: Display the output.Step 8: Stop the program

Page 8: Cg Lab Manual

PROGRAM#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { int gd,gm,x1=0,y1=0,r1=0,r2=0; float x=0,y=0,t,d,i;clrscr(); printf("Enter the co-ordinates of a circle:"); scanf("%d%d",&x1,&y1); printf("Enter the radius of a circle:"); scanf("%d%d",&r1,&r2); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI\\"); for(i=0;i<360;i++){t=3.14/180;d=i*t;x=x1+ceil(r1*sin(d));y=y1+ceil(r2*cos(d));putpixel(x,y,15);}getch(); closegraph(); }

Page 9: Cg Lab Manual

PROGRAM#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>void disp();float x,y;int xc,yc;void main(){int gd=DETECT,gm;int a,b;float p1,p2;clrscr();initgraph(&gd,&gm,"");scanf("%d%d",&xc,&yc);scanf("%d%d",&a,&b);x=0;y=b;disp();p1=(b*b)-(a*a*b)+(a*a)/4;while((2.0*b*b*x)<=(2.0*a*a*y)){x++;if(p1<=0)p1=p1+(2.0*b*b*x)+(b*b);else{y--;p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);}disp();x=-x;disp();x=-x;}x=a;y=0;disp();p2=(a*a)+2.0*(b*b*a)+(b*b)/4;while((2.0*b*b*x)>(2.0*a*a*y)){y++;if(p2>0)p2=p2+(a*a)-(2.0*a*a*y);else{

Page 10: Cg Lab Manual

x--;p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);}disp();y=-y;disp();y=-y;}getch();closegraph();}void disp(){putpixel(xc+x,yc+y,10);putpixel(xc-x,yc+y,10);putpixel(xc+x,yc-y,10);putpixel(xc+x,yc-y,10);}

Input:Enter the center co-or:100 150Enter the radius1:40 Enter radius2:20

Result:         Thus the program to draw ellipse using Bresenham’s ellipse drawing Algorithm was executed successfully.

Page 11: Cg Lab Manual

EX.NO: 2 IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTESDATE:

Aim:               To write a program to draw a Line, Circle and ellipse Attributes

Functions used:Circle()

      The function circle() is used to draw a circle using(x,y) as centre point.

Syntax:        circle (x,y,radius)

initgraph().

       This function takes thee arguments and they are          i).the video driver to be used (gd).         ii).the graphics mode (gm).         iii).the path name.

Syntax:       Initgraph(gd,gm,path)

Putpixel ()      The function putpixel() is used to place a pixel at particular coordinate

Syntax:Putpixel(x,y,color)

Algorithm:Step 1: StartStep 2: Get the center point as (xc,yc),get the radius as r.Step 3: Assign y=r,x=0Step 4: Calculate p=3-2rStep 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and y=y-1Step 6: Increment x by 1Step 7:stop

Page 12: Cg Lab Manual

PROGRAM:

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>#define ROUND(a) ((int)(a+0.5))//Line drawingvoid lineBres(int xa,int ya,int xb,int yb){int dx=abs(xa-xb),dy=abs(ya-yb);int p=2*dy-dx;int twoDy=2*dy,twoDyDx=2*(dy-dx);int x,y,xEnd;/* Determine which point to use as start,which as end */if(xa>xb){x=xb;y=yb;xEnd=xa;}Else{x=xa;y=ya;xEnd=xb;}putpixel(x,y,15);while(x<xEnd){x++;if(p<0)p+=twoDy;else{y++;p+=twoDyDx;}putpixel(x,y,15);}}

Page 13: Cg Lab Manual

//Circle drawingvoid circleMidPoint(int xCenter,int yCenter,int radius){int x=0;int y=radius;int p=1-radius;void circlePlotPoints(int,int,int,int);/*plot first set of points*/circlePlotPoints(xCenter,yCenter,x,y);while(x<y){x++;if(p<0)p+=2*x+1;else{y--;p+=2*(x-y)+1;}circlePlotPoints(xCenter,yCenter,x,y);}}void circlePlotPoints(int xCenter,int yCenter,int x,int y){putpixel(xCenter+x,yCenter+y,WHITE);putpixel(xCenter-x,yCenter+y,WHITE);putpixel(xCenter+x,yCenter-y,WHITE);putpixel(xCenter-x,yCenter-y,WHITE);putpixel(xCenter+y,yCenter+x,WHITE);putpixel(xCenter-y,yCenter+x,WHITE);putpixel(xCenter+y,yCenter-x,WHITE);putpixel(xCenter-y,yCenter-x,WHITE);}//Ellipse drawingvoid ellipsemidpoint(int xcenter,int ycenter,int rx,int ry){int rx2=rx*rx;int ry2=ry*ry;int tworx2=2*rx2;int twory2=2*ry2;int p,x=0,y=ry,px=0;int py=tworx2*y;

Page 14: Cg Lab Manual

void ellipseplotpoints(int,int,int,int);ellipseplotpoints(xcenter,ycenter,x,y);p=ROUND(ry2-(rx2*ry)+(0.25*rx2));while(px<py){x++;px+=twory2;if(p<0)p+=ry2+px;else{y--;py-=tworx2;p+=ry2+px-py;}ellipseplotpoints(xcenter,ycenter,x,y);}p=ROUND(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);while(y>0){y--;py-=tworx2;if(p>0)p+=rx2-py;else{x++;px+=twory2;p+=rx2-px+px;}ellipseplotpoints(xcenter,ycenter,x,y);}}void ellipseplotpoints(int xcenter,int ycenter,int x,int y){putpixel(xcenter+x,ycenter+y,5);putpixel(xcenter-x,ycenter+y,5);putpixel(xcenter+x,ycenter-y,5);putpixel(xcenter-x,ycenter-y,5);}void main()

Page 15: Cg Lab Manual

{int ch,c;co:clrscr();printf("\n\t\tBRESENHAM BDRAWINGS\n");printf("\n\t\t1-Line drawing");printf("\n\t\t2-Circle drawing");printf("\n\t\t3-Ellipse drawing");printf("\n\t\t4-Exit");printf("\nEnter your choice :");scanf("%d",&ch);int gdriver = DETECT, gmode;initgraph(&gdriver, &gmode, " ");switch(ch){case 1:int x1,y1,x2,y2;printf("Enter the starting co-ordinates: ");scanf("%d %d",&x1,&y1);printf("Enter the ending co-ordinates: ");scanf("%d %d",&x2,&y2);lineBres(x1,y1,x2,y2);getch();printf("\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):");scanf("%d",&c);if (c==1)goto co;break;

case 2:int xc1,yc1,r;printf("Enter the centre co-ordinates: ");scanf("%d %d",&xc1,&yc1);printf("Enter the radius: ");scanf("%d",&r);circleMidPoint(xc1,yc1,r);getch();printf("\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):");scanf("%d",&c);if(c==1)goto co;

Page 16: Cg Lab Manual

break;

case 3:int xc,yc,rx,ry;printf("Enter the value of xcenter and ycenter co-ordinates: ");scanf("%d %d",&xc,&yc);printf("Enter the radius of x and y: ");scanf("%d %d",&rx,&ry);ellipsemidpoint(xc,yc,rx,ry);getch();printf("\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):");scanf("%d",&c);if (c==1)goto co;break;

case 4: break;}}

Result:         Thus the program to draw and implement Line, Circle and ellipse Attributes was executed successfully.

EX.NO:3 TWO DIMENSIONAL TRANSFORMATIONSDATE:

AIM:

Page 17: Cg Lab Manual

To write a c program for 2D transformation.

ALGORITHM:

STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: Display a Menu and get choice from user. STEP 4: get the object (triangle) coordinates. STEP 5: Display the original object position. STEP 6: a. if the choice is 1 then get translation vertex from user and translate the object to given vertex. b. if the choice is 2 then get rotation angle and pivot point from user and rotate the object about pivot point. c. if the choice is 3 then get rotation angle from user and rotate the object about origin. d. if the choice is 4 then get scaling factor and fixed point from user and scale the object from fixed point. e. if the choice is 5 then get shear value and fixed point from user and shear the object from fixed point. f. If the choice is 6 then reflect the object. g. if the choice is 7 then exit the program. STEP 7: Display the transformed object.

PROGRAM:#include<stdio.h> #include<conio.h> #include<graphics.h>

Page 18: Cg Lab Manual

#include<dos.h> #include<math.h> #include<stdlib.h> void menu(); void input(); void output(); void translation(); void rotation(); void originrotation(); void scaling(); void shearing(); void reflection(); int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y; float sx,sy; void menu() { printf("-----------------------------------\n"); printf("\tMENU\n"); printf("\t1-Translation\n"); printf("\t2-Rotation\n"); printf("\t3-Rotation about origen\n"); printf("\t4-Scaling\n"); printf("\t5-Shearing\n"); printf("\t6-Reflection\n"); printf("\t7-Exit\n"); printf("-----------------------------------\n"); printf("Enter your choice:"); scanf("%d",&option); switch(option) { case 1: input(); translation(); break; case 2:

Page 19: Cg Lab Manual

input(); rotation(); break; case 3: input(); originrotation(); break; case 4: input(); scaling(); break; case 5: input(); shearing(); break; case 6: input(); reflection(); break; case 7: exit(0); break; } } void input() { printf("Enter the number of vertices:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the coordinates:"); scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]); } } void output() { cleardevice(); for(i=0;i<n;i++) { line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);

Page 20: Cg Lab Manual

} } void translation() { output(); printf("Enter the transformation vertex tx,ty:\n"); scanf("%d%d",&tx,&ty); for(i=0;i<=n;i++) { a[i][0]=a[i][0]+tx; a[i][1]=a[i][1]+ty; } output(); delay(2); menu(); } void rotation() { output(); printf("Enter the rotation angle:"); scanf("%d",&y); printf("Enter the pivot point:"); scanf("%d%d",&fx,&fy); k=(y*3.14)/180; for(i=0;i<=n;i++) { a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k); a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k); } output(); delay(2); menu(); } void originrotation() { output(); printf("Enter the rotation angle:"); scanf("%d",&y); k=(y*3.14)/180; for(i=0;i<=n;i++)

Page 21: Cg Lab Manual

{ a[i][0]=a[i][0]*cos(k)-a[i][1]*sin(k); a[i][1]=a[i][0]*sin(k)-a[i][1]*cos(k); } output(); } void scaling() { output(); printf("Enter the scaling factor:\n"); scanf("%f%f",&sx,&sy); printf("Enter the fixed piont:"); scanf("%d%d",&fx,&fy); for(i=0;i<=n;i++) { a[i][0]=a[i][0]*sx+fy*(1-sx); a[i][1]=a[i][1]*sy+fy*(1-sy); } output(); delay(2); menu(); } void shearing() { output(); printf("Enter the shear value:"); scanf("%d",&sh); printf("Enter the fixed piont:"); scanf("%d%d",&fx,&fy); printf("Enter the axis for shearing if x-axis then 1 if y-axis then 0:"); scanf("%d",&axis); for(i=0;i<=n;i++) { if(axis==1) { a[i][0]=a[i][0]+sh*(a[i][1]-fy); } else { a[i][1]=a[i][1]+sh*(a[i][0]-fx);

Page 22: Cg Lab Manual

} } output(); delay(2); menu(); } void reflection() { for(i=0;i<=n;i++) { temp=a[i][0]; a[i][0]=a[i][1]; a[i][1]=temp; } output(); } void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\TC\\BGI\\"); menu(); getch(); }

INPUT: ---------------------------------------------------------- MENU 1-Translation 2-Rotation 3-Rotation about origin 4-Scaling 5-Shearing 6-Reflection 7-Exit Enter your choice: 1 Enter the number of vertices: 3 Enter the coordinates: 30 150 10 200 Enter the coordinates: 10 200 60 200 Enter the coordinates: 60 200 30 150

RESULT: Thus the above program for 2D transformation was successfully executed.

Page 23: Cg Lab Manual

EX.NO: 4 COMPOSITE 2D TRANSFORMATION DATE:

AIM: To write a c program for composite 2D transformation.

ALGORITHM:

STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: Display a Menu and get choice from user. STEP 4: get the object (triangle) coordinates. STEP 5: Display the original object position. STEP 6: a. if the choice is 1 then get translation vertex from user and translate the object to given vertex. b. if the choice is 2 then get rotation angle and pivot point from user and rotate the object about pivot point. c. if the choice is 3 then get rotation angle from user and rotate the object about origin. d. if the choice is 4 then get scaling factor and fixed point from user and scale the object from fixed point. e. if the choice is 5 then get shear value and fixed point from user and shear the object from fixed point. f. If the choice is 6 then reflect the object. g. if the choice is 7 then exit the program. STEP 7: Display the transformed object.

Page 24: Cg Lab Manual

PROGRAM:

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> #include<stdlib.h> void menu(); void input(); void output(); void translation(); void rotation(); void originrotation(); void scaling(); void shearing(); void reflection(); int a[10][2],i,x,option,temp,angle,tx,ty,tx1,ty1,tx2,ty2,fx,fy,sh,k,n,axis,y,y1,y2,k1,k2,sh1,sh2; float sx,sy,sx1,sy1,sx2,sy2; void menu() { printf("-----------------------------------\n"); printf("\tMENU\n"); printf("\t1-Translation\n"); printf("\t2-Rotation\n"); printf("\t3-Rotation about origen\n"); printf("\t4-Scaling\n"); printf("\t5-Shearing\n"); printf("\t6-Reflection\n"); printf("\t7-Exit\n"); printf("-----------------------------------\n"); printf("Enter your choice:"); scanf("%d",&option); switch(option) { case 1: input(); translation();

Page 25: Cg Lab Manual

break; case 2: input(); rotation(); break; case 3: input(); originrotation(); break; case 4: input(); scaling(); break; case 5: input(); shearing(); break; case 6: input(); reflection(); break; case 7: exit(0); break; } } void input() { printf("Enter the number of vertices:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the coordinates:"); scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]); } } void output() { cleardevice(); for(i=0;i<n;i++)

Page 26: Cg Lab Manual

{ line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); } } void translation() { output(); printf("Enter the transformation vertex tx1,ty1 and tx2,ty2:\n"); scanf("%d%d%d%d",&tx1,&ty1,&tx2,&ty2); tx=tx1+tx2; ty=ty1+ty2; for(i=0;i<=n;i++) { a[i][0]=a[i][0]+tx; a[i][1]=a[i][1]+ty; } output(); } void rotation() { output(); printf("Enter the rotation angle:"); scanf("%d%d",&y1,&y2); printf("Enter the pivot point:"); scanf("%d%d",&fx,&fy); k1=(y1*3.14)/180; k2=(y2*3.14)/180; k=k1*k2; for(i=0;i<=n;i++) { a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k); a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k); } output(); } void originrotation() { output(); printf("Enter the rotation angle:"); scanf("%d",&y1,&y2);

Page 27: Cg Lab Manual

k1=(y1*3.14)/180; k2=(y2*3.14)/180; k=k1*k2; for(i=0;i<=n;i++) { a[i][0]=a[i][0]*cos(k)-a[i][1]*sin(k); a[i][1]=a[i][0]*sin(k)-a[i][1]*cos(k); } output(); } void scaling() { output(); printf("Enter the scaling factor:\n"); scanf("%f%f%f%f",&sx1,&sy1,&sx2,&sy2); printf("Enter the fixed piont:"); scanf("%d%d",&fx,&fy); sx=sx1*sx2; sy=sy1*sy2; for(i=0;i<=n;i++) { a[i][0]=a[i][0]*sx+fy*(1-sx); a[i][1]=a[i][1]*sy+fy*(1-sy); } output(); } void shearing() { output(); printf("Enter the shear value:"); scanf("%d%d",&sh1,&sh2); printf("Enter the fixed piont:"); scanf("%d%d",&fx,&fy); printf("Enter the axis for shearing if x-axis then 1 if y-axis then 0:"); scanf("%d",&axis); sh=sh1*sh2; for(i=0;i<=n;i++) { if(axis==1) {

Page 28: Cg Lab Manual

a[i][0]=a[i][0]+sh*(a[i][1]-fy); } else { a[i][1]=a[i][1]+sh*(a[i][0]-fx); } } output(); } void reflection() { for(i=0;i<=n;i++) { temp=a[i][0]; a[i][0]=a[i][1]; a[i][1]=temp; } output(); } void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\TC\\BGI\\"); menu(); getch(); }

RESULT: Thus the above program for composite 2D transformation was successfully executed.

Page 29: Cg Lab Manual

EX.NO:5 COHEN-SUTHERLAND CLIPPING& WINDOWINGDATE:

Aim:       To implement Cohen-Sutherland clipping& WindowingAlgorithm.

Functions used:

Line()      The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:        line (x1,y1,x2,y2)

initgraph().       This function takes thee arguments and they are          i).the video driver to be used (gd).         ii).the graphics mode (gm).         iii).the path name.

Syntax:       Initgraph(gd,gm,path)

Setcolor().       This function changes the drawing colour.

Syntax:      Setcolor(value of the color)

Settextstyle().The function settextstyle() is used to change the style of the text.

Syntax:Settextstyle(font,direction,charsize) 

Where font is the constant value or the font filename, direction is the number either 0 or 1, which makes the output to display in horizontal, or in vertical direction, charsize is the character size or magnification factor and it varies from 1 to 10.

Outtext().This function display a text message on upper left of the screen

Syntax:    Outtext(“message”);

Page 30: Cg Lab Manual

Algorithm:

Step 1. Create a class sulc with functions drawwindow, drawline, setcode, visibility and             reset endpoint.Step 2. Using the function line set the parameters to draw window.Step 3. Using the function defined in class sulc, setcode is used to save the line inside the             window and to the line outside the window.Step 4. Using the function visibility            i).check the code to know the points inside or outside the window.            ii).if the code value is zero the point is inside the window.Step 5. Using the function reset end point            i). if the code value for the line is outside the window.            ii).reset the endpoint to the boundary of the window.Step 6. Initialize the graphics functionsStep 7. Declare the variables x1, x2, y1, y2 of array type.Step 8. Get the value of two endpoints x1, y1 and x2, y2 to draw the line.Step 9. Using the object c, display the window before clipping.Step 10. Using the function setcode, visibility display the clipped window only with lines                inside the window class was displayed after clipping.

Page 31: Cg Lab Manual

Program:#include<iostream.h>#include<graphics.h>#include<conio.h>#include<stdlib.h>typedef struct coord{int x,y;char code[4];}pt;class sulc{public:void drawwindow();void drawline(pt p1,pt p2,int c1);pt setcode(pt p);int visibility(pt p1,pt p2);pt resetendpt(pt p1,pt p2);};void sulc::drawwindow(){setcolor(WHITE);line(150,100,450,100);line(450,100,450,350);line(450,350,150,350);line(150,350,150,100);}void sulc::drawline(pt p1,pt p2,int c1){setcolor(c1);line(p1.x,p1.y,p2.x,p2.y);}pt sulc::setcode(pt p){pt ptemp;if(p.y<100)ptemp.code[0]='1';elseptemp.code[0]='0'; if(p.y>350)ptemp.code[1]='1';elseptemp.code[1]='0';if(p.y>450)ptemp.code[2]='1';else

Page 32: Cg Lab Manual

ptemp.code[2]='0';if(p.y<150)ptemp.code[3]='1';elseptemp.code[3]='0';ptemp.x=p.x;ptemp.y=p.y;return(ptemp);}int sulc::visibility(pt p1,pt p2){int i,flag=0;for(i=0;i<4;i++){if((p1.code[i]!='0')||(p2.code[i]!='0'))flag=1;}if(flag==0)return(0);for(i=0;i<4;i++){if((p1.code[i]==p2.code[i])&&(p1.code[i]=='1'))flag=0;}if(flag==0)return(1);return(2);}pt sulc::resetendpt(pt p1,pt p2){pt temp;int x,y,i;float m,k;if(p1.code[3]=='1')x=150;if(p1.code[2]=='1')x=450;if((p1.code[3]=='1')||(p1.code[2]=='1')){m=(float)(p2.y-p1.y)/(p2.x-p1.x);k=(p1.y+(m*(x-p1.x)));temp.y=k;temp.x=x;for(i=0;i<4;i++)temp.code[i]=p1.code[i];if(temp.y<=350 &&temp.y>=100)return(temp);

Page 33: Cg Lab Manual

}if(p1.code[0]=='1')y=100;if(p1.code[1]=='1')y=350;if((p1.code[0]=='1')||(p1.code[1]=='1')){m=(float)(p2.y-p1.y)/(p2.x-p1.x);k=(float)p1.x+(float)(y-p1.y)/m;temp.x=k;temp.y=y;for(i=0;i<4;i++)temp.code[i]=p1.code[i];if(temp.y<=350 &&temp.y>=100)return(temp);}elsereturn(p1);}void main(){int gd=DETECT,gm,v;sulc c1;pt p1,p2,ptemp;initgraph(&gd,&gm,"");int x1[10],y1[10],x2[10],y2[10];cleardevice();int i,n;settextstyle(4,0,4);outtext("cohen sutherland line clipping");cout<<"\n\n enter the no.of lines:";cin>>n;for(i=0;i<n;i++){cout<<"\n\n enter end-point1(x1,y1):";cin>>x1[i]>>y1[i];cout<<"\n\n enter end-point2(x2,y2):";cin>>x2[i]>>y2[i];}cleardevice();settextstyle(0,0,3);outtext("before clipping");c1.drawwindow();for(i=0;i<n;i++){p1.x=x1[i];

Page 34: Cg Lab Manual

p1.y=y1[i];p2.x=x2[i];p2.y=y2[i];c1.drawline(p1,p2,15);}getch();cleardevice();settextstyle(0,0,3);outtext("after clipping");for(i=0;i<n;i++){p2.x=x2[i];p2.y=y2[i];p1=c1.setcode(p1);p2=c1.setcode(p2);v=c1.visibility(p1,p2);switch(v){case 0:c1.drawwindow();c1.drawline(p1,p2,15);break;case 1:c1.drawwindow();break;case 2:p1=c1.resetendpt(p1,p2);p2=c1.resetendpt(p2,p1);c1.drawwindow();c1.drawline(p1,p2,15);break;}}getch();closegraph();}

Input:

Enter the no.of lines:    1Enter end-point1(x1,y1):30 40 Enter end-point1(x2,y2):300 400

Result:Thus the program to implement Cohen-Sutherland clipping& Windowing was  executed

successfully

Page 35: Cg Lab Manual

EX NO: 6 COHEN SUTHERLAND 2D LINE CLIPPING AND WINDOWINGDATE:

Aim:       To implement Sutherland – Hodgeman Polygon clipping Algorithm

Functions used:Line()      The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:        line (x1,y1,x2,y2)

initgraph().       This function takes thee arguments and they are          i).the video driver to be used (gd).         ii).the graphics mode (gm).         iii).the path name.

Syntax:       Initgraph(gd,gm,path)

Setcolor().       This function changes the drawing colour.

Syntax:      Setcolor(value of the color)

Settextstyle().The function settextstyle() is used to change the style of the text.

Syntax:Settextstyle(font,direction,charsize) 

Where font is the constant value or the font filename, direction is the number either 0 or 1, which makes the output to display in horizontal, or in vertical direction, charsize is the character size or magnification factor and it varies from 1 to 10.

Outtext().This function display a text message on upper left of the screen

Syntax:    Outtext(“message”);

Page 36: Cg Lab Manual

Algorithm:

Step 1. Create a class sulc with functions drawwindow, drawline, setcode, visibility and             reset endpoint.Step 2. Using the function line set the parameters to draw window.Step 3. Using the function defined in class sulc, setcode is used to save the line inside the             window and to the line outside the window.Step 4. Using the function visibility            i).check the code to know the points inside or outside the window.            ii).if the code value is zero the point is inside the window.Step 5. Using the function reset end point            i). if the code value for the line is outside the window.            ii).reset the endpoint to the boundary of the window.Step 6. Initialize the graphics functionsStep 7. Declare the variables x1, x2, y1, y2 of array type.Step 8. Get the value of two endpoints x1, y1 and x2, y2 to draw the line.Step 9. Using the object c, display the window before clipping.Step 10. Using the function setcode, visibility display the clipped window only with lines                inside the window class was displayed after clipping.

Page 37: Cg Lab Manual

PROGRAM:#include<stdio.h>#include<conio.h>#include<math.h>#include<graphics.h>#include<dos.h>#include<process.h>int pixels[2][4];float xn1,xn2,yn1,yn2,x3,y3,m;int xmin,ymin,xmax,ymax,x1,y1,x2,y2;int choice,ed[20],num;void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax){int i,j,f1;for(i=0;i<2;i++)for(j=0;j<4;j++)pixels[i][j]=0;if(y1>ymax)pixels[0][0]=1;if(y1<ymin)pixels[0][1]=1;if(x1>xmax)pixels[0][2]=1;if(x1<xmin)pixels[0][3]=1;if(y2>ymax)pixels[1][0]=1;if(y2<ymin)pixels[1][1]=1;if(x2>xmax)pixels[1][2]=1;if(x2<xmin)pixels[1][3]=1;for(j=0;j<4;j++){if(pixels[0][j]==0&&pixels[1][j]==0)continue;if(pixels[0][j]==1&&pixels[1][j]==1){f1=3;break;

Page 38: Cg Lab Manual

}f1=2;}switch(f1){case 1:line(320+x1,240-y1,320+x2,240-y2);break;case 3:printf(“\n\n\t\”LINE IS NOT VISIBLE..\”");break;case 2:m=(y2-y1)/(x2-x1);xn1=x1;yn1=y1;xn2=x2;yn2=y2;if(pixels[0][0]==1){xn1=x1+(ymax-y1)/m;yn1=ymax;3}if(pixels[0][1]==1){xn1=x1+(ymin-y1)/m;yn1=ymin;}if(pixels[0][2]==1){yn1=y1+(xmax-x1)*m;xn1=xmax;}if(pixels[0][3]==1){yn1=y1+(xmin-x1)*m;xn1=xmin;}if(pixels[1][0]==1){xn2=x2+(ymax-y2)/m;yn2=ymax;}if(pixels[1][1]==1){

Page 39: Cg Lab Manual

xn2=x2+(ymin-y2)/m;yn2=ymin;}if(pixels[1][2]==1){yn2=y2+(xmax-x2)*m;xn2=xmax;}if(pixels[1][3]==1){yn2=y2+(xmin-x2)*m;xn2=xmin;}line(320+xn1,240-yn1,320+xn2,240-yn2);break;}}void cohen(){clearviewport();line(320+xmin,240-ymin,320+xmin,240-ymax);line(320+xmin,240-ymax,320+xmax,240-ymax);line(320+xmax,240-ymax,320+xmax,240-ymin);line(320+xmax,240-ymin,320+xmin,240-ymin);line(320+x1,240-y1,320+x2,240-y2);getch();cleardevice();line(320+xmin,240-ymin,320+xmin,240-ymax);line(320+xmin,240-ymax,320+xmax,240-ymax);line(320+xmax,240-ymax,320+xmax,240-ymin);line(320+xmax,240-ymin,320+xmin,240-ymin);su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);getch();}void main(){int gd=DETECT,gm,i,j;initgraph(&gd,&gm,”..\\bgi”);printf(“\n\n\t\t\”Enter the coordinate of the Clipping Window\”");printf(“\n\n\t\t\”Enter X(min),Y(min)\”:=”);scanf(“%d%d”,&xmin,&ymin);

Page 40: Cg Lab Manual

printf(“\n\n\t\t\”Enter X(max),Y(max)\”:=”);scanf(“%d%d”,&xmax,&ymax);printf(“\n\n\t\tEnter the coordinates of the Line”);printf(“\n\n\t\t Enter X(1) & Y(1):”);scanf(“%d%d”,&x1,&y1);printf(“\n\n\t\t Enter X(2) & Y(2):”);scanf(“%d%d”,&x2,&y2);clrscr();cohen();}

Result

                              Sutherland – Hodgeman Polygon clipping Algorithm was executed successfully.

EX.NO: 7 THREE DIMENSIONAL(3D) TRANSFORMATIONSDATE:Aim:

Page 41: Cg Lab Manual

To perform 3D transformations such as translation, rotation and scaling.

ALGORITHM:

STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: display the original object. STEP 4: get translation vertex from user and translate the object to given vertex. STEP 5: display the translated object. STEP 6: get scaling factors from user and scale the object by given values. STEP 7: display the scaled object. STEP 8: get rotation angle from user and rotate the object about x-axis,y-axis and z-axis. STEP 9: display the rotated object.

PROGRAM:#include<stdio.h> #include<conio.h>

Page 42: Cg Lab Manual

#include<graphics.h> #include<math.h> int maxx,maxy,midx,midy; void axis() { getch(); cleardevice(); line(midx,0,midx,maxy); line(0,midy,maxx,midy); } void main() { int gd,gm,x,y,z,o,x1,x2,y1,y2; float a,b,c; detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI\\"); setfillstyle(0,getmaxcolor()); maxx=getmaxx(); maxy=getmaxy(); midx=maxx/2; midy=maxy/2; axis(); bar3d(midx+50,midy-100,midx+60,midy-90,5,1); printf("Enter Translation Factor:"); scanf("%d%d%d",&x,&y,&z); axis(); printf("After translation"); bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1); axis(); bar3d(midx+50,midy+100,midx+60,midy-90,5,1); printf("Enter Scaling Factor:"); scanf("%f%f%f",&a,&b,&c); axis(); printf("After Scaling"); bar3d(midx+(a*50),midy-(b*100),midx+(a*60),midy-(b*90),5*c,1);

axis();bar3d(midx+50,midy-100,midx+60,midy-90,5,1); printf("Enter Rotating Angle:"); scanf("%d",&o); x1=50*cos(o*3.14/180)-100*sin(o*3.14/180); y1=50*cos(o*3.14/180)+100*sin(o*3.14/180); x2=60*sin(o*3.14/180)-90*cos(o*3.14/180); y2=60*sin(o*3.14/180)+90*cos(o*3.14/180); axis(); printf("After Rotation about Z Axis"); bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);

Page 43: Cg Lab Manual

axis(); printf("After Rotation about X Axis"); bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1); axis(); printf("\nAfter Rotation about Y Axis"); bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1); getch(); closegraph();

}

RESULT: Thus the above program for 3D transformation was successfully executed.

EX.NO: 8 COMPOSITE 3D TRANSFORMATIONS

DATE:

Page 44: Cg Lab Manual

AIM: To write a c program for composite 3D transformation.

ALGORITHM:

STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: display the original object. STEP 4: get translation vertex from user and translate the object to given vertex. STEP 5: display the translated object. STEP 6: get scaling factors from user and scale the object by given values. STEP 7: display the scaled object. STEP 8: get rotation angle from user and rotate the object about x-axis,y-axis and z-axis. STEP 9: display the rotated object.

PROGRAM:#include<stdio.h> #include<conio.h> #include<graphics.h>

Page 45: Cg Lab Manual

#include<math.h> int maxx,maxy,midx,midy; void axis() { getch(); cleardevice(); line(midx,0,midx,maxy); line(0,midy,maxx,midy); } void main() { int gd,gm,x,y,z,tx1,ty1,tz1,tx2,ty2,tz2,o,x1,x2,y1,y2,o1,o2; float a,b,c,sa,sb,sc,sa1,sb1,sc1; detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI\\"); setfillstyle(0,getmaxcolor()); maxx=getmaxx(); maxy=getmaxy(); midx=maxx/2; midy=maxy/2; axis(); bar3d(midx+50,midy-100,midx+60,midy-90,5,1); printf("Enter Translation Factor:"); scanf("%d%d%d%d%d%d",&tx1,&ty1,&tz1,&tx2,&ty2,&tz2); axis(); x=tx1+tx2; y=ty1+ty2; //z=tx1+tz2; printf("After translation"); bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1); axis(); bar3d(midx+50,midy+100,midx+60,midy-90,5,1); printf("Enter Scaling Factor:"); scanf("%f%f%f%f%f%f",&sa,&sb,&sc,&sa1,&sb1,&sc1); axis();

Page 46: Cg Lab Manual

a=sa+sa1; b=sb+sb1; c=sc+sb1; printf("After Scaling"); bar3d(midx+(a*50),midy-(b*100),midx+(a*60),midy-(b*90),5*c,1); axis(); bar3d(midx+50,midy-100,midx+60,midy-90,5,1); printf("Enter Rotating Angle:"); scanf("%d%d",&o1,&o2); o=o1+o2; x1=50*cos(o*3.14/180)-100*sin(o*3.14/180); y1=50*cos(o*3.14/180)+100*sin(o*3.14/180); x2=60*sin(o*3.14/180)-90*cos(o*3.14/180); y2=60*sin(o*3.14/180)+90*cos(o*3.14/180); axis(); printf("After Rotation about Z Axis"); bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1); axis(); printf("After Rotation about X Axis"); bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1); axis(); printf("\nAfter Rotation about Y Axis"); bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1); getch(); closegraph(); }

RESULT: Thus the above program for composite 3D transformation was successfully executed.

Page 47: Cg Lab Manual

EX.NO: DRAWING THREE DIMENSIONAL OBJECTS AND SCENESDATE:Aim:To write program to visualize the Drawing three dimensional objects and Scenes

Functions used:

Line()      The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:        line (x1,y1,x2,y2)

initgraph().       This function takes thee arguments and they are          i).the video driver to be used (gd).         ii).the graphics mode (gm).         iii).the path name.Syntax:       Initgraph(gd,gm,path)

Algorithm:Step 1. Create a class parallel.Step 2. Create a constructor parallel    i).initialize graphicsStep 3. Create a function initialize     i).draw the x,y,z axisStep 4. Create a function projection      i).get the reference angle alpha as j.      ii).assign the value of k as 45.      iii).compute the following

 fi=(3.14/180)*k;            a1=(3.14/180)*j;            z=pz1[1];

i=z/tan(a1);i1=floor(i*cos(fi));i2=floor(i*sin(fi));

      iv).Calculatepx3[1]=px1[1]+i1;

py3[1]=py1[1]+i2;px3[2]=px1[2]+i1;py3[2]=py1[2]+i2;px3[3]=px1[3]+i1;py3[3]=py1[3]+i2;px3[4]=px1[4]+i1;py3[4]=py1[4]+i2;

       v).compute the following

Page 48: Cg Lab Manual

z=pz1[5];i=z/tan(a1);i1=floor(i*cos(fi));i2=floor(i*sin(fi));

       vi). calculatepx3[5]=px1[5]+i1;py3[5]=py1[5]+i2;px3[6]=px1[6]+i1;py3[6]=py1[6]+i2;px3[7]=px1[7]+i1;py3[7]=py1[7]+i2;px3[8]=px1[8]+i1;py3[8]=py1[8]+i2;

     vii).compute the values to screen coordinate value.     viii).join the coordinate using line function     ix).display the projected object.

Page 49: Cg Lab Manual

Program#include<iostream.h>#include<graphics.h>#include<conio.h>#include<math.h>#include<string.h>#include<process.h>class parallel{public:int a,k;int gd,gm;int px[8],py[8],pz[8],px1[8],py1[8],pz1[8],px3[8],py3[8];parallel();void initialize();void drawobj();void proj(int);};parallel::parallel(){gd=DETECT;initgraph(&gd,&gm,"");}void parallel::initialize(){px1[1]=100,py1[1]=100,pz1[1]=0;px1[2]=200,py1[2]=100,pz1[2]=0;px1[3]=200,py1[3]=200,pz1[3]=0;px1[4]=100,py1[4]=200,pz1[4]=0;px1[5]=100,py1[5]=100,pz1[5]=100;px1[6]=200,py1[6]=100,pz1[6]=100;px1[7]=200,py1[7]=200,pz1[7]=100;px1[8]=100,py1[8]=200,pz1[8]=100;}void parallel::drawobj(){setcolor(WHITE);line(px1[1],py1[1],px1[2],py1[2]);line(px1[2],py1[2],px1[3],py1[3]);line(px1[3],py1[3],px1[4],py1[4]);line(px1[4],py1[4],px1[8]+50,py1[8]+50);line(px1[8]+50,py1[8]+50,px1[5]+50,py1[5]+50);line(px1[5]+50,py1[5]+50,px1[6]+50,py1[6]+50);line(px1[6]+50,py1[6]+50,px1[7]+50,py1[7]+50);line(px1[7]+50,py1[7]+50,px1[8]+50,py1[8]+50);line(px1[1],py1[1],px1[5]+50,py1[5]+50);

Page 50: Cg Lab Manual

line(px1[2],py1[2],px1[6]+50,py1[6]+50);line(px1[3],py1[3],px1[7]+50,py1[7]+50);line(px1[4],py1[4],px1[1],py1[1]);getch();}void parallel::proj(int j){cleardevice();int z,xp,yp;int dx,dy;float a1,fi,i,i1,i2;k=45;fi=(3.14/180)*k;a1=(3.14/180)*j;z=pz1[1];i=z/tan(a1);i1=floor(i*cos(fi));i2=floor(i*sin(fi));px3[1]=px1[1]+i1;py3[1]=py1[1]+i2;px3[2]=px1[2]+i1;py3[2]=py1[2]+i2;px3[3]=px1[3]+i1;py3[3]=py1[3]+i2;px3[4]=px1[4]+i1;py3[4]=py1[4]+i2;z=pz1[5];i=z/tan(a1);i1=floor(i*cos(fi));i2=floor(i*sin(fi));px3[5]=px1[5]+i1;py3[5]=py1[5]+i2;px3[6]=px1[6]+i1;py3[6]=py1[6]+i2;px3[7]=px1[7]+i1;py3[7]=py1[7]+i2;px3[8]=px1[8]+i1;py3[8]=py1[8]+i2;cout<<"enter the projected object";line(px3[1],py3[1],px3[2],py3[2]);line(px3[2],py3[2],px3[3],py3[3]);line(px3[3],py3[3],px3[4],py3[4]);line(px3[4],py3[4],px3[1],py3[1]);line(px3[5],py3[5],px3[6],py3[6]);line(px3[6],py3[6],px3[7],py3[7]);line(px3[7],py3[7],px3[8],py3[8]);

Page 51: Cg Lab Manual

line(px3[8],py3[8],px3[5],py3[5]);line(px3[1],py3[1],px3[5],py3[5]);line(px3[2],py3[2],px3[6],py3[6]);line(px3[3],py3[3],px3[7],py3[7]);line(px3[4],py3[4],px3[8],py3[8]);}void main(){parallel p;int a;char c='y';clrscr();cleardevice();while(c=='y'||c=='y'){cleardevice();p.initialize();p.drawobj();cout<<"enter the reference angle\ny";cin>>a;p.proj(a);cout<<"do u want to continue(y/n)";cin>>c;}cleardevice();}

Result:Thus the program to visualize the Drawing three dimensional objects and Scenes was

executed successfully.

Page 52: Cg Lab Manual

EX.NO:10 FRACTAL IMAGESDATE:

Aim:

 To write program to draw the Generating Fractal images

Functions used:Line()      The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:        line (x1,y1,x2,y2)

initgraph().       This function takes thee arguments and they are          i).the video driver to be used (gd).         ii).the graphics mode (gm).         iii).the path name.

Syntax:       Initgraph(gd,gm,path)

Algorithm:Step1:Pres()          i).Initialise graphics.Step 2: Initialize()           i).Draw x,y,z axis           ii).Number all axis with regular interval 1 to 8step 3:Draw-obj           i). Get the value of each p6 of 3D object           ii).Convert the value to screen coordinate values           iii).Join the coordinate using the function.Step 4:Proj()           i).Get the reference point in z-axis as j           ii).Get the position of the view plane as k           iii).Assign the values for Zprp=j and Zrp=k           iv).Compute

   z=pz1[1];   u=(zprp-zvp)/(zprp-z);v).Calculate

                        px3[1]=px1[1]*u;py3[1]=py1[1]*u;px3[2]=px1[2]*u;py3[2]=py1[2]*u;

Page 53: Cg Lab Manual

px3[3]=px1[3]*u;py3[3]=py1[3]*u;px3[4]=px1[4]*u;py3[4]=py1[4]*u;vi).Computez=pz1[5];u=(zprp-zvp)/(zprp-z);

            vii).Calculatepx3[5]=px1[5]*u;py3[5]=py1[5]*u;px3[6]=px1[6]*u;py3[6]=py1[6]*u;px3[7]=px1[7]*u;py3[7]=py1[7]*u;px3[8]=px1[8]*u;py3[8]=py1[8]*u;

viii).Compute the values to screen coordinate value.ix).Join the coordinate using line function.

            x).Display the projected object.

Page 54: Cg Lab Manual

Program:#include<iostream.h>.#include<graphics.h>#include<conio.h>#include<math.h>#include<string.h>#include<process.h>class pres{public:int a,k;int gd,gm;int px[8],py[8],pz[8],px1[8],py1[8],pz3[8],px3[8],py3[8],pz1[8];pres();void initialize();void drawobj();void proj(int,int);};pres::pres(){gd=DETECT;initgraph(&gd,&gm,"");}void pres::initialize(){px1[1]=200,py1[1]=200,pz1[1]=100;px1[2]=300,py1[2]=200,pz1[2]=100;px1[3]=300,py1[3]=300,pz1[3]=100;px1[4]=200,py1[4]=300,pz1[4]=100;px1[5]=200,py1[5]=200,pz1[5]=200;px1[6]=300,py1[6]=200,pz1[6]=200;px1[7]=300,py1[7]=300,pz1[7]=200;px1[8]=200,py1[8]=300,pz1[8]=200;}void pres::drawobj(){setcolor(WHITE);line(px1[1],py1[1],px1[2],py1[2]);line(px1[2],py1[2],px1[3],py1[3]);line(px1[3],py1[3],px1[4],py1[4]);line(px1[4],py1[4],px1[8]+50,py1[8]+50);line(px1[8]+50,py1[8]+50,px1[5]+50,py1[5]+50);line(px1[5]+50,py1[5]+50,px1[6]+50,py1[6]+50);line(px1[6]+50,py1[6]+50,px1[7]+50,py1[7]+50);line(px1[7]+50,py1[7]+50,px1[8]+50,py1[8]+50);line(px1[1],py1[1],px1[5]+50,py1[5]+50);

Page 55: Cg Lab Manual

line(px1[2],py1[2],px1[6]+50,py1[6]+50);line(px1[3],py1[3],px1[7]+50,py1[7]+50);line(px1[4],py1[4],px1[1],py1[1]);getch();}void pres::proj(int j,int k){cleardevice();int z,xp,yp;float zprp,zvp,u;zprp=j;zvp=k;z=pz1[1];u=(zprp-zvp)/(zprp-z);z=pz1[1];px3[1]=px1[1]*u;py3[1]=py1[1]*u;px3[2]=px1[2]*u;py3[2]=py1[2]*u;px3[3]=px1[3]*u;py3[3]=py1[3]*u;px3[4]=px1[4]*u;py3[4]=py1[4]*u;z=pz1[5];u=(zprp-zvp)/(zprp-z);px3[5]=px1[5]*u;py3[5]=py1[5]*u;px3[6]=px1[6]*u;py3[6]=py1[6]*u;px3[7]=px1[7]*u;py3[7]=py1[7]*u;px3[8]=px1[8]*u;py3[8]=py1[8]*u;cleardevice();cout<<"enter the projected object";line(px3[1],py3[1],px3[2],py3[2]);line(px3[2],py3[2],px3[3],py3[3]);line(px3[3],py3[3],px3[4],py3[4]);line(px3[4],py3[4],px3[1],py3[1]);line(px3[5],py3[5],px3[6],py3[6]);line(px3[6],py3[6],px3[7],py3[7]);line(px3[7],py3[7],px3[8],py3[8]);line(px3[8],py3[8],px3[5],py3[5]);line(px3[1],py3[1],px3[5],py3[5]);line(px3[2],py3[2],px3[6],py3[6]);line(px3[3],py3[3],px3[7],py3[7]);line(px3[4],py3[4],px3[8],py3[8]);

Page 56: Cg Lab Manual

}void main(){pres p;int a,b;clrscr();cleardevice();cout<<"prespective projection"<<endl;p.initialize();p.drawobj();cout<<"enter the reference point inz-axis"<<endl;cin>>a;cout<<"enter the positionof view plane"<<endl;cin>>b;p.proj(a,b);getch();}

Output:Enter the values for h,s and r: 0.5 0.6 0.7   R=0.28   G=0.28   B=0.7

Result:Thus the program to draw the Generating Fractal images object was executed

successfully.