cg lab manual

84
Sutherland-Hodgeman polygon clipping algorithm Aim:- To write a C program to implement polygon clipping. Procedure Algorithm For Polygon Clipping 1. Get the minimum and maximum coordinates of both window and view port. 2. Get the number of sides of a polygon and its corresponding coordinates. 3. If the polygon lies within region code window, display it. 4. If any one of the polygon side is neither inside nor outside the boundary, find point of intersection and clip the regions that lies outside the boundary. 5. Display the polygon after clipping. Source Code Programming in C #include<stdio.h> #include<conio.h> #include<graphics.h> typedef enum { left,right,bottom,top } edge; #define N_EDGE 4 #define TRUE 1 #define FALSE 0 struct point { int x; int y; }p,wmin,wmax,p1,p2,ipt,i,pin[50],pout[50],first[50],s[50],i; int inside(struct point p,int b,struct point wmin,struct point wmax) { switch(b) { case left: if(p.x<wmin.x) return (FALSE);

Upload: spriyar

Post on 13-Sep-2014

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cg Lab Manual

Sutherland-Hodgeman polygon clipping algorithm

Aim:- To write a C program to implement polygon clipping.

Procedure Algorithm For Polygon Clipping 1. Get the minimum and maximum coordinates of both window and view port.2. Get the number of sides of a polygon and its corresponding coordinates.3. If the polygon lies within region code window, display it.4. If any one of the polygon side is neither inside nor outside the boundary, find point of intersection and clip the regions that lies outside the boundary.5. Display the polygon after clipping.

Source Code Programming in C #include<stdio.h>#include<conio.h>#include<graphics.h>typedef enum { left,right,bottom,top } edge;#define N_EDGE 4#define TRUE 1#define FALSE 0struct point{int x;int y;}p,wmin,wmax,p1,p2,ipt,i,pin[50],pout[50],first[50],s[50],i;int inside(struct point p,int b,struct point wmin,struct point wmax){switch(b){case left:if(p.x<wmin.x)return (FALSE);break;case right:if(p.x>wmax.x)return (FALSE);break;case bottom:if(p.y<wmin.y)return (FALSE);break;case top:if(p.y>wmax.y)return (FALSE);break;

Page 2: Cg Lab Manual

}return (TRUE);}int cross(struct point p1,struct point p2,int b,struct point wmin,struct point wmax){if(inside(p1,b,wmin,wmax)==inside(p2,b,wmin,wmax))return (FALSE);elsereturn (TRUE);}struct point intersect(struct point p1,struct point p2,int b,struct point wmin,struct point wmax){float m;if(p1.x!=p2.x)m=(p1.y-p2.y)/(p1.x-p2.x);switch(b){case left:ipt.x=wmin.x;ipt.y=p2.y+(wmin.x-p2.x)*m;break;case right:ipt.x=wmax.x;ipt.y=p2.y+(wmax.x-p2.x)*m;break;case bottom:ipt.y=wmin.y;if(p1.x!=p2.x)ipt.x=p2.x+(wmin.y-p2.y)/m;elseipt.x=p2.x;break;case top:ipt.y=wmax.y;if(p1.x!=p2.x)ipt.x=p2.x+(wmax.y-p2.y)/m;elseipt.x=p2.x;break;}return(ipt);}void clippoint(struct point p,int b,struct point wmin,struct point wmax,struct point *pout,int *cnt,struct point *first[],struct point *s){if(!first[b])

Page 3: Cg Lab Manual

first[b]=&p;elseif(cross(p,s[b],b,wmin,wmax)){ipt=intersect(p,s[b],b,wmin,wmax);if(b<top)clippoint(ipt,b+1,wmin,wmax,pout,cnt,first,s);else{pout[*cnt]=ipt;(*cnt)++;}}s[b]=p;if(inside(p,b,wmin,wmax))if(b<top)clippoint(p,b+1,wmin,wmax,pout,cnt,first,s);else{pout[*cnt]=p;(*cnt)++;}}void closeclip(struct point wmin,struct point wmax,struct point *pout,int *cnt,struct point *first[],struct point *s){int b;for(b=left;b<=top;b++){if(cross(s[b],*first[b],b,wmin,wmax)){i=intersect(s[b],*first[b],b,wmin,wmax);if(b<top)clippoint(i,b+1,wmin,wmax,pout,cnt,first,s);else{pout[*cnt]=i;(*cnt)++;}}}}int clippolygon(struct point wmin,struct point wmax,int n,struct point *pin,struct point *pout){struct point *first[N_EDGE]={0,0,0,0},s[N_EDGE];int i,cnt=0;

Page 4: Cg Lab Manual

for(i=0;i<n;i++)clippoint(pin[i],left,wmin,wmax,pout,&cnt,first,s);closeclip(wmin,wmax,pout,&cnt,first,s);return(cnt);}void main(){int c,gm,gr,n,j,np;clrscr();detectgraph(&gm,&gr);initgraph(&gm,&gr,"d:\\tc\\BGI");printf("Enter the window minimum coordinates");scanf("%d%d",&wmin.x,&wmin.y);printf("Enter the window max coordinates");scanf("%d%d",&wmax.x,&wmax.y);rectangle(wmin.x,wmax.y,wmax.x,wmin.y);printf("Enter the no of sides in polygon:\n");scanf("%d",&n);printf("Enter the coordinates(x,y)for pin ,pout:\n");for(j=0;j<n;j++){scanf("%d%d",&pin[j].x,&pin[j].y);scanf("%d%d",&pout[j].x,&pout[j].y);}detectgraph(&gm,&gr);initgraph(&gm,&gr,"d:\\tc\\BGI");for(j=0;j<n;j++){if(j!=n-1)line(pin[j].x,pin[j].y,pout[j].x,pout[j].y);elseline(pin[j].x,pin[j].y,pout[j].x,pout[j].y);}rectangle(wmin.x,wmax.y,wmax.x,wmin.y);printf("\n1.polygon clipping 2.exit");scanf("%d",&c);switch(c){case 1:detectgraph(&gm,&gr);initgraph(&gm,&gr,"d:\\tc\\BGI");rectangle(wmin.x,wmax.y,wmax.x,wmin.y);np=clippolygon(wmin,wmax,n,pin,pout);for(j=0;j<np;j++){line(pout[j].x,pout[j].y,pout[(j+1)].x,pout[(j+1)].y);

Page 5: Cg Lab Manual

}break;case 2:exit(0);}getch();}

