Computer Graphics
Lab Manual
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
UNIVERSITY SYLLABUS FOR PRACTICALS
1. 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.
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
List Of Experiments
1. 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 Bresenham’s Line drawing algorithm.
4. Write a program to implement Bresenham’s Circle drawing algorithm.
5. Write a program to implement Bresenham’s 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.
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 01
AIM : 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<stdio.h>
#include<conio.h>
#include<graphics.h>
Void main()
{
int gd=DETECT,gm;
initgraph(&gm,&gd,”c:tcbgi”);
putpixel(100,200,RED);
i=getpixel(100,200);
printf(“The color of pixel is : “);
printf(“%d”,i);
getch();
}
INPUT
100,200,RED
OUTPUT
.
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.
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 02
AIM :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+1
PROGRAM
# 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 , “tcbgi”) ;
printf ( “Enter the value of (xa , ya ) & (xb , yb)”) ;
scanf ( “%d%d%d%d”, &xa ,&ya , &xb , &yb) ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 ( ) ;
}
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
INPUT
Enter the value of (xa , ya ) & (xb , yb) :-
190
42
25
200
OUTPUT
VIVA-–VOCE QUESTIONS
1. Define DDA algorithm.
2. What is abs function?
3. Why we use steps?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 03
AIM :Write a program to implement Bresenham’s 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 >
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 , “tcbgi”) ;
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)
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
{
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
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
VIVA-–VOCE QUESTIONS
1. What is the difference between DDA & Bresenham’s line drawing algorithm?
2. What is c:tcbgi?
3. Define closegraph().
4. What is DETECT graphics driver?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 04
AIM :Write a program to implement Bresenham’s 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 – r
4. 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 , “tcbgi”) ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 ?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
3. What is symmetry points?
4. What is plot points?
5. What is circle mid point?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 05
AIM :Write a program to implement Bresenham’s 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 = r2
y – r2
xry + ¼ r2
x
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+1,yk) and
P1k+1 = p1k+ 2r2
yxk+1 + r2
y
Otherwise the next point along the circle is (xk+1,yk-1) and
P1k+1 = p1k+ 2r2
yxk+1 – 2r2
xyk+1 r2
y
With
2r2
yxk+1 =2r2
yxk + 2r2
y , 2r2
xyk+1 = 2r2
xyk – 2r2
x
4. Calculate the initial value of the decision parameter in region 2 using the last
point (x0,y0) calculated in region 1 as
P20=r2
y(x0+1/2)2
+r2
x(y0-1)2
– r2
xr2
y
5. At each yk position in region 2 , starting at k=0 perform the following test: If
p2k>0 , the next point along the ellipse centered on (0,0) is (xk,yk-1) and
P2k+1 =p2k-2r2
yk+1 + r2
x
Otherwise , the next point along the circle is (xk+1,yk-1) and
P2k+1 =p2k+2r2
yxk+1 – 2r2
xyk+1 + r2
x
Using 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:
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
x=x+xc , y=y+yc.
8. Repeat the steps for region 1 until 2r2
yx>=2r2
xy.
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 , “tcbgi”) ;
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 ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 ) ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 06
AIM :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 ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
int gd=DETECT , gm ;
initgraph ( &gd , &gm , “tcbgi”) ;
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) ;
}
}
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
OUTPUT
VIVA-–VOCE QUESTIONS
1. 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.
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 07
AIM :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 <stdio.h>
#include <conio.h>
#include <graphics.h>
void ffill( int x , int y , int fill , int boundary , int new , int old ) ;
void main( )
{
int fill , boundary , gd = DETECT , gm ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
initgraph (&gd , &gm ,”tcbgi”) ;
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
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
1. Define Flood Fill Algorithm.
2. How many parameters are needed in Flood Fill Alorithm?
3. Define fillarea function.
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 08
AIM :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<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"tcbgi");
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?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 09
AIM :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<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
float x1,y1,r;
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"tcbgi");
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
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#define PI (22.0/7.0)
main()
{
int gd=DETECT,gm,x1,y1,x2,y2,theta,xs,xc;
initgraph(&gd,&gm,"c:tcbgi");
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();
}
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
OUTPUT
VIVA-–VOCE QUESTIONS
1.What is difference between line() and lineto()?
2.Define setcolor.
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 11
AIM :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 <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 ,”tcbgi”) ;
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 ) ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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
VIVA-QUESTIONS
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
1. Define Composite Transformation.
2. What do you mean by DETECT?
3. What do you mean by End points?
EXPERIMENT NO. 12
AIM :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 ( )
{
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
float t ;
int gd = DETECT , gm ;
int x1 ,y1, x4, y4, sx1, sy1 ;
initgraph ( &gd , &gm , “tcbgi” ) ;
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
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
INPUT
Enter The Scaling Vector :- 2 3
OUTPUT
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
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
// (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 , “tcbgi” ) ;
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 ( ) ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
}
INPUT
Enter The Four Co-ordinate For Rectangle : 50 50 100 100
OUTPUT
INPUT
Enter 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
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 , “tcbgi” ) ;
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 ) ) ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
rectangle ( x1 , y1 , x4 , y4 ) ;
getch ( ) ;
closegraph ( ) ;
}
INPUT
Enter Four Co-ordinates : - 50 50 110 110
OUTPUT
INPUT
Enter Angle For Rotation : - 30
OUTPUT
VIVA- 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?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 13
AIM : 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)
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
A
B
C
D
E
F
G
H
I
100
1
000
1
010
1
010
0
000
0
100
0
101
0
001
0
011
0
{
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 ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 ;
}
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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)
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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.
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 14
AIM: 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.
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
( ) ( )∑=
=
d
i
d
ii tBt
0
px ( ) ( ) idid
i tt
i
d
tB
−
−





