+ All Categories
Home > Engineering > COMPUTER GRAPHICS LAB MANUAL

COMPUTER GRAPHICS LAB MANUAL

Date post: 12-Jan-2017
Category:
Upload: vivek-kumar-sinha
View: 55 times
Download: 9 times
Share this document with a friend
79
Computer Graphics Lab Manual [Type text] CSE/6 th /CG Lab/Prepared by Vivek Kumar Sinha
Transcript

COMPUTER GRAPHICS LAB MANUAL

Computer Graphics

Lab ManualUNIVERSITY SYLLABUS FOR PRACTICALS1. Implementation of line drawing , circle drawing & ellipse algorithm.

2. Programs to implement 2D transformation( Line , Cube , Rectangle)

Scaling

Translation

Rotation

3. Program to implement simple clipping algorithm. Implementation of Bezier Curve.

List Of Experiments1. Write a program to draw the pixel(x,y) and display the color in which

pixel(x,y) is illuminated on the screen.2. Write a program to implement DDA line drawing algorithm.

3. Write a program to implement Bresenhams Line drawing algorithm.

4. Write a program to implement Bresenhams Circle drawing algorithm.

5. Write a program to implement Bresenhams Ellips drawing algorithm.

6. Write a program to implement Boundary Fill algorithm.

7. Write a program to implement Flood Fill algorithm.

8. Write a program to Draw Rectangle from (100,200) pixel to (400,500) pixel .9. Write a program to draw a Circle with center (150,150) pixel and radius 25.10. Write a program to draw a Hexagon on the screen.11. Write a program to implement Composite Transformations.

12. Write a program to implement Basic Transformations (translation ,rotation ,

and scaling on a rectangle).

13. Write a program to implement Cohen Sutherland algorithm.

14. Write a program to implement Bezier Curve.

15. Write a program to implement B-Spline Curve.

16. Write a program to implement animation using C function.

17. Write a program to implement a cartoon using C function.18. Write a program to draw a chain of circles.19. Write a program to draw concentric circles.

20. Write a program to fill an ellipse by reducing the size of an ellipse.

EXPERIMENT NO. 01AIM : Write a program to draw the pixel(x,y) and display the color in which pixel(x,y) is illuminated on the screen.

DESCRIPTION: With the help of this program ,we are going to draw a Pixel(x,y) and also displaying the color in which this pixel(x,y) is illuminated on the screen.PROGRAM

#inlcude

#include

#include

Void main()

{

int gd=DETECT,gm;

initgraph(&gm,&gd,c:\\tc\\bgi);

putpixel(100,200,RED);

i=getpixel(100,200);

printf(The color of pixel is : );

printf(%d,i);

getch();

}

INPUT

100,200,REDOUTPUT

.The color of pixel is : 4 (RED)VIVA VOCE QUESTIONS

1. What are the parameters of initgraph().2. What is graphics driver.

3. What is graphics mode.

4. Define the function of putpixel.

5. Define the function of getpixel.

EXPERIMENT NO. 02AIM :Write a program to implement DDA line drawing algorithm.

DESCRIPTION: Digital Differential Analyzer (DDA) Method

The basis of the DDA method is to take unit steps along one coordinate and compute the corresponding values along the other coordinate. The unit steps are always along the coordinate of greatest change, e.g. if dx = 10 and dy = 5, then we would take unit steps along x and compute the steps along y.

ALGORITHM

1. input line endpoints, (xa,ya) and (xb, yb)

2. set pixel at position (xa,ya)

3. calculate slope m

4. Case |m|1: repeat the following steps until (xb, yb) is reached:

i. yi+1 = yi + dy/ dx

ii. xi+1 = xi + 1

iii. set pixel at position (xi+1,Round(yi+1))

5. Case |m|>1: repeat the following steps until (xn, yn) is reached:

i. xi+1 = xi + dx/ dy

ii. yi+1 = yi + 1