Project Example Output Enter the window minimum coordinates200200Enter the window max coordinates400400Enter the no of sides in polygon3Enter the coordinates(x,y)for pin ,pout1503002501752501753504103504101503001.polygon clipping 2.exit RESULT Thus the c program to implement polygon clipping was coded and executed successfully.

Page 6: Cg Lab Manual

EX. NO.:-

DATE:-

Visualization of 3-Dimensional Images Algorithm

These Program is draw the three dimensional image using bard3d graphics function. using maxx=getmaxx();maxy=getmaxy(); it will get the center of the coordinate using these coordinate it will draw the three dimensional visualization imageSource code three Dimensional Image algorithm #include<stdio.h>#include<conio.h>#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;clrscr();detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:/tc/bgi");setfillstyle(0,getmaxcolor());maxx=getmaxx();maxy=getmaxy();midx=maxx/2;midy=maxy/2;bar3d(midx-250,midy+150,midx-150,midy+225,20,4);printf("\nEnter the Perspective Projection Vector");scanf("%d%d%d",&x,&y,&z);bar3d(midx-(x*5),midy-(y*9),midx-(x*3),midy-(y*5),10*z,4);getch();closegraph();}

Page 8: Cg Lab Manual

AIM :Write a program to implement Composite Transformation .

PROGRAM

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void main( )

{

int gd = DETECT , gm ;

int x1 , y1 , x4 , y4 , tx1 , ty1 , tx2 , ty2 ;

clrscr ( ) ;

initgraph (&gd , &gm ,”\\tc\\bgi”) ;

rectangle ( 250 , 250 , 250 , 250 ) ;

printf ( “ Enter the End Points”) ;

scanf ( “ %d %d”, &tx1 , &ty1 ) ;

x1 = 250 ; x4 = 300 ;

y1 = 250 ; y4 = 300 ;

x1 = x1 + tx1 ;

y1 = y1 + ty1 ;

x4 = x4 + tx1 ;

y4 = y4 + ty1 ;

Page 9: Cg Lab Manual

rectangle ( x1 , y1 , x4 , y4 ) ;

getch ( ) ;

printf ( “ Enter the value ” ) ;

scanf ( “ %d %d” , &tx2 , &ty2 ) ;

x1 = x1 + tx2 ; x4 = x4 + tx2 ;

y1 = y1 + ty2 ; y4 = y4 + ty2 ;

rectangle ( x1 , y1 , x4 , y4 ) ;

getch ( ) ;

closegraph ( ) ;

}

INPUT

Enter the End Points

90

80

OUTPUT

INPUT

Enter the value 30 40

OUTPUT

Page 10: Cg Lab Manual

Window to view-port mapping

Program:-

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>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 Co-ordinates 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);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)+0.5);x2=v1+floor(((float)(x2-w1)*sx)+0.5);x3=v1+floor(((float)(x3-w1)*sx)+0.5);y1=v2+floor(((float)(y1-w2)*sy)+0.5);y2=v2+floor(((float)(y2-w2)*sy)+0.5);y3=v2+floor(((float)(y3-w2)*sy)+0.5);

Page 11: Cg Lab Manual

line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);getch();return 0;}Output of the window View PortCS1355-Graphics and Multimedia Lab

Page 12: Cg Lab Manual

EX. NO. 1(A)DATE:

BRESENHAM’S LINE DRAWING

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

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: StartStep 2: Get the values of the end points as(x1, y1) &(x2, y2)Step 3: Assign x=x1, y=y1;Step 4: Compute dx=x2-x1Step 5: Compute dy=y2-y1Step 6: Assign sx=x2-x1, sy=y2-y1Step 7: If dy>dx then interchange the values of dx and dy and assign exch=1Step 8: Compute p=2xdy-dxStep 9: Put a pixel on(x,y)Step 10: If exch=1, y=sy else x=x+sxStep 11: If p>0 and exch =1, x=x+sx else y=y+sy, p=p-2xdxStep 12: Compute p=p+2xdyStep 13: Do steps (9) t0 (12) for dx timesStep 14: Stop

Page 13: Cg Lab Manual

PROGRAM:

#include<iostream.h>#include<graphics.h>#include<conio.h>#include<math.h> int signs(int m);void bres_line(int,int,int,int);void main(){int gd=DETECT,gm;int x1,x2,y1,y2;initgraph(&gd,&gm,"");cout<<"enter starting value of X-Axis----->";cin>>x1;cout<<"enter starting value of Y-Axis----->";cin>>y1;cout<<"enter ending value of X-Axis----->";cin>>x2;cout<<"enter ending value of y-Axis----->";cin>>y2;bres_line(x1,y1,x2,y2);getch();closegraph();}void bres_line(int x1,int y1,int x2,int y2){int x,y,sx,sy,p,temp,exch=0,i,dx,dy;x=x1;y=y1;dy=abs(y1-y2);dx=abs(x1-x2);sx=signs(x2-x1);sy=signs(y2-y1);if(dy>dx){temp=dx;dx=dy;dy=temp;exch=1;}p=2*dy-dx;for(i=0;i<=dx;i++){putpixel(x,y,15);

Page 14: Cg Lab Manual

if(exch==1)y+=sy;elsex+=sx;if(p>=0){if(exch==1)x+=sx;elsey+=sy;p=p-2*dx;}p=p+2*dy;}}int signs(int m){if(m<0)return(-1);else if(m>0)return(1);elsereturn(0);}

INPUT:

Enter starting value of X-Axis----->100Enter starting value of Y-Axis----->100Enter ending value of X-Axis----->200Enter ending value of Y-Axis----->200

OUTPUT:

RESULT: Thus the program is executed and verified.

Page 15: Cg Lab Manual

EX. NO. 1(B)DATE:

CIRCLE DRAWING

AIM: To write a program to draw a circle using Bresenham’s circle drawing Algorithm.

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:

Page 16: Cg Lab Manual

Step 1: Start

Step 2: Get the center point as (xc,yc),get the radius as r.

Step 3: Assign y=r,x=0

Step 4: Calculate p=3-2r

Step 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and y=y-1

Step 6: Increment x by 1

Step 7: Do steps (9) to (16)

Step 8: Repeat steps (5) to (9) until x<=y