= 1
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 , “ tcbgi ” ) ;
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 ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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 () ;
}
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 15
AIM : 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 Bézier 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 Bézier 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
Bézier 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; k<=n; k++)
{
b = SplineBlend(k,t,u,v);
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
output->x + = 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] <= v) && (v < u[k+1]))
value = 1;
else
value = 0;
}
else
{
if ((u[k+t-1] == u[k]) && (u[k+t] == u[k+1]))
value = 0;
else if (u[k+t-1] == u[k])
value = (u[k + t] - v) / (u[k+t] - u[k+1]) * SplineBlend(k+1,t-1,u,v);
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
else if (u[k+t] == u[k+1])
value = (v - u[k]) / (u[k+t-1] - u[k]) * SplineBlend(k,t-1,u,v);
else
value = (v - u[k]) / (u[k+t-1] - u[k]) * SplineBlend(k,t-1,u,v) +
(u[k+t] - v) / (u[k+t] - u[k+1]) * SplineBlend(k+1,t-1,u,v);
}
return(value);
}
void SplineKnots(int *u,int n,int t)
{
int j;
for (j=0;j<=n+t;j++) {
if (j < t)
u[j] = 0;
else if (j <= n)
u[j] = j - t + 1;
else if (j > n)
u[j] = n - t + 2;
}
}
OUTPUT
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
VIVA-VOCE QUESTIONS
1. What is B-Spline Curve?
2. What is SplinePoint?
3. Define the SplineBlend?
4. What do you mean by SplineKnots?
EXPERIMENT NO. 16
AIM :Write a program to implement Animation using C function.
DESCRIPTION: A simulation of movement created by displaying a series of pictures,
or frames. Cartoons on television is one example of animation. Animation on computers
is one of the chief ingredients of multimedia presentations. There are many software
applications that enable you to create animations that you can display on a computer
monitors.
The difference between animation and video. Whereas video takes continuous motion
and breaks it up into discrete frames, animation starts with independent pictures and puts
them together to form the illusion of continuous motion.
PROGRAM
#include < stdio.h >
# include < conio.h >
# include < graphicd.h >
void main ( )
{
int x , y , a , b , i ;
int gd = DETECT , gm ;
initgraph ( &gd ,&gm , “ tcbgi ” ) ;
for ( i = 0 ; i < 320 ; i ++ )
{
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
VIVA-VOCE QUESTIONS
1. 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. 17
AIM :Write a program to implement a cartoon using C function.
DESCRIPTION : In this program ,we are going to create a cartoon.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd = DETECT , gm, i, a ;
clrscr( ) ;
initgraph ( &gd, &gm, "c:tcbgi") ;
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
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
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
VIVA-VOCE QUESTIONS
1. Define setfillstyle function.
2. Define floodfill function.
3. Define delay function .
4. Why we used arc function?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 18
AIM :Write a program to draw a chain of Circle.
DESCRIPTION : Here in this program , we are drawing a chain of circles.
PROGRAM
# include<graphics.h>
# include<conio.h>
# include<stdio.h>
#include<math.h>
void main()
{
int gd = DETECT, gm , i ;
initgraph ( &gd, &gm, "c:tcbgi") ;
for(i=50;i<=200;i=i+30)
{
circle(i,i+30,50);
getch();
restorcrtmode();
}
OUTPUT
VIVA-VOCE QUESTIONS
1.Expain all the parameters of initgraph().
2.What do you mean by DETECT?
3.What is restorcrtmode()?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 19
AIM :Write a program to display concentric circles.
DESCRIPTION : Here in this program , we are drawing a concentric circles.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int d=DETECT,m;
initgraph(&d,&m,"c:tcbgi");
setcolor(4);
for(i=25;i<150;i+=25)
{
circle(150,150,i);
getch();
/*draw concentric circle of radius i pixels and center at 50,50,I in color 3*/
restorcrtmode();
}
OUTPUT
VIVA-VOCE QUESTION
1.Expain all the parameters of initgraph().
2.What do you mean by DETECT?
3.What is restorcrtmode()?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
EXPERIMENT NO. 20
AIM :Write a program to fill an ellipse by reduce the size of ellipse.
DESCRIPTION :
PROGRAM
#include<graphics.h>
include<stdio.h>
#include<conio.h>
#include<math.h>
elli_R(xc,yc,rx,ry)
float xc,yc,rx,ry;
{
while(rx>0.0 && ry>0.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:tcbgi”);
setpalette(0,23);
for(i=0;i<=7;++i)
{
setcolor(1);
getch();
ellipse(250,250,0,360,140,80);
getch();
elli_R(250.0,250.0,140.0,80.0);
}
restorecrtmode();
return;
}
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha
OUTPUT
VIVA-VOCE QUESTION
1. Define ellipse function.
2. What do you mean by setpalette function?
3. What is DETECT driver?
4. What is Graphics Mode?
[Type text]
CSE/6th
/CG Lab/Prepared by Vivek Kumar Sinha

COMPUTER GRAPHICS LAB MANUAL

  • 1.
    Computer Graphics Lab Manual [Typetext] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 2.
    UNIVERSITY SYLLABUS FORPRACTICALS 1. 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. [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 3.
    List Of Experiments 1.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 Bresenham’s Line drawing algorithm. 4. Write a program to implement Bresenham’s Circle drawing algorithm. 5. Write a program to implement Bresenham’s 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. [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 4.
    EXPERIMENT NO. 01 AIM: 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<stdio.h> #include<conio.h> #include<graphics.h> Void main() { int gd=DETECT,gm; initgraph(&gm,&gd,”c:tcbgi”); putpixel(100,200,RED); i=getpixel(100,200); printf(“The color of pixel is : “); printf(“%d”,i); getch(); } INPUT 100,200,RED OUTPUT . 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. [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 5.
    EXPERIMENT NO. 02 AIM: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+1 PROGRAM # 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 , “tcbgi”) ; printf ( “Enter the value of (xa , ya ) & (xb , yb)”) ; scanf ( “%d%d%d%d”, &xa ,&ya , &xb , &yb) ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 6.
    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 ( ) ; } [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 7.
    INPUT Enter the valueof (xa , ya ) & (xb , yb) :- 190 42 25 200 OUTPUT VIVA-–VOCE QUESTIONS 1. Define DDA algorithm. 2. What is abs function? 3. Why we use steps? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 8.
  • 9.
    EXPERIMENT NO. 03 AIM:Write a program to implement Bresenham’s 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 > [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 10.
    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 , “tcbgi”) ; 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) [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 11.
    { 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 [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 12.
    VIVA-–VOCE QUESTIONS 1. Whatis the difference between DDA & Bresenham’s line drawing algorithm? 2. What is c:tcbgi? 3. Define closegraph(). 4. What is DETECT graphics driver? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 13.
    EXPERIMENT NO. 04 AIM:Write a program to implement Bresenham’s 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 – r 4. 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 , “tcbgi”) ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 14.
    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 ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 15.
    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 ? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 16.
    3. What issymmetry points? 4. What is plot points? 5. What is circle mid point? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 17.
    EXPERIMENT NO. 05 AIM:Write a program to implement Bresenham’s 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 = r2 y – r2 xry + ¼ r2 x 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+1,yk) and P1k+1 = p1k+ 2r2 yxk+1 + r2 y Otherwise the next point along the circle is (xk+1,yk-1) and P1k+1 = p1k+ 2r2 yxk+1 – 2r2 xyk+1 r2 y With 2r2 yxk+1 =2r2 yxk + 2r2 y , 2r2 xyk+1 = 2r2 xyk – 2r2 x 4. Calculate the initial value of the decision parameter in region 2 using the last point (x0,y0) calculated in region 1 as P20=r2 y(x0+1/2)2 +r2 x(y0-1)2 – r2 xr2 y 5. At each yk position in region 2 , starting at k=0 perform the following test: If p2k>0 , the next point along the ellipse centered on (0,0) is (xk,yk-1) and P2k+1 =p2k-2r2 yk+1 + r2 x Otherwise , the next point along the circle is (xk+1,yk-1) and P2k+1 =p2k+2r2 yxk+1 – 2r2 xyk+1 + r2 x Using 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: [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 18.
    x=x+xc , y=y+yc. 8.Repeat the steps for region 1 until 2r2 yx>=2r2 xy. 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 , “tcbgi”) ; 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 ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 19.
    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 ) ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 20.
    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? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 21.
  • 22.
    EXPERIMENT NO. 06 AIM: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 ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 23.
    int gd=DETECT ,gm ; initgraph ( &gd , &gm , “tcbgi”) ; 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) ; } } [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 24.
    OUTPUT VIVA-–VOCE QUESTIONS 1. DefineBoundary Fill Algorithm? 2. How many parameters are required in Boundary Fill Algorithm? 3. Define the 4-connected region. 4. Define the 8-connected region. [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 25.
  • 26.
    EXPERIMENT NO. 07 AIM: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 <stdio.h> #include <conio.h> #include <graphics.h> void ffill( int x , int y , int fill , int boundary , int new , int old ) ; void main( ) { int fill , boundary , gd = DETECT , gm ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 27.
    initgraph (&gd ,&gm ,”tcbgi”) ; 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 [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 28.
    1. Define FloodFill Algorithm. 2. How many parameters are needed in Flood Fill Alorithm? 3. Define fillarea function. [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 29.
    EXPERIMENT NO. 08 AIM: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<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> main() { int gd=DETECT,gm; initgraph(&gd,&gm,"tcbgi"); 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? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 30.
  • 31.
    EXPERIMENT NO. 09 AIM: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<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> float x1,y1,r; main() { int gd=DETECT,gm; initgraph(&gd,&gm,"tcbgi"); 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 [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 32.
    1. What issetpallette()? 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<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> #define PI (22.0/7.0) main() { int gd=DETECT,gm,x1,y1,x2,y2,theta,xs,xc; initgraph(&gd,&gm,"c:tcbgi"); 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(); } [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 33.
    OUTPUT VIVA-–VOCE QUESTIONS 1.What isdifference between line() and lineto()? 2.Define setcolor. [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 34.
    EXPERIMENT NO. 11 AIM: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 <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 ,”tcbgi”) ; 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 ) ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 35.
    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 VIVA-QUESTIONS [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 36.
    1. Define CompositeTransformation. 2. What do you mean by DETECT? 3. What do you mean by End points? EXPERIMENT NO. 12 AIM :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 ( ) { [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 37.
    float t ; intgd = DETECT , gm ; int x1 ,y1, x4, y4, sx1, sy1 ; initgraph ( &gd , &gm , “tcbgi” ) ; 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 [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 38.
    INPUT Enter The ScalingVector :- 2 3 OUTPUT 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 [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 39.
    // (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 , “tcbgi” ) ; 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 ( ) ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 40.
    } INPUT Enter The FourCo-ordinate For Rectangle : 50 50 100 100 OUTPUT INPUT Enter 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 [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 41.
    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 , “tcbgi” ) ; 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 ) ) ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 42.
    rectangle ( x1, y1 , x4 , y4 ) ; getch ( ) ; closegraph ( ) ; } INPUT Enter Four Co-ordinates : - 50 50 110 110 OUTPUT INPUT Enter Angle For Rotation : - 30 OUTPUT VIVA- 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? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 43.
  • 44.
    EXPERIMENT NO. 13 AIM: 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) [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha A B C D E F G H I 100 1 000 1 010 1 010 0 000 0 100 0 101 0 001 0 011 0
  • 45.
    { 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 ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 46.
    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 ; } [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 47.
    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) [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 48.
    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. [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 49.
  • 50.
    EXPERIMENT NO. 14 AIM: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. [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha ( ) ( )∑= = d i d ii tBt 0 px ( ) ( ) idid i tt i d tB − −      = 1
  • 51.
  • 52.
    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 , “ tcbgi ” ) ; 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 ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 53.
    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 () ; } [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 54.
    OUTPUT VIVA-QUESTIONS 1. What isBezier Curve? 2. How many parameters are needed for Bezier Curve? 3. What is blendingValue? 4. What is the computeCoefficients? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 55.
    EXPERIMENT NO. 15 AIM: 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 Bézier 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 Bézier 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 Bézier 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; k<=n; k++) { b = SplineBlend(k,t,u,v); [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 56.
    output->x + =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] <= v) && (v < u[k+1])) value = 1; else value = 0; } else { if ((u[k+t-1] == u[k]) && (u[k+t] == u[k+1])) value = 0; else if (u[k+t-1] == u[k]) value = (u[k + t] - v) / (u[k+t] - u[k+1]) * SplineBlend(k+1,t-1,u,v); [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 57.
    else if (u[k+t]== u[k+1]) value = (v - u[k]) / (u[k+t-1] - u[k]) * SplineBlend(k,t-1,u,v); else value = (v - u[k]) / (u[k+t-1] - u[k]) * SplineBlend(k,t-1,u,v) + (u[k+t] - v) / (u[k+t] - u[k+1]) * SplineBlend(k+1,t-1,u,v); } return(value); } void SplineKnots(int *u,int n,int t) { int j; for (j=0;j<=n+t;j++) { if (j < t) u[j] = 0; else if (j <= n) u[j] = j - t + 1; else if (j > n) u[j] = n - t + 2; } } OUTPUT [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 58.
    VIVA-VOCE QUESTIONS 1. Whatis B-Spline Curve? 2. What is SplinePoint? 3. Define the SplineBlend? 4. What do you mean by SplineKnots? EXPERIMENT NO. 16 AIM :Write a program to implement Animation using C function. DESCRIPTION: A simulation of movement created by displaying a series of pictures, or frames. Cartoons on television is one example of animation. Animation on computers is one of the chief ingredients of multimedia presentations. There are many software applications that enable you to create animations that you can display on a computer monitors. The difference between animation and video. Whereas video takes continuous motion and breaks it up into discrete frames, animation starts with independent pictures and puts them together to form the illusion of continuous motion. PROGRAM #include < stdio.h > # include < conio.h > # include < graphicd.h > void main ( ) { int x , y , a , b , i ; int gd = DETECT , gm ; initgraph ( &gd ,&gm , “ tcbgi ” ) ; for ( i = 0 ; i < 320 ; i ++ ) { [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 59.
    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 [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 60.
    VIVA-VOCE QUESTIONS 1. Whatis 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. 17 AIM :Write a program to implement a cartoon using C function. DESCRIPTION : In this program ,we are going to create a cartoon. PROGRAM #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void main() { int gd = DETECT , gm, i, a ; clrscr( ) ; initgraph ( &gd, &gm, "c:tcbgi") ; [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 61.
    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 [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 62.
    VIVA-VOCE QUESTIONS 1. Definesetfillstyle function. 2. Define floodfill function. 3. Define delay function . 4. Why we used arc function? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 63.
    EXPERIMENT NO. 18 AIM:Write a program to draw a chain of Circle. DESCRIPTION : Here in this program , we are drawing a chain of circles. PROGRAM # include<graphics.h> # include<conio.h> # include<stdio.h> #include<math.h> void main() { int gd = DETECT, gm , i ; initgraph ( &gd, &gm, "c:tcbgi") ; for(i=50;i<=200;i=i+30) { circle(i,i+30,50); getch(); restorcrtmode(); } OUTPUT VIVA-VOCE QUESTIONS 1.Expain all the parameters of initgraph(). 2.What do you mean by DETECT? 3.What is restorcrtmode()? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 64.
    EXPERIMENT NO. 19 AIM:Write a program to display concentric circles. DESCRIPTION : Here in this program , we are drawing a concentric circles. PROGRAM #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int d=DETECT,m; initgraph(&d,&m,"c:tcbgi"); setcolor(4); for(i=25;i<150;i+=25) { circle(150,150,i); getch(); /*draw concentric circle of radius i pixels and center at 50,50,I in color 3*/ restorcrtmode(); } OUTPUT VIVA-VOCE QUESTION 1.Expain all the parameters of initgraph(). 2.What do you mean by DETECT? 3.What is restorcrtmode()? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 65.
    EXPERIMENT NO. 20 AIM:Write a program to fill an ellipse by reduce the size of ellipse. DESCRIPTION : PROGRAM #include<graphics.h> include<stdio.h> #include<conio.h> #include<math.h> elli_R(xc,yc,rx,ry) float xc,yc,rx,ry; { while(rx>0.0 && ry>0.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:tcbgi”); setpalette(0,23); for(i=0;i<=7;++i) { setcolor(1); getch(); ellipse(250,250,0,360,140,80); getch(); elli_R(250.0,250.0,140.0,80.0); } restorecrtmode(); return; } [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha
  • 66.
    OUTPUT VIVA-VOCE QUESTION 1. Defineellipse function. 2. What do you mean by setpalette function? 3. What is DETECT driver? 4. What is Graphics Mode? [Type text] CSE/6th /CG Lab/Prepared by Vivek Kumar Sinha