set pixel at position (Round(xi+1), yi+1PROGRAM# include < stdio.h >

# include < conio.h >

# include < graphics.h >

# define Round (a) (int (a+0.5))

void main()

{

int xa , ya , xb , yb , dx , dy , steps , k ;

float Xincr , Yincr , X ,Y ;

int gd=DETECT , gm ;

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

printf ( Enter the value of (xa , ya ) & (xb , yb)) ;

scanf ( %d%d%d%d, &xa ,&ya , &xb , &yb) ;

X = xa ; Y = yb ;

dx = xb xa ; dy = yb ya ;

cleardevice ( ) ;

if (abs (dx) > abs (dy))

steps = abs (dx) ;

else

steps = abs (dy) ;

Xincr = dx / (float) steps ;

Yincr = dy / (float) steps ;

putpixel (Round (x) , Round (y) , RED) ;

for (k = 0 ; k < steps ; k++ )

{

X = x + Xincr ;

Y = y + Yincr ;

putpixel ( Round (x) , Round (y) , RED) ;

}

getch ( ) ;

closegraph ( ) ;

}

INPUT

Enter the value of (xa , ya ) & (xb , yb) :-190

42

25

200

OUTPUT

VIVA-VOCE QUESTIONS1. Define DDA algorithm.2. What is abs function?3. Why we use steps?

EXPERIMENT NO. 03AIM :Write a program to implement Bresenhams line drawing algorithm.

DESCRIPTION:In this method, developed by Jack Bresenham, we look at just the center of the pixels. We determine d1 and d2 which is the "error", i.e., the difference from the " true line".

Steps in the Bresenham algorithm:

1. Determine the error terms

2. Define a relative error term such that the sign of this term tells us which pixel to choose

3. Derive equation to compute successive error terms from first

4. Compute first error term

ALGORITHM 1. Input line endpoints, (x0,y0) and (xn, yn)

2. Calculate (x = xn - x0 and (y = yn - y0

3. Calculate parameter p0 = 2 (y - (x

4. Set pixel at position (x0,y0)

5. Repeat the following steps until (xn, yn) is reached:

6. if pi < 0

Set the next pixel at position (xi +1, yi )

Calculate new pi+1 = pi + 2 (y

7. if pi 0

Set the next pixel at position (xi +1, yi + 1 )

Calculate new pi+1 = pi + 2((y - (x)PROGRAM

# include < stdio.h >

# include < conio.h >

# include < graphics.h >

void main ( )

{

int Xa , Ya , Xb , Yb ;

int dx , dy , p ;

int twody , twodydx ;

int x , y , Xend ;

int gd=DETECT , gm ;

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

printf ( Enter the value of (Xa , Ya ) & (Xb , Yb)) ;

scanf ( %d %d %d %d, &Xa ,&Ya , &Xb , &Yb) ;

dx = abs (Xb - Xa) ; dy = abs ( Yb - Ya) ;

p = 2 * dy dx ;

twody = 2 * dy ;

twodydx = 2 * ( dy - dx) ;

if ( Xa > Xb)

{

X = Xb ; Y = Ya ;

Xend = Xa ;

}

else

{

X = Xa ; Y = Ya ;

Xend = Xb ;

}

while (x < Xend)

{

x++ ;

if ( p < 0)

p + = twody ;

else

{

y++ ;

p + = twodydx ;

}

putpixel ( x ,y , BLUE) ;

}

getch( ) ;

closegraph ( ) ;

} INPUT

Enter the value of (Xa , Ya ) & (Xb , Yb) :-

100

110

240

250

OUTPUT

VIVA-VOCE QUESTIONS

1. What is the difference between DDA & Bresenhams line drawing algorithm?

2. What is c:\\tc\\bgi?3. Define closegraph().4. What is DETECT graphics driver?

EXPERIMENT NO. 04AIM :Write a program to implement Bresenhams Circle algorithm. DESCRIPTION : We sample at unit intervals and determine the closet pixel position to the specified circle path at each step. For a given radius r and screen center position (xc,yc), we can first set up our algorithm to calculate pixel positions around a circle path centered at the coordinate origin (0,0). Than each calculated position(x,y) is moved to its proper screen position by adding xc to x and yc to y. Along the circle section from x=0 to x=y in the first quadrant , the slope of the curve varies from 0 to -1. Therefore , we can take unit steps in the positive x direction over this octant and use a decision parameter to determine which of the two possible y positions is closer to the circle path at each step.ALGORITHM

1. Input radius r.2. Plot a point at (0, r).3. Calculate the initial value of the decision parameter as p0 = 5/4 r 1 r4. At each position xk, starting at k = 0, perform the following test:

if pk < 0

plot point at (xk +1, yk)

compute new pk+1 = pk + 2xk+1 + 1

else

plot point at (xk + 1, yk 1)

compute new pk+1 = pk + 2xk+1 + 1 2yk+1

where xk+1 = xk + 1 and yk+1 = yk - 1

5. Determine symmetry points in the other seven octants and plot points.6. Repeat steps 4 and 5 until x ( y.PROGRAM # include < stdio.h >

# include < conio.h >

# include < graphics.h >

void circle ( int , int ) ;

void main ( )

{

int x , y , p , r , i ;

int gd=DETECT , gm ;

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

setbkcolor (WHITE) ;

printf ( Enter the radius = ) ;

scanf ( %d , &r) ;

x = 0 ; y = r ;

p = 3 2 * r ;

putpixel ( x , y , RED) ;

while ( x < = y )

{

if (p < 0)

p + = 4 * x + 6;

else

{

p + = 4 * ( x - y) + 10 ;

y - - ;

}

x ++ ;

circle p ( x , y) ;

}

getch ( ) ;

closegraph ( ) ;

}

void circle p ( int a , int b)

{

int x , y ;

x = a ; y = b ;

putpixel ( 300 + x , 300 + y , 1 ) ;

putpixel ( 300 + y , 300 + x , 1 ) ;

putpixel ( 300 - x , 300 + y , 1 ) ;

putpixel ( 300 + x , 300 - y , 1 ) ;

putpixel ( 300 - x , 300 - y , 1 ) ;

putpixel ( 300 - y , 300 - x , 1 ) ;

putpixel ( 300 + y , 300 - x , 1 ) ;

putpixel ( 300 - y , 300 + x , 1 ) ;

}

getch ( ) ;

}INPUT

Enter the radius = 80

OUTPUT

VIVA-VOCE QUESTIONS

1. How many parameters are required to draw the circle?

2. Why we calculate the initial value of the decision parameter as p0 = 5/4 r 1 r ?

3. What is symmetry points?

4. What is plot points?

5. What is circle mid point?

EXPERIMENT NO. 05AIM :Write a program to implement Bresenhams ellipse algorithm. DESCRIPTION: We only need to calculate the values on the border of the circle in the first octant. The other values may be determined by symmetry. Assume a circle of radius r with center at (0,0).

ALGORITHM

1.Input rx,ry and ellipse center(xc,yc) and obtain the first point on an ellipse centered on the origin as(x0,y0)=(0,ry)

2.Calculate the initial value of the decision parameter in region 1 as

P10 = r2y r2xry + r2x

3.At each xk position in region 1 , starting at k=0 perform the following test: If p1k 0 , the next point along the ellipse centered on (0,0) is (xk,yk-1) andP2k+1 =p2k-2r2yk+1 + r2xOtherwise , the next point along the circle is (xk+1,yk-1) and

P2k+1 =p2k+2r2yxk+1 2r2xyk+1 + r2xUsing the same incremental calculations for x and y as in region 1.6.Determine symmetry points in the other three quadrants.

7.Move each calculated pixel position (x,y) onto the elliptical path centered on(xc,yc) and plot the coordinated values:

x=x+xc , y=y+yc.8. Repeat the steps for region 1 until 2r2yx>=2r2xy.

PROGRAM# include < stdio.h >

# include < conio.h >

# include < graphics.h >

void ellipse ( int , int ) ;

void main ( )

{

int a , b ;

int x = 0 , y ;

int aa , bb , aa2 , bb2 ;

int fx = 0 , fy ;

int p ;

int gd=DETECT , gm ;

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

printf ( \n Enter the value of a and b) ;

scanf ( %d %d , &a , &b) ;

y = b ; aa = a * a ;

bb = b * b ; aa2 = aa * 2 ;

bb2 = bb * 2 ; fy = aa2 * b ;

p = bb ( aa * b ) + (0.25 * aa) ;

while ( fx < fy )

{

x ++ ;

fx = fx + bb2 ;

if ( p < 0)

p + = fx + bb ;

else

{

y -- ;

fy = fy aa2 ;

p + = fx + bb fy ;

}

}

x ++ ;

circle p ( x , y) ;

}

getch ( ) ;

closegraph ( ) ;

}

void circle p ( int a , int b)

{

int x , y ;

x = a ; y = b ;

putpixel ( 300 + x , 300 + y , 1 ) ;

putpixel ( 300 + y , 300 + x , 1 ) ;

putpixel ( 300 - x , 300 + y , 1 ) ;

putpixel ( 300 + x , 300 - y , 1 ) ;

putpixel ( 300 - x , 300 - y , 1 ) ;

putpixel ( 300 - y , 300 - x , 1 ) ;

putpixel ( 300 + y , 300 - x , 1 ) ;

putpixel ( 300 - y , 300 + x , 1 ) ;

}

getch ( ) ;

}

INPUT

Enter the value of a and b =

16

25

OUTPUT

VIVA-VOCE QUESTIONS

1. Define region in ellipse.2. Define ellipse mid point?3. What do you mean by gm?

4. What is putpixel() & getpixel()?5. What do you mean by decision parameter?EXPERIMENT NO. 06AIM :Write a program to implement Boundary fill algorithm. DESCRIPTION : Start at a point inside the figure and paint with a particular color. Filling continues until a boundary color is encountered.

ALGORITHM

Start at a point inside a region. Paint the interior outward to the edge. The edge must be specified in a single color. Fill the 4-connected or 8-connected region. 4-connected fill is faster, but can have problems:

PROGRAM# include < stdio.h >

# include < conio.h >

# include < graphics.h >

void bfill ( int x , int y , int fill , int boundary ) ;

void main ( )

{

Int bfill , boundary ;

int gd=DETECT , gm ;

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

setcolor (WHITE ) ;

getch ( ) ;

bfill ( 22 , 22 , RED , WHITE ) ;

closegraph ( ) ;

}

void bfill ( int x , int y , int fill , int boundary)

{

int current ;

current = getpixel ( x , y) ;

if (current ! = boundary && current = = fill)

{

setcolor (fill) ;

putpixel ( x , y , fill ) ;

putpixel ( x , y , fill ) ;

bfill ( x + 1 , y , fill , boundary) ;

bfill ( x 1 , y , fill , boundary) ;

bfill ( x , y + 1 , fill , boundary) ;

bfill ( x , y - 1 , fill , boundary) ;

}

}

OUTPUT

VIVA-VOCE QUESTIONS1. Define Boundary Fill Algorithm?

2. How many parameters are required in Boundary Fill Algorithm?

3. Define the 4-connected region.4. Define the 8-connected region.EXPERIMENT NO. 07AIM :Write a program to implement Flood fill algorithm.

DESCRIPTION: The user speciies an interior color to be replaced by fill color (use 4 or 8 fill method). The process stops when no adjacent pixels are of interior color, e.g., replace white with blue.

But, even worse for overlapping polygons Same result as before, but no cure. In general the Scan Conversion method is the most versatile.

ALGORITHM Used when an area defined with multiple color boundaries Start at a point inside a region

Replace a specified interior color (old color) with fill color

Fill the 4-connected or 8-connected region until all interior points being replaced.PROGRAM

#include

#include

#include

void ffill( int x , int y , int fill , int boundary , int new , int old ) ;

void main( )

{

int fill , boundary , gd = DETECT , gm ;

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

setcolor ( WHITE ) ;

rectangle ( 20 , 20 ,40 ,50 ) ;

getch ( ) ;

closegraph ( ) ;

void ffill ( int x , int y , int new , int old )

{

if (getpixel ( x ,y )= = old )

{

setcolor ( new ) ;

putpixel ( x ,y ,new ) ;

ffill (x+1 ,y ,new ,old ) ;

ffill ( x-1 ,y ,new ,old ) ;

ffill( x ,y+1 ,new ,old ) ;

ffill( x ,y-1 ,new ,old ) ;

}

}OUTPUT

VIVA-VOCE QUESTIONS

1. Define Flood Fill Algorithm.2. How many parameters are needed in Flood Fill Alorithm?

3. Define fillarea function.

EXPERIMENT NO. 08AIM :Write a program to draw Rectangle from (100,200) pixel to (400,500) pixel.

DESCRIPTION : In this program ,we are going to draw a Rectangle having a pixel position from (100,200) pixel to (400,500) pixel.

PROGRAM#include

#include

#include

#include

main()

{

int gd=DETECT,gm;

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

setpalette(0,3);

moveto(100,200);

lineto(100,400);

lineto(400,400);

lineto(400,200);

lineto(100,200);

getch();

restorecrtmode();

return;

getch();

}

OUTPUT

VIVA-VOCE QUESTIONS

1. Expain all the parameters of initgraph().

2. What do you mean by DETECT?3. What is lineto and moveto function?EXPERIMENT NO. 09AIM :Write a program to draw a Circle with center (150,150) pixel and radius

25.

DESCRIPTION: With the help of this program ,we are going to draw a Circle having a center (150,150) pixel. And radius of the circle is 25.

PROGRAM

#include

#include

#include

#include

float x1,y1,r;

main()

{

int gd=DETECT,gm;

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

printf("Enter the center 'x1','y1' and radius 'r':\n");

scanf("%f%f%f",&x1,&y1,&r);

setpalette(0,12);

/*background will change from 0-block to 12*/

getch();

setcolor(3);

/*circle will be drawn in 3-green color*/

circle(x1,y1,r);

getch();

restorecrtmode();

return;

}

OUTPUT

VIVA-VOCE QUESTIONS

1. What is setpallette()?

2. How many parameters are required to draw a circle?

3. What is restorecrtmode function?

EXPERIMENT NO. 10

AIM :Write a program to draw a Hexagon on the screen.

DESCRIPTION: In this program , we are going to draw a hexagon on the screen using line(),moveto() & lineto().

PROGRAM

#include

#include

#include

#include

#define PI (22.0/7.0)main()

{

int gd=DETECT,gm,x1,y1,x2,y2,theta,xs,xc; initgraph(&gd,&gm,"c:\\tc\\bgi");

setcolor(4); x1=110;y1=200;

x2=60;y2=200;

line(x1,y1,x2,y2);

moveto(x2,y2);

theta=60*PI/180.0; xs=sin(theta);

xc=cos(theta);

lineto(x2=x2+50*xs,y2=y2-50*xc);

moveto(x2,y2);

lineto(x2=60,y2=y2-50*xc);

moveto(x2,y2);

lineto(x2=x2+50,y2=y2);

moveto(x2,y2);

lineto(x2=x2-50*xs,y2=y2+50*xc);

moveto(x2,y2);

lineto(110,200);

getch();

}OUTPUT

VIVA-VOCE QUESTIONS

1.What is difference between line() and lineto()?

2.Define setcolor.

EXPERIMENT NO. 11AIM :Write a program to implement Composite Transformation .

DESCRIPTION : A composite transformation is a sequence of transformations. For example, scaling followed by translation and rotation is a composite translation. The MultiplyTransform, RotateTransform, ScaleTransform, and TranslateTransform methods are used to generate composite transformations.PROGRAM :

#include

#include

#include

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 ;

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

INPUTEnter the value 30 40

OUTPUT

VIVA-QUESTIONS1. Define Composite Transformation.2. What do you mean by DETECT?3. What do you mean by End points?

EXPERIMENT NO. 12AIM :Write a program to implement Basic Transformations ( translation ,

rotation, and scaling on a Rectangle).

DESCRIPTION: Translation is one of the simplest transformations. A translation moves all points of an object a fixed distance in a specified direction. It can also be expressed in terms of two frames by expressing the coordinate system of object in terms of translated frames.1 .Development of the Transformation in Terms of Frames.2. Applying the Transformation Directly to the Local Coordinates of a Point.

ALGORITHM/*FOR SCALE (SX, SY) */1.Input the Scaling Factors SX, SY.

2.Calculate Scaled points by using Scaling Factors:

NewX=oldX * SX

NewY=oldY*SY

3.Repeat step 2 for all points.

4.Draw the new scale using new points.

PROGRAM // (A) SCALING//

# include < stdio.h >

# include < conio.h >

# include < graphics.h >

void main ( )

{

float t ;

int gd = DETECT , gm ;

int x1 ,y1, x4, y4, sx1, sy1 ;

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

x1 = 50 ; y1 = 50 ;

x4 = 130 ; y4 = 130 ;

rectangle ( x1, y1, x4, y4 ) ;

getch ( ) ;

cleardevice ( ) ;

printf ( \n Enter The Scaling Vector : ) ;

scanf ( %d %d , &sx1, &sy1 ) ;

x1 =x1 * sx1 ;

y1 = y1 * sy1 ;

x4 = x4 * sx1 ;

y4 = y4 * sy1 ;

rectangle ( x1 , y1 , x4 , y4 ) ;

getch ( ) ;

closegraph ( ) ;

}

OUTPUT

INPUT

Enter The Scaling Vector :- 2 3OUTPUT

ALGORITHM

/*For Translate (TX, TY)*/1.Input the Translation Factors TX, TY.2.Calculate Translated points by using Translation Factors: NewX=oldX + TX

NewY=oldY+TY 3.Repeat step 2 for all points. 4.Draw the new rectangle using new points.PROGRAM

// (B) TRANSLATION//

# include < stdio.h >

# include < conio.h >

# include < graphics.h >

void main ( )

{

float t ;

int gd = DETECT , gm ;

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

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

printf ( Enter The Four Co-ordinate For Rectangle :) ;

scanf ( %d %d %d %d , &x1 , &y1 , &x4 , &y4 ) ;

rectangle ( x1, y1, x4, y4 ) ;

getch ( ) ;

cleardevice ( ) ;

printf ( \n Enter The Translation Vector : ) ;

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

x1 =x1 * tx1 ;

y1 = y1 * ty1 ;

x4 = x4 * tx1 ;

y4 = y4 * ty1 ;

rectangle ( x1 , y1 , x4 , y4 ) ;

getch ( ) ;

closegraph ( ) ;

}

INPUTEnter The Four Co-ordinate For Rectangle : 50 50 100 100

OUTPUT

INPUTEnter The Translation Vector : - 70 70

OUTPUT

ALGORITHM /*FOR ROTATE (A)*/1.Input the Rotation angel A.

2.Calculate rotated points by using Rotation angle:

NewX=oldX cos OldY*sin NewY=oldX*cos+OldY*cos 3.Repeat step 2 for all points.

4.Draw the new rotation using new points

PROGRAM

// (C) ROTATION//

# include < stdio.h >

# include < conio.h >

# include < graphics.h >

# include < math.h >

void main ( )

{

float t ;

int gd = DETECT , gm ;

int x1 ,y1, x4, y4 ;

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

printf ( Enter Four Co-ordinates ) ;

scanf ( %d 5d %d %d , &x1 , &y1 , &x4 , &y4 ) ;

rectangle ( x1, y1, x4, y4 ) ;

getch ( ) ;

cleardevice ( ) ;

printf ( \n Enter Angle For Rotation : ) ;

scanf ( %f , &t ) ;

t = ( t * 2 * 3.14 ) / 360 ;

x1 = ( x1 * cos ( t ) + y1 * sin ( t ) ) ;

y1 = ( - x1 * sin ( t ) + y1 * cos ( t ) ) ;

x4 = ( x4 * cos ( t ) + y4 * sin ( t ) ) ;

y4 = ( - x4 * sin ( t ) + y4 * cos ( t ) ) ;

rectangle ( x1 , y1 , x4 , y4 ) ;

getch ( ) ;

closegraph ( ) ;

}

INPUTEnter Four Co-ordinates : - 50 50 110 110

OUTPUT

INPUTEnter Angle For Rotation : - 30

OUTPUTVIVA- VOCE QUESTIONS

1. What is translation?2. How many arguments are passed for translation?3. What is rotation?

4. How many arguments are passed for rotation?5. What is scaling on a rectangle?

6. How many arguments are passed for rectangle?EXPERIMENT NO. 13AIM : Write a program to implement Cohen Sutherland line Clipping algorithm.

DESCRIPTION: The Cohen-Sutherland line clipping algorithm quickly detects and dispenses with two common and trivial cases. To clip a line, we need to consider only its endpoints. If both endpoints of a line lie inside the window, the entire line lies inside the window. It is trivially accepted and needs no clipping. On the other hand, if both endpoints of a line lie entirely to one side of the window, the line must lie entirely outside of the window. It is trivially rejected and needs to be neither clipped nor displayed.

ALGORITHM 1. End-points pairs are checked for trivial acceptance or rejection using outcode;

2. If not trivially accepted or rejected, divide the line segment into two at a clip edge;

3. Iteratively clipped by test trivial-acceptance or trivial-rejection, and divided into two segments until completely inside or trivial-rejection.

PROGRAM

#include < stdio.h>

#include < conio.h>

#include < graphics.h>

#define ROUND ( a ) ( ( int ) ( a + 0.5 ))

#define LEFT_EDGE ox1

#define RIGHT_EDGE ox2

#define BOTTOM_EDGE ox4

#define TOP_EDGE ox8

unsigned char encode ( wcpt2 pt, dept winmin, dept winmax)

{

unsigned char code = ox00 ;

if ( pt.x < winmin.x)

code = code | LEFT_EDGE ;

if ( pt.x > winmax.x)

code = code | RIGHT_EDGE ;

if ( pt.y < winmin.y)

code = code | BOTTOM_EDGE ;

if ( pt.y > winmax.y)

code = code | TOP_EDGE ;

return ( code ) ;

}

void swappts ( unsigned char *c1, unsigned char *c2)

{

unsigned char temp ;

temp = *c1 ;

*c1 = *c2 ;

*c2 = temp ;

}

void dipline ( dept winmin, dept winmax, wept2 p1, wept 2 p2)

{

unsigned char code1,code2 ;

int done = FALSE, draw = FALSE ;

float m ;

while ( ! done )

{

code1 = emode ( p1, winmin, winmax) ;

code2 = emode ( p2, winmin, winmax) ;

if ( ACCEPT ( code1,code2 ))

{

done = FALSE ;

else

if ( REJECT ( code1, code2))

done = TRUE ;

else

if ( INSIDE ( code1))

{

swappts ( &p1, &p2) ;

swapcodes ( &code1, &code2) ;

if ( p2.x ! = p1.x)

{

m = ( p2.y p1.y) / ( p2.x p1.x) ;

}

if ( code1 & LEFT_EDGE)

{

p1.y + = ( winmin.x p1.x ) * m ;

p1.x + = winmin.x ;

}

else

if ( code & RIGHT_EDGE)

{

p1.y + = ( winmax.x p1.x) * m ;

p1.x = winmax.x ;

}

else

if ( code & BOTTOM_EDGE)

{

if ( p2.x ! = p1.x)

{

p1.x + = ( winmin.y p1.y) / m ;

p1.y = winmin.y ;

}

}

else

if( code & TOP_EDGE)

{

if ( p2.x ! = p1.x)

{

p1.x + = ( winmax.y p1.y) / m ;

p1.y = winmax.y ;

}

}

if ( draw)

lineDDA ( ROUND ( p1.x), ROUND ( p1.y), ROUND ( p2.x), ROUND ( p2.y)) ;

}

}

OUTPUT

VIVA- VOCE QUESTION

1. Define Cohen Sutherland line Clipping algorithm.2. What is intersection point?

3. Define the clip rectangle with diagonal.

EXPERIMENT NO. 14AIM: Write a program to implement Bezier Curve.

DESCRIPTION: A bezier curve allows you to specify, not only the end points of the line, but also the direction of the line as it passes through the end points. The algorithm draws a curve that passes through the end points at an angle parallel to the specified direction.ALGORITHM The user supplies d control points, pi

Write the curve as:

The functions Bid are the Bernstein polynomials of degree d

Where else have you seen them?

This equation can be written as a matrix equation also

There is a matrix to take Hermite control points to Bezier control points.

Bezier Basis Functions for d=3

Bezier Curve Properties

The first and last control points are interpolated. The tangent to the curve at the first control point is along the line joining the first and second control points. The tangent at the last control point is along the line joining the second last and last control points. The curve lies entirely within the convex hull of its control points: The Bernstein polynomials (the basis functions) sum to 1 and are everywhere positive. They can be rendered in many ways: E.g.: Convert to line segments with a subdivision algorithm.PROGRAM #include < stdio.h >

# include < conio.h >

# include < graphicd.h >

void main ( )

{

double numsteps , i , t ;

float stepsize ;

int x0 , y0 , x1 , y1 , x2 , y2 , x3 , y3 , x , y ;

int ax , ay , bx , by , cx , cy , dx , dy ;

int gd = DETECT , gm ;

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

printf ( \n Enter The Value of x0 & y0 ) ;

scanf ( %d %d , &x0 , &y0 ) ;

printf ( \n Enter The Value of x1 & y1 ) ;

scanf ( %d %d , &x1 , &y1 ) ;

printf ( \n Enter The Value of x2 & y2 ) ;

scanf ( %d %d , &x2 , &y2 ) ;

printf ( \n Enter The Value of x3 & y3 ) ;

scanf ( %d %d , &x3 , &y3 ) ;

ax = - x0 + 3 * x1 + ( - 3 * x2 ) + x3 ;

ay = - y0 + 3 * y1 + ( - 3 * y2 ) + y3 ;

bx = 3 * x0 + ( -6 * x1 ) + 3 * x2 ;

by = 3 * y0 + ( -6 * y1 ) + 3 * y2 ;

cx = 3 * x0 + 3 * x1 ;

cy = 3 * y0 + 3 * y1 ;

dx = x0 ;

dy = y0 ;

setcolor ( MAGENDA ) ;

numstep = 100 ;

stepsize = 1.0 / ( double ) numsteps ;

moveto ( dx , dy ) ;

for ( i = 1 ; i < numsteps ; i ++)

{

t = stepsize * ( double ) i ;

x = ax * ( t * t * t ) + by * ( t * t ) + cy * t + dy ;

lineto ( x , y ) ;

}

getch ( ) ;

closegraph () ;

}

OUTPUT

VIVA-QUESTIONS

1. What is Bezier Curve?

2. How many parameters are needed for Bezier Curve?

3. What is blendingValue?

4. What is the computeCoefficients?

EXPERIMENT NO. 15AIM : Write a program to implement B-Spline Curve.

DESCRIPTION : In the process of subdividing a B-spline curve, a large number of control points will be introduced. Therefore, manipulating a B-spline curve is easier than manipulating its component Bzier curves. Moreover, the B-spline curve is Cp-k continuous at a knot point, where k is the multiplicity of the corresponding knot. When we manipulate a B-spline curve by moving control points, this continuity is always maintained. However, if a B-spline curve is subdivided into a sequence of Bzier curves, maintaining the continuity at the joining control points would be a challenging task. Consequently, handling a B-spline curve is much easier than handling a sequence of Bzier curves.

ALGORITHM PROGRAM

#include < stdio.h >

#include < conio.h >

#include < graphics.h >

void SplinePoint(int *u,int n,int t,double v,XYZ *control,XYZ *output)

{

int k;

double b;

output->x = 0;

output->y = 0;

output->z = 0;

for (k=0; kx + = control[k].x * b;

output->y + = control[k].y * b;

output->z + = control[k].z * b;

}

}

double SplineBlend(int k,int t,int *u,double v)

{

double value;

if (t == 1)

{

if ((u[k]

# include < graphicd.h >

void main ( )

{

int x , y , a , b , i ;

int gd = DETECT , gm ;

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

for ( i = 0 ; i < 320 ; i ++ )

{

rectangle ( 325 , 440 , 330 , 50 ) ;

rectangle ( 325 , 440 , 440 , 455 ) ;

rectangle ( 325 , 440 - i , 450 , 370 - i ) ;

circle ( 387 , 410 i , 10 ) ;

line ( 325 , 399 i , 450 , 399 i ) ;

line ( 325 , 420 i , 450 , 420 i ) ;

delay ( 15 ) ;

clear device ( ) ;

}

rectangle ( 325 , 410 , 330 , 50 ) ;

rectangle ( 325 , 440 , 440 , 455 ) ;

rectangle ( 325 , 120 , 450 , 50 ) ;

circle ( 387 , 90 , 10 ) ;

line ( 325 , 79 , 450 , 79 ) ;

line ( 325 , 100 , 450 , 100 ) ;

outtextxy ( x , y , * ) ;

getch ( ) ;

closegraph ( ) ;

}

}

OUTPUT

VIVA-VOCE QUESTIONS1. What is outtextxy()?2. Write the purpose of rectangle function & how many parameters are required?

3. What is graphics mode?

4. What is graphics driver?

EXPERIMENT NO. 17AIM :Write a program to implement a cartoon using C function.

DESCRIPTION : In this program ,we are going to create a cartoon.PROGRAM

#include

#include

#include

#include

void main()

{

int gd = DETECT , gm, i, a ;

clrscr( ) ;

initgraph ( &gd, &gm, "c:\\tc\\bgi") ;

for (i = (- 80 ) ; i< = 300 ; i + = 5 )

{

circle ( 200 + i, 200,100 ) ;

setfillstyle(SOLID_FILL,i) ;

floodfill ( 200 + i, 200, WHITE ) ;

circle ( 160 + i, 160, 10 ) ;

setfillstyle ( SOLID_FILL, i + 1 ) ;

floodfill (160 + i, 160, WHITE ) ;

circle ( 240 + i, 160, 10 ) ;

setfillstyle ( SOLID_FILL, i + 1 ) ;

floodfill ( 240 + i, 160, WHITE ) ;

arc (200 + i, 200, 200, 340, 60 ) ;

delay ( 200 ) ;

clrscr( ) ;

}

getch( ) ;

}

OUTPUT

VIVA-VOCE QUESTIONS1. Define setfillstyle function.

2. Define floodfill function.

3. Define delay function .4. Why we used arc function?

EXPERIMENT NO. 18AIM :Write a program to draw a chain of Circle.

DESCRIPTION : Here in this program , we are drawing a chain of circles.PROGRAM

# include# include# include#include

void main()

{

int gd = DETECT, gm , i ;

initgraph ( &gd, &gm, "c:\\tc\\bgi") ;

for(i=50;i0.0)

{

ellipse(xc,yc,0,360,rx,ry);

--rx;

++ry;

getch();

}

return;

}

Void main(){

float xc,yc,rx,ry;

int gd=DETECT,gm,i;

initgraph(&gd,&gm,c:\\tc\\bgi);

setpalette(0,23);

for(i=0;i


Recommended