Step 9: Put a pixel on (xc+x,yc+y,15);

Step 10: Put a pixel on (xc+x,yc-y,15);

Step 11: Put a pixel on (xc-x,yc+y,15);

Step 12: Put a pixel on (xc-x, yc-y, 15);

Step 13: Put a pixel on (xc+y,yc+x,15);

Step14: Put a pixel on (xc+y, yc-x, 15);

Step 15: Put a pixel on (xc-y, yc+x, 15);

Step 16: Put a pixel on (xc-y, yc-x, 15);

Step 17: Stop

PROGRAM:

Page 17: Cg Lab Manual

#include<iostream.h>#include<graphics.h>#include<conio.h>#include<math.h>void my_circle(int,int,int);void plot(int,int,int,int);void main(){int gd=DETECT,gm;int xc,yc,r;initgraph(&gd,&gm,"");cout<<"enter X-Axis----->";cin>>xc;cout<<"enter Y-Axis----->";cin>>yc;cout<<"enter radius----->";cin>>r;my_circle(xc,yc,r);getch();closegraph();}void my_circle(int xc,int yc,int r){int x,y,p;p=3-2*r;x=0;y=r;while(x<=y){if(p<0)p=p+(4*x+6);else{p=p+10+4*(x-y);y=y-1;}plot(xc,yc,x,y);x=x+1;}}void plot(int xc,int yc,int x,int y){putpixel(xc+x,yc+y,15);putpixel(xc+x,yc-y,15);putpixel(xc-x,yc+y,15);

Page 18: Cg Lab Manual

putpixel(xc-x,yc-y,15);putpixel(xc+y,yc+x,15);putpixel(xc+y,yc-x,15);putpixel(xc-y,yc+x,15);putpixel(xc-y,yc-x,15);}

INPUT:

Enter X-Axis----->:100Enter Y-Axis----->:200Enter radius----->: 40

OUTPUT: 

:

RESULT: Thus the program is executed and verified.

Page 19: Cg Lab Manual

EX. NO. 1(C)DATE:

MIDPOINT ELLIPSE DRAWING ALGORITHM

AIM

To write a C / C++ program to draw an ellipse using midpoint ellipse algorithm.

Algorithm

Step 1: Input radius rx, ry and ellipse center (Xc, Yc) and obtain the first point on the circumference of a

circle centered on the origin as (X0, Y0) = (0, ry)

Step 2: Calculate the initial values of the decision parameter in region 1 as

P10 = r2y – r2

x ry + 1/4 r2x

Step 3: At each Xk position in region 1, starting at k = 0, perform the following test:

If P1k < 0, the next point to plot is (Xk+1, Yk) and

P1k+1 = P1k+2 r2yXk+1 + r2

y

Otherwise the next point is (Xk+1, Yk-1) and

P1k+1 = P1k+2 r2yXk+1 - 2r2

xYk+1 + r2y

With

2 r2yXk+1=2 r2

yXk+ 2r2y

2r2xYk+1=2r2

xYk- 2r2x

Step 4: Calculate the initial values of the decision parameter in region 2 as

P20 = r2y(X0+1/2)2+ r2

x(Y0 – 1)2- r2x r2

y

Page 20: Cg Lab Manual

Step 5: At each position starting at Yk position in region 2, starting at k = 0,

perform the following test:

If P2k > 0, the next point to plot is (Xk, Yk-1) and

P2k+1 = P2k - 2 r2yYk+1 + r2

x

Otherwise the next point is (Xk+1, Yk-1) and

P2k+1 = P2k - 2 r2yXk+1 - 2r2

xYk+1 + r2x

Step 6: Determine symmetry points in the other three octants

Step 7: Move each pixel position(X, Y) onto the circular path centered on

(Xc, Yc) and plot the coordinate values as

X = X + Xc Y = Y + Yc

Step 8: Repeat steps for region 1 until 2 r2yX>=2 r2

xY

FUNCTIONS USED:

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)

Page 21: Cg Lab Manual

Putpixel ()

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

Syntax:

Putpixel(x,y,color)

ALGORITHM:

Step 1: Start

Step 2: Get the center point as(x1, y1)

Step 3: Get the length of semi-major, semi-minor axes as r1 & r2

Step 4: Calculate t=pi/180

Step 5: Initialise i=0;

Step 6: Compute d=i*t

Step 7: Compute x=x1+y1*sin(d), y=y1+r2*cos(d).

Step 8: Put a pixel on(x,y)

Step 9: Increment I by 1

Step 10: Repeat steps(6) to (9) until i<360

Step 11: Stop

PROGRAM:

#include<iostream.h>#include<graphics.h>#include<conio.h>#include<math.h>void main(){int gd=DETECT,gm,x1=0,y1=0,r1=0,r2=0;float x=0,y=0,t,d,i;initgraph(&gd,&gm,"");cout<<"enter the center co-or:";cin>>x1>>y1;cout<<"enter the radius1:";cin>>r1;cout<<"enter radius2:";cin>>r2;for(i=0;i<360;i++)

Page 22: Cg Lab Manual

{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();}

INPUT:

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

OUTPUT:

Page 23: Cg Lab Manual

RESULT: Thus the program is executed and verified.

Page 24: Cg Lab Manual

EX. NO. 2DATE:

IMPLEMENTATION OF LINE,CIRCLE & ELLIPSE ATTRIBUTES

Aim:

To write a C / C++ Program to display the various attributes of line, circle and ellipse.

Algorithm:

1. Start the program .

2. Initialize the variables.

3. Call the initgraph() function

4. Set color for the output primitives.

5. Using Outtextxy() display the chosen particular primitives.

6. Include the various attributes of line, circle and ellipse.

7. close the graph and run the program.

8. stop the program.

Page 25: Cg Lab Manual

Ex. No. 5DATE: COHEN-SUTHERLAND CLIPPING

AIM: To implement Cohen-Sutherland 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 26: 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 functions

Step 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 27: 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)

Page 28: Cg Lab Manual

ptemp.code[2]='1';elseptemp.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];

Page 29: Cg Lab Manual

if(temp.y<=350 &&temp.y>=100)return(temp);}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++)

Page 30: Cg Lab Manual

{p1.x=x1[i];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();}

Page 31: Cg Lab Manual

INPUT:

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

OUTPUT:

Before clipping 

After clipping

Page 32: Cg Lab Manual

RESULT: Thus the program is executed and verified.

Page 33: Cg Lab Manual

EX. NO. : 3DATE: 2D TRANSFORMATION

AIM: To perform the 2D transformation such as translation, rotation, scaling, shearing, Reflection

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: Declare the variables xa,ya,xa1,ya1 of array type.

Step2:Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.

Step3: Initialise the graphics function.

Step4: Input the number of points.

Step5: Input the value of co-ordinate according to number of points.

Step6. Using switch statement selects the option to perform translation, rotation, scaling, reflection and

shearing.

Step7: Translation:

a).input the translation vector

b).add the translation vectors with the coordinates

xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,

Page 34: Cg Lab Manual

c).using the function line,display the object before and after translation.

Step8: Rotation

a). input the rotation angle

b). using formula theta=(theta*3.14)/180

c).input the value of reference point

d). calculate new coordinate point using formula

xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),

ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),

e). using the function line,display the object before and after rotation.

Step9: Scaling:

a).input the scaling factor and reference point

b).calculate new coordinate point using formula

xa1[i]=(xa[i]*sx+rx*(1-sx),

ya1 [i] = (ya[i]*sy+ry*(1-sy)

c). using the function line, display the object before and after scaling.

Step10: Shearing:

a).input the shearing value and reference point.

b). input the shear direction x or y

i).if direction x

xa1[i]=xa[i]+shx*(ya[i]-yref)

ii).otherwise

ya1[i]=ya[i]+shy*(xa[i]-xref)

iii). using the function line, display the object before and after shearing.

Step11: Reflection:

a).display the object before reflection using the function line

b). display the object after reflection using the function line

Step12: Stop.

Page 35: Cg Lab Manual

PROGRAM:

#include<iostream.h>#include<conio.h>#include<math.h>#include<graphics.h>#include<stdlib.h>void main(){int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,sx,sy,shx,shy,xref,yref;char d;gd=DETECT;initgraph(&gd,&gm,"");cout<<"enter the no of points";cin>>n;for(i=0;i<n;i++){cout<<"enter the coordinates"<<i+1;cin>>xa[i]>>ya[i];}

do{cout<<"menu";cout<<"\n1.translation\n2.rotation\n3.scaling\n4.shearing\n5.reflection\n6.exit";cin>>op;switch(op){case 1:cout<<"enter the translation vector";cin>>tx>>ty;for(i=0;i<n;i++){xa1[i]=xa[i]+tx;ya1[i]=ya[i]+ty;}cout<<"before translation";for(i=0;i<n;i++){line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);} cout<<"after translation";for(i=0;i<n;i++){

Page 36: Cg Lab Manual

line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);}getch();cleardevice();break;case 2:cout<<"enter the rotation angle";cin>>theta;theta=(theta*3.14)/180;cout<<"enter the reference points";cin>>xf>>yf;for(i=0;i<n;i++){xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);}cout<<"before rotation";for(i=0;i<n;i++){line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);}

cout<<"after rotation";for(i=0;i<n;i++){line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);}getch();cleardevice();break;case 3:cout<<"enter the scaling factor";cin>>sx>>sy;cout<<"enter the reference point";cin>>rx>>ry;for(i=0;i<n;i++){xa1[i]=xa[i]*sx+rx*(1-sx);ya1[i]=ya[i]*sy+ry*(1-sy); }cout<<"before scaling";for(i=0;i<n;i++){line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);} cout<<"after scaling";

Page 37: Cg Lab Manual

for(i=0;i<n;i++){line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);}getch();cleardevice();break;case 4:cout<<"enter the shear value";cin>>shx>>shy;cout<<"enter the reference point";cin>>xref>>yref;cout<<"enter the shear direction x or y";cin>>d;if(d=='x'){for(i=0;i<n;i++){xa1[i]=xa[i]+shx*(ya[i]-yref);ya1[i]=ya[i]; }}cout<<"before shearing";for(i=0;i<n;i++){line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);}cout<<"after shearing";for(i=0;i<n;i++){line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);}getch();cleardevice();break;case 5:cout<<"before reflection";for(i=0;i<n;i++){line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);} cout<<"after reflection";for(i=0;i<n;i++){line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]);}

Page 38: Cg Lab Manual

getch();cleardevice();break;case 6:exit(0);break;}}while(op!=6);}

INPUT & OUTPUT:enter the no of points:3enter the coordinates 1:50 150enter the coordinates 2:50 50enter the coordinates 3:75 150

menu1. translation2. rotation3. scaling4.shearing5.reflection6.exit1enter the translation vector:30 40

Before translation After Translation

menu1. translation2. rotation3. scaling

Page 39: Cg Lab Manual

4.shearing5.reflection6.exit 2

enter the rotation angle:40

enter the reference points:100 100

before rotation after rotation

 

menu1. translation2. rotation3. scaling4.shearing5.reflection6.exit 3

Enter the scaling factor: 3 4Enter the reference points: 30 40

Before scaling after scaling

Page 40: Cg Lab Manual

menu1. translation2. rotation3. scaling4.shearing5.reflection6.exit 4

Enter the shear value: 3 4Enter the reference point: 20 30Enter the shear direction x or y: X

Before shearing After shearing

menu1. translation2. rotation3. scaling4.shearing5.reflection

Page 41: Cg Lab Manual

6.exit 5

Before reflection after reflection 

menu1. translation

2. rotation3. scaling4.shearing5.reflection6.exit 6

RESULT: Thus the program is executed and verified.

Page 42: Cg Lab Manual

EX. NO. 7DATE:

3D- TRANSFORMATION

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

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 cube with function draw cube.

Step 2: Use the function draw cube to draw a cube using eight points by means of

functions line.

Step 3: Declare the variables x1, y1, x2, y2, x3, y3, in array type which of data type int.

Step 4:Declare the variables theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.

Step 5: Initialize graphics functions.

Step 6: Input the first point in the cube.

Step 7: Input the size of the edge.

Step 8: Create an object to call the function.

Step 9: Using switch operation selects the operation to perform translation, rotation, scaling.

Step 10: Translation

a).input the translation vectortx,ty,tz.

b).calculate points using formula

Page 43: Cg Lab Manual

x3[i]=x1[i]+tx.

y3[i]=y1[i]+ty

z3[i]=z1[i]+tz.

x4[i]=x3[i]+z3[i]/2

y4[i]=y3[i]+z3[i]/2

c).using the function line, display the object before and after translation.

Step11: Rotation:

a). input the rotation angle

b). using formula theta=(theta*3.14)/180

c).input the direction in x,y,z axis

d). if the direction is along x axis,

x3[i]=x1[i].

y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),

y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),

if the direction is along yaxis,

y3[i]=y1[i].

z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),

x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),

if the direction is along z axis,

z3[i]=z1[i].

x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),

y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),

e).calculate the points using the formula

x4[i]=x3[i]+z3[i]/2

y4[i]=y3[i]+z3[i]/2

f). using the function line,display the object before and after rotation.

Step12: Scaling:

a).input the scaling factor and reference point

b).calculate coordinates point using formula

x3[i]=xf+(x1[i]*sx+xf*(1-sx),

y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)

Page 44: Cg Lab Manual

z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)

c). calculate the points using the formula

x4[i]=x3[i]+z3[i]/2

y4[i]=y3[i]+z3[i]/2

d). using the function line, display the object before and after scaling.

Step13: Stop.

PROGRAM:

#include<iostream.h>#include<graphics.h>#include<math.h>#include<conio.h>#include<stdlib.h>class cube{public:void drawcube(int x1[],int y1[]){int i;for(i=0;i<4;i++){if(i<3)line(x1[i],y1[i],x1[i+1],y1[i+1]);line(x1[0],y1[0],x1[3],y1[3]);} for(i=4;i<8;i++){if(i<7)line(x1[i],y1[i],x1[i+1],y1[i+1]);line(x1[4],y1[4],x1[7],y1[7]);}

for(i=0;i<4;i++){line(x1[i],y1[i],x1[i+4],y1[i+4]);}}};void main(){

Page 45: Cg Lab Manual

int i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],theta,op,ch,tx,ty,tz,sx,sy,sz,xf,yf,zf,x,y,z,size;int driver=DETECT;int mode;initgraph(&driver,&mode,"");cout<<"enter the points on the cube:";cin>>x>>y>>z;cout<<"enter the size of the edge:";cin>>size;x1[0]=x1[3]=x;x1[1]=x1[2]=x+size;x1[4]=x1[7]=x;x1[5]=x1[6]=x+size;y1[0]=y1[1]=y;y1[2]=y1[3]=y+size;y1[4]=y1[5]=y;y1[6]=y1[7]=y+size;z1[1]=z1[2]=z1[3]=z1[0]=z ;z1[4]=z1[5]=z1[6]=z1[7]=z-size;for(i=0;i<8;i++){x2[i]=x1[i]+z1[i]/2;y2[i]=y1[i]+z1[i]/2;}cube c;getch();cleardevice();do{cout<<"menu"<<endl;cout<<"\n1.translation\n2.rotation\n3.scaling\n4.exit\n";cout<<"enter the choice:";cin>>ch;switch(ch){case 1:cout<<"enter the translation vector:";cin>>tx>>ty>>tz;for(i=0;i<8;i++){x3[i]=x1[i]+tx;y3[i]=y1[i]+ty;z3[i]=z1[i]+tz;}for(i=0;i<8;i++)

Page 46: Cg Lab Manual

{x4[i]=x3[i]+z3[i]/2;y4[i]=y3[i]+z3[i]/2;}cleardevice();cout<<"before translation";c.drawcube(x2,y2);getch();cleardevice(); cout<<"after translation"; c.drawcube(x4,y4);getch();cleardevice();break;case 2:cout<<"enter the rotation angle:";cin>>theta;theta=(theta*3.14)/180;cout<<"enter the direction"<<endl;cout<<"1.rotation about x axis"<<endl<<"2.rotation about y axis"<<endl<<"3.rotation about z axis";cin>>op;if(op==1){for(i=0;i<8;i++)

{x3[i]=x1[i];y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);}}elseif(op==2){for(i=0;i<8;i++){y3[i]=y1[i];x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta);x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta);}}elseif(op==3){for(i=0;i<8;i++)

Page 47: Cg Lab Manual

{z3[i]=z1[i];x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta);y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta);}}elsecout<<"enter correct option";for(i=0;i<8;i++){x4[i]=x3[i]+z3[i]/2;y4[i]=y3[i]+z3[i]/2;}cleardevice();cout<<"before rotation";c.drawcube(x2,y2);getch();cleardevice(); cout<<"after rotation"; c.drawcube(x4,y4);getch();cleardevice();break;case 3:cout<<"enter the scaling factor:";cin>>sx>>sy>>sz;cout<<"enter the reference point:";cin>>xf>>yf>>zf;for(i=0;i<8;i++){x3[i]=xf+(x1[i]*sx)+xf*(1-sx);y3[i]=yf+(y1[i]*sy)+yf*(1-sy);z3[i]=zf+(z1[i]*sz)+zf*(1-sz); } for(i=0;i<8;i++){x4[i]=x3[i]+z3[i]/2;y4[i]=y3[i]+z3[i]/2;}cleardevice();cout<<"before scaling";c.drawcube(x2,y2);getch();cleardevice();

Page 48: Cg Lab Manual

cout<<"after scaling"; c.drawcube(x4,y4);getch();cleardevice();break;case 4:exit(0);break;}}while(op!=4);getch();}

INPUT& OUTPUT:

Enter the point in the cube: 100 100 100Enter the size of the edge: 50

Menu1. translation2. rotation3. scaling4. exit

Enter the choice:1Enter the translation vector: 5,10,15

Before translation After translation

                                                           

RESULT: Thus the program is executed and verified.

Page 49: Cg Lab Manual

EX. NO. 10DATE:

BASIC OPERATION ON IMAGES

AIM:

To perform basic operation on images using Adobe Photoshop.

PROCEDURE:

Feathering:

  Select the elliptical marques tool on duplicate   Right click on area and select feather   Set the feather radius.

Filtering:

  From filtering menu select B   From select radial Blue and motion Blue.   Set the angle and distance.   From filter menu, select liquefy which change the shape of flower.   From filter menu, select distart and then select glass.   Set distortion, smoothness and scaling for the object

Selection Tool:

This is used to select a portion of image and paste it in document.

Lasso Tool:

This is used to select the image in its shape.

Text Tool:

This is used to add text along with image.

Blur Tool:

This is used to change the image by its color, shape and dimension.

Painting Tool:

Page 50: Cg Lab Manual

The powerful Photoshop paint engine lets you to simulate traditional painting technique

include charcoal, pastel and wet or dry brush effects.

Drawing Tool:

Draw resolution independent vector shapes instantly with the line, rectangle, ellipse,

polygon and custom shape tool.

Transformation Tools:

Scale, rotate, distor or skew engine easily. Apply the 3D transform filter to simulate 3D

effects such as tables and boxes. Using the liquefy command to interactively push, pull, bucker

or bloat an image

Sponge Tool:

The sponge tool subtly changes the color saturation of an area.in gray scale mode,the tool

increases or decreases contrast by moving gray level away from or towards the middle gray.

Marques tools:

The marques tools let you to select rectangle, ellipse, rounded rectangular and 1-pixel

rows and columns by default, a selection border is dragged from its corner.

Pen Tools:

The pen tools used in conjunction with the shape tools to create complex shapes.

Page 51: Cg Lab Manual

RESULT: Thus the program is executed and verified.

Page 52: Cg Lab Manual

3D TRANSFORMATIONS

3 dimensional transformation it has three axis x,y,z.Depandting upon there coordinate it will perform there Translation ,Rotaion,Scaling, The translation can be performed by changing the sign of the translation components Tx, Ty, and Tz.

Source code programming#include <stdio.h>#include <stdlib.h>#include<graphics.h>#include<conio.h>void draw3d(int fs,int x[20],int y[20],int tx,int ty,int d);void draw3d(int fs,int x[20],int y[20],int tx,int ty,int d){int i,j,k=0;for(j=0;j<2;j++){for(i=0;i<fs;i++){if(i!=fs-1)line(x[i]+tx+k,y[i]+ty-k,x[i+1]+tx+k,y[i+1]+ty-k);elseline(x[i]+tx+k,y[i]+ty-k,x[0]+tx+k,y[0]+ty-k);}k=d;}for(i=0;i<fs;i++){line(x[i]+tx,y[i]+ty,x[i]+tx+d,y[i]+ty-d);}}

void main(){int gd=DETECT,gm;int x[20],y[20],tx=0,ty=0,i,fs,d;initgraph(&gd,&gm,"");printf("no of sides (front view only) : ");scanf("%d",&fs);printf("co-ordinates : ");for(i=0;i<fs;i++){printf("(x%d,y%d)",i,i);scanf("%d%d",&x[i],&y[i]);}printf("Depth :");

Page 53: Cg Lab Manual

scanf("%d",&d);draw3d(fs,x,y,tx,ty,d);printf("translation (x,y)");scanf("%d%d",&tx,&ty);draw3d(fs,x,y,tx,ty,d);getch();}

3D TRANSFORMATION in c program computer graphics lab---------------------------------------------------1.Translation2.Rotation3.Scaling4.ExitEnter your Choice :1Enter the points 01: 288Enter the points 10: 288Enter the points 10: 258Enter the points 11: 288Enter the points 20: 258Enter the points 21: 258Enter the points 30: 288

Page 54: Cg Lab Manual
Page 55: Cg Lab Manual

// C++ program to draw and move a circle on the screen (up and down). when hit any key from the //keyboard circle will stop moving and program ends if you don't press any key from the keyboard circle //will move on the screen infinite times. Here in this program kbhit() function is//used. it checks for the keyboard keystroke.

#include<iostream.h>#include<conio.h>#include<graphics.h>#include<dos.h>

main(){clrscr();int driver,mode;driver = DETECT;initgraph(&driver,&mode,"");

while (!kbhit()){for (int i=1;i<=400; i++){circle(300,i,10);delay(10);cleardevice();}

for ( i=400;i>=1; i--){circle(300,i,10);delay(10);cleardevice();}

}

Page 56: Cg Lab Manual

C++ program to generate concentric circles .

Here For loop will give radius (i) to circle function and each time it will increment by 5.Delay function will slow down the process to print the concentric circles on the screen. For initgraph() function and circle() function graphics.h header file is used the function initgraph() will convert text mode to graphics mode and closegraph() again converts graphics screen into text mode.In initgraph function third parameter is path of BGI files where graphics files are located. If the graphics driver files are other than this directory than type that path as a third parameter in initgraph() function. Many time we run a graphics program and often a BGI error occurres. It is related to this path of BGI files. Give correct path of BGI files in initgraph() function. If you don't know the path of BGI files then find *.BGI files by find option in winodws and you will be known the correct path.

#include<iostream.h>#include<conio.h>#include<graphics.h>#include<dos.h>

main(){clrscr();int driver,mode;driver = DETECT;initgraph(&driver,&mode,"C:\TC\BGI");for(int i=1;i<=50;i=i+5){circle(100,100,i);delay(100);}getch();closegraph();}

Page 57: Cg Lab Manual
Page 58: Cg Lab Manual
Page 59: Cg Lab Manual

Ex NO 9DATE: Drawing three dimensional objects and Scenes

AIM:To draw 3D objects and scenes

ALGORITHM:1. Parallel Projectiona) Enter the boundary coordinates of the object.b) Define the direction of the projection line.c) Align the projection plane so that it intersects each coordinate axis inwhich the object is defined at the same distance from the origin.d) The projection is perpendicular to the view planee) If the view plane is placed at position Z1 along the z axis , then anypoint (x,y,z) in viewing coordinates is transformed to projectioncoordinates xp=x and yp=y.2.Perspective Projectiona) Enter the boundary coordinates of the object.b) Define the projection reference point and thus the direction of theprojection lines.c) The projected view is obtained by transforming points along projectionlines that meet at the projection reference point.d) Calculate the intersection of the projection lines with the view plane.e) Parallel lines in the object that are not parallel to the plane areprojected into converging lines.f) Parallel lines that are parallel to the view plane will be projected asparallel lines.QUESTIONS1. What do you mean by Perspective projection?Perspective projection is one in which the lines of projection are not parallel. Instead,they all converge at a single point called the center of projection.2. What is Projection reference point?In Perspective projection, the lines of projection are not parallel. Instead, they allconverge at a single point called Projection reference point.3. What you mean by parallel projection?Parallel projection is one in which z coordinates is discarded and parallel lines fromeach vertex on the object are extended until they intersect the view plane.4. What is tweening?It is the process, which is applicable to animation objects defined by a sequence ofpoints, and that change shape from frame to frame.5.Define frame?One of the shape photographs that a film or video is made of is known asframe.6. What is key frame?One of the shape photographs that a film or video is made of the shape of an object isknown initially and for a small no of other frames called keyframe

Page 60: Cg Lab Manual

EX NO 10 Generating Fractal images

AIM: To generate fractal images.ALGORITHM:

The sierpinski triangle-A fractal image1.The sierpinski Triangle is created by infinite removals2.Each triangle is divided into 4 smaller upside down triangles3.The center of the 4 triangles is removed4. As the process is iterated infinite number of times, the total area of theset goes to infinity as the size of the each new triangle goes to zero5.After closer examination magnification factor is 2.6.With each magnification there are 3 divisions of a triangleDimension D=ln(3)/ln(2)D=1.5850QUESTIONS1.What is a fractal imageFractal is "a rough or fragmented geometric shape that can be split into parts, each ofwhich is (at least approximately) a reduced-size copy of the whole,"[1] a propertycalled self-similarity.2. Define the following:(i) X-height (ii) Set (iii) KerningX-height: The X-height is the measurement of the height of the characterX, in other words of the middle bit without any ascender or descender.(ii) Set: The width of the letters is called the set and is fixed relative to thepoint-size.(iii) Kerning: The spaces between letters in one world (tracking) can beadjusted in a process called kerning.3. Define the following respective to sound:(i) Waveform (ii) Frequency (iii) Amplitudei) WaveformSound is produced by the vibration of matter. During the vibration pressure variationare created in the air surrounding it. The pattern of the oscillation is called awaveform.(ii) FrequencyThe frequency of the sound is the reciprocal value of the period. It represents thenumber of period s in a second and it is measured in Hertz(Hz) or cycles per second.(iii) AmplitudeCS76©Einstein College of EngineeringPage 25 of 26A sound also has amplitude. The amplitude of a sound is a measure

Page 61: Cg Lab Manual

of the displacement of the air pressure wave from its, or quiescent state.4. Define quantization (or) resolution?The resolution (or) quantization of a sample value depends on the number of bits usedin measuring the height of the waveform. An 8-bit quantization yields 256 possiblevalues, 16-bit CD-qudra quantization results in over 65536 values.5. What are the types of sound objects that can be used in multimedia production?There are four types of sound objects that can be used in multimedia production:_ Waveform audio_ MIDI sound tracks_ Compact disc (CD) audio_ MP3 files6.What are the applications of fractal image?Produces visual effect. Hence used in film industry

Page 62: Cg Lab Manual

/*@@ program to translate rotate and scale an object in three dimention

@

*/

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

#define degree 3.14/180

int xaxis,yaxis,zaxis;

int i,j,k;

/*@ function to multiply two matrix@

*/

int matrixmul(float **a,float **b,float **c,int n){

for(i=0;i<n;i++){

for(j=0;j<4;j++){

c[i][j]=0;

for(k=0;k<4;k++){

c[i][j]+=a[i][k]*b[k][j];

}

}

}

return 1;

}

typedef struct{

float x,y,z;

int ad;

int *a;

} point3;

typedef struct{

float x,y;

Page 63: Cg Lab Manual

} point2;

point2 point(point3 p){

point2 p1;

p1.x=p.x+(p.z)*(cos(degree*225));

p1.y=p.y+(p.z)*(sin(degree*225));

return p1;

}

/*@ function to draw all three axis@

*/

void drawaxis(){

int x=getmaxx()/2,y=getmaxy()/2;

line(x,y,x-y*cos(degree*45),y+y*sin(degree*45));//zaxis

line(x,y,x,0);//yaxis/

line(x,y,getmaxx(),y);//xaxis

}

/*@ function to draw the object in three d

@

*/

void draw(point2 *p2,point3 *p3,int n){

for(i=0;i<n;i++){

for(j=0;j<p3[i].ad;j++){

line(xaxis+p2[i].x,yaxis-p2[i].y,xaxis+p2[p3[i].a[j]-1].x,yaxis-p2[p3[i].a[j]-1].y);

}

}

}

/*@ function to transalate the object in three d about origin@

*/

void transalate(float **a,float **b,float **c,int n){

float tx,ty,tz;

printf("Enter value of tx and ty and tz : ");

Page 64: Cg Lab Manual

scanf("%f%f%f",&tx,&ty,&tz);

b[0][1]=b[0][2]=b[1][0]=b[1][2]=b[0][3]=b[1][3]=b[2][3]=b[2][0]=b[2][1]=0;

b[0][0]=b[1][1]=b[2][2]=b[3][3]=1;

b[3][0]=tx;

b[3][1]=ty;

b[3][2]=tz;

matrixmul(a,b,c,n);

}

/*@ function to rotate the object in three d about origin@

*/

void rotate(float **a,float **b,float **c,int n){

int theta, choice;

printf("Enter your choice-----: 1 for x,2 for y, or 3 for z :");

scanf("%d",&choice);

printf("Enter value of angle : ");

scanf("%d",&theta);

b[0][3]=b[1][3]=b[2][3]=0;

b[3][3]=1;

switch(choice){

case 1: b[0][0]=1;

b[0][1]=0;

b[0][2]=0;

b[1][0]=0;

b[1][1]=cos(theta*degree);;

b[1][2]=-sin(theta*degree);;

b[2][0]=0;

b[2][1]=sin(theta*degree);;

b[2][2]=cos(theta*degree);;

b[3][0]=0;

b[3][1]=0;

Page 65: Cg Lab Manual

b[3][2]=0;

break;

case 2: b[0][0]=cos(theta*degree);;

b[0][1]=0;

b[0][2]=sin(theta*degree);;

b[1][0]=0;

b[1][1]=1;

b[1][2]=0;

b[2][0]=-sin(theta*degree);;

b[2][1]=0;

b[2][2]=cos(theta*degree);;

b[3][0]=0;

b[3][1]=0;

b[3][2]=0;

break;

case 3: b[0][0]=cos(theta*degree);;

b[0][1]=-sin(theta*degree);;

b[0][2]=0;

b[1][0]=sin(theta*degree);;

b[1][1]=cos(theta*degree);;

b[1][2]=0;

b[2][0]=0;

b[2][1]=0;

b[2][2]=1;

b[3][0]=0;

b[3][1]=0;

b[3][2]=0;

break;

default:printf("wrong choice.");break;

}

matrixmul(a,b,c,n);

}

Page 66: Cg Lab Manual

/*@ function for scaling @

*/

void scale(float **a,float **b,float **c,int n){

float sx,sy,sz;

printf("Enter scaling factors sx ,sy and sz : ");

scanf("%f%f%f",&sx,&sy,&sz);

b[0][2]=b[1][2]=b[2][0]=b[2][1]=b[0][3]=b[1][3]=b[2][3]=0;

b[2][2]=sz;

b[0][0]=sx;

b[1][0]=0;

b[0][1]=0;

b[1][1]=sy;

b[3][3]=1;

matrixmul(a,b,c,n);

}

void copy(point3 *p3a,point3 *p3b,int n){

for(i=0;i<n;i++){

p3a[i].x=p3b[i].x;

p3a[i].y=p3b[i].y;

p3a[i].z=p3b[i].z;

p3a[i].ad=p3b[i].ad;

for(j=0;j<p3b[i].ad;j++){

p3a[i].a[j]=p3b[i].a[j];

}

}

return;

}

/*@ MAIN PROGRAM @

*/

void main(){

int gd=DETECT,gm,n=2,choice=1;

float **a,**b,**c;

Page 67: Cg Lab Manual

point2 *p2;

point3 *p3,*Startp3;/*Startp3 has initial value*/

printf("Enter the number of vertices :");

scanf("%d",&n);

/*@ memory allocation to a b c matrices@

*/

a=(float**)malloc(n*sizeof(float*));

b=(float**)malloc(4*sizeof(float*));

c=(float**)malloc(n*sizeof(float*));

for(i=0;i<n;i++){

a[i]=(float*)malloc(4*sizeof(float));

c[i]=(float*)malloc(4*sizeof(float));

if(i<4) b[i]=(float*)malloc(4*sizeof(float));

}

/*@memory allocation to stuctures @

*/

p2=(point2 *)malloc(n*sizeof(point2));

p3=(point3 *)malloc(n*sizeof(point3));

Startp3=(point3 *)malloc(n*sizeof(point3));

//@ Taking input coordinates and index of their adjacent vertices@

printf("Enter the coordinates of vertices :");

for(i=0;i<n;i++){

printf("(x%d,y%d,z%d) :",i+1,i+1,i+1);

scanf("%f%f%f",&p3[i].x,&p3[i].y,&p3[i].z);

printf("Enter the number of adjacent vertices:");

scanf("%d",&p3[i].ad);

p3[i].a=(int *)malloc(p3[i].ad*sizeof(int));

printf("Enter index number\n");

for(j=0;j<p3[i].ad;j++){

printf("%d :",j+1);

Page 68: Cg Lab Manual

scanf("%d",&p3[i].a[j]);

}

p2[i]=point(p3[i]);

}

copy(Startp3,p3,n);

//@

//Showing values in p3

//@

for(i=0;i<n;i++){

printf("\n %5.2f%5.2f%5.2f",p3[i].x,p3[i].y,p3[i].z);

}

printf("\n\n");

//@@@

//Showing values in p2

//@@@@@@

for(i=0;i<n;i++){

printf("\n %5.2f%5.2f",p2[i].x,p2[i].y);

}

getch();

initgraph(&gd,&gm,"../bgi");

xaxis=getmaxx()/2;

yaxis=getmaxy()/2;

while(choice!=0){

//@

//showing the object

//@@@

drawaxis();

draw(p2,p3,n);

printf("Enter your choice :\n");

Page 69: Cg Lab Manual

printf(" 1 for transalate\n2 for rotate\n3 for scale \n 8 for reset the values\n 0 for exit . Enter :");

scanf("%d",&choice);

clrscr();

closegraph();

initgraph(&gd,&gm,"../bgi");

//@@

//making matrix a i.e ph

//@@

for(i=0;i<n;i++){

a[i][0]=p3[i].x;

a[i][1]=p3[i].y;

a[i][2]=p3[i].z;

a[i][3]=1;

}

switch(choice){

//@@@

case 1:transalate(a,b,c,n);

break;

case 2:rotate(a,b,c,n);

break;

case 3:scale(a,b,c,n);

break;

case 8:printf("Reset done,");

break;

case 0:printf("Bye Bye");

break;

default:printf("Wrong choice.\n ");

break;

}

Page 70: Cg Lab Manual

//@@

//converting matrix c i.e. ph` in p3 then p2

//@

for(i=0;i<n;i++){

p3[i].x=c[i][0];

p3[i].y=c[i][1];

p3[i].z=c[i][2];

p2[i]=point(p3[i]);

}

/*

@ Reset the value @

*/

if(choice==8){

copy(p3,Startp3,n);

for(i=0;i<n;i++){

p2[i]=point(p3[i]);

}

getch();

}

cleardevice();

//@@

//showing the transalated object

//@@

drawaxis();

draw(p2,p3,n);

printf("Press Enter to continue\n");

getch();

}

closegraph();

}

Page 71: Cg Lab Manual

Additional Exercises:-

Text Animation Program Using C Programming

#include<stdio.h>#include<math.h>#include<conio.h>#include<graphics.h>#define round(val) (int)(val+0.5)void main(){    int gd=DETECT,gm,sx,sy,tx,ty;      char text[50];      void move(int,int,int,int,char[]);      printf("Enter the text:");      scanf("%s",text);      printf("Enter the initial points:");      scanf("%d%d",&sx,&sy);      printf("Enter the TARGET points:");      scanf("%d%d",&tx,&ty);      initgraph(&gd,&gm,"");      outtextxy(sx,sy,text);      move(sx,sy,tx,ty,text);      getch();      closegraph();}void move(int sx,int sy,int tx,int ty,char text[50]){       int dx=tx-sx,dy=ty-sy,steps,k;       float xin,yin,x=sx,y=sy;       getch();       if(abs(dx)>abs(dy))          steps=abs(dy);       else          steps=abs(dy);       xin=dx/(float)steps;       yin=dy/(float)steps;       for(k=0;k<steps;k++)       {              cleardevice();              x+=xin;              y+=yin;              setcolor(15);              outtextxy(round(x),round(y),text);              delay(50);        }  }