CG Lab Manual
CG Lab Manual
1
Sr.no. Title Remarks Signature
1 Program :-1
To draw basic shapes in computer graphics using C
2 Program :- 2
To draw concentric circle in computer graphics using
C
3 Program :- 3
To draw a line using bresenham's line drawing
algorithm in computer graphics using C
4 Program :- 4
To draw a circle using bresenham's line drawing
algorithm in computer graphics using C
5 Program :- 5
To draw a moving object by transferring in value of X
in computer graphics using C
2
Program :- 1
Aim :- To draw basic shapes in computer graphics using C .
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
Void main()
//Drawing rectangle
//Drawing circle
circle(x, y, radius);
//drawing a bar
//Drawing a line
//Drawing eclipse
getch();
closegraph();
3
4
Program :-2
Aim :- To draw concentric circle in computer graphics using C.
#include<conio.h>
#include<graphics.h>
Int main(){
intgd = DETECT, gm;
initgraph(&gd, &gm, "");
for (int i=0; i<10; i++)
{
setcolor(i);
circle(250, 250, 50+15*i);
}
getch();
closegraph();
return0;
}
5
6
Program :- 3
Aim :- To draw a line using bresenham's algorithm in computer graphics using
C.
#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
int main()
{
int gd=DETECT, gm, x0, y0, x1, y1;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}
7
8
Program :- 4
Aim :-To draw a circle using bresenham's algorithm in computer graphics
using C.
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main(){
int xc,yc,r,p,x,y;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
clrscr();
printf("\nEnter the co-ordinates of center : ");
scanf("%d %d",&xc,&yc);
printf("\nEnter the radius: ");
scanf("%d",&r);
x = 0;
y = r;
p=3-(2*r);
for(x=0;x<=y;x++)
{
if(p < 0)
{
p = p + (4 * x)+6;
}
else
{
y=y-1;
p = p +4 *(x-y)+10;
}
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
}
getch();
closegraph();
}
9
10
Experiment- 5
Creating various types of texts and fonts
DEFAULT_FONT,
1. Horizontal
TRIPLEX_FONT,
direction(HORIZ_DIR or 0)
SMALL_FONT,
SANS_SERIF_FONT, 2. Vertical direction(VERT_DIR
GOTHIC_FONT,
SCRIPT_FONT,
SIMPLEX_FONT,
TRIPLEX_SCR_FON
T, COMPLEX_FONT,
EUROPEAN_FONT,
11
16
12
Program:
#include<stdio.h
>
#include<conio.h
>
#include<graphi
cs.h> void
main()
{
int gd=DETECT,gm,x=25,y=25,font=10;
initgraph(&gd,&gm,"C:\\turboC3\\BGI");
for(font=0;font<=4;font++)
{
settextstyle(font,HORIZ_DIR,font+1);// sets font type, font
direction, size setcolor(font+1); // sets color for text.
outtextxy(x,y,"text with different fonts"); // prints message
on screen at (x,y) y=y+25;
}
for(font=0;font<=2;font++)
{
settextstyle(font,VERT_DI
R,font+2); setcolor(font+1);
x=25
0;
y=10
0;
outtextxy(x,y,"text in vertical
direction"); y=y+25;
}
getch();
closegraph()
;
}
Output:
13
14
Experiment- 6
Creating two dimensional objects
The following graphics functions are available for creating two dimensional shapes in C.
line
circle
ellipse
rectang
le
drawpo
ly
line: line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2)
are end points of the line.
Declaration: - line(x1, y1, x2, y2); ---line (100,200,300,400);
Circle: Circle function is used to draw a circle with center (x, y) and third parameter specifies the
radius of the circle.
Declaration: circle(x, y, r)—circle (100, 200, 25) ;( 25 is radius of circle, (100,100) is center of
circle).
Ellipse: Ellipse is used to draw an ellipse (x, y) are coordinates of center of the ellipse, startangle is
the starting angle, endangle is the ending angle, and fifth and sixth parameters specifies the X and Y
radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360
respectively.
Usage: ellipse(x, y, startangle, endangle, xradius, yradius);--ellipse(100,200,0,360,25,45);((100,200)
is center of ellipse, 0 is start angle, 360 is end angle, 25 is x-axis radius, 45 is radius circle).
Rectangle: rectangle function is used to draw a rectangle. Coordinates of left top and right bottom
corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top
specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom
corner, bottom specifies the Y-coordinate of right bottom corner.
Syntax: rectangle(left,top,right,bottom);--rectangle(100,200,300,400);
15
Drawpoly: Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon
etc.
Syntax: drawpoly( num,points );--num indicates number of vertices of polygon. Num = num+1.
Example: we will draw a triangle using drawpoly, consider for example the array :-
int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note
that last point(320, 150) in array is same as first.
Number of vertices are denoted by num. for any polygon, number of vertices are (num+1). For
triangle, number of vertices are 4.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(5);
rectangle(60,80,150,200);
rectangle(95,140,120,200);
line(60,80,100,15);
line(100,15,150,80);
circle(100,60,10);
getch();
closegraph();
}
Output:
16
ii) Program for creating simple car shape:
#include<stdio.h
>
#include<conio.
h>
#include<graphi
cs.h>
#include<dos.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); cleardevice();
line( 150, 100, 242, 100);
ellipse(242, 105, 0, 90, 10, 5);
line(150, 100, 120, 150);
line(252, 105, 280, 150);
line(100, 150, 320, 150);
line(100, 150, 100, 200);
line(320, 150, 320, 200);
line(100, 200, 110, 200);
line( 320, 200, 310, 200);
arc(130, 200, 0, 180, 20);
arc( 290, 200, 0, 180, 20);
line( 270, 200, 150, 200);
circle(130, 200, 17);
circle(290, 200,
17); getch();
}
17
Output:
18
iii)Program for creating fish:
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<ctype.h>
void
main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3
\\BGI"); cleardevice();
ellipse(520,200,30,330,90,30);
circle(450,193,3);
line(430,200,450,200);
line(597,185,630,170);
line(597,215,630,227);
line(630,170,630,227);
line(597,200,630,200);
line(597,192,630,187);
line(597,207,630,213);
line(500,190,540,150);
line(530,190,540,150);
getch();
}
Output:
19
iv) Program for creating man object:
#include<stdio.h
>
#include<graphi
cs.h>
#include<conio.
h> void main() int gd=DETECT,gm;
{ initgraph(&gd,&gm,"C:\\TurboC3\\BGI"
); setcolor(9);
circle(150,150,35);
line(150,185,150,300);
line(150,200,120,230);
line(150,200,180,230);
line(150,300,120,330);
line(150,300,180,330);
outtextxy(230,350,"HI, This is Computer
Graphics"); getch();
Output:
20
Sample program illustrating the use drawpoly() function:
Description:
Using drawpoly (), we can draw any polygon of any number of vertices.
Syntax: drawpoly (num, points);--num indicates number of vertices of polygon. Num = num+1.
Example: we will draw a triangle using drawpoly, consider for example the array:-
int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
1) Points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note
that last point (320, 150) in array is same as first.
2) Number of vertices is denoted by num. for any polygon, numbers of vertices are (num+1). For
triangle, number of vertices are 4.
Program:
#include
<graphics.h>
#include
<conio.h>
main
()
{ int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); drawpoly(4, points);
getch();
closegraph();
return 0;
}
Output:
21
Experiment 7
Two Dimensional Transformations
1. Translation: Translation is defined as moving the object from one position to another position along
straight line path.
We can move the objects based on translation distances along x and y axis. tx denotes
translation distance along x-axis and ty denotes translation distance along y axis.
Translation Distance: It is nothing but by how much units we should shift the object
from one location to another along x, y-axis.
Consider (x,y) are old coordinates of a point. Then the new coordinates of that same point (x’,y’)
can be
obtained as follows:
X’=x
+tx
Y’=y
+ty
We denote translation transformation as P. we express above equations in matrix form as:
x,y---old
22
coordinates
x’,y’—new
coordinates after
translation
tx,ty—translation distances, T is
23
2. Scaling: scaling refers to changing the size of the object either by increasing or decreasing. We will
increase or decrease the size of the object based on scaling factors along x and y-axis.
If (x, y) are old coordinates of object, then new coordinates of object after
applying scaling transformation are obtained as:
x’=x*
sx
y’=y*
sy.
sx and sy are scaling factors along x-axis and y-axis. we express the above equations in matrix
form as:
x sx 0 x
y 0 s y
y
Scaling Matrix
3. Rotation: A rotation repositions all points in an object along a circular path in the plane centered at the
pivot point. We rotate an object by an angle theta.
24
25
4. Reflection: Reflection is nothing but producing mirror image of an object. Reflection can be done just by
rotating the object about given axis of reflection with an angle of 180 degrees.
5. Shear:
1. Shear is the translation along an axis by an amount that increases linearly with another axis (Y). It
produces shape distortions as if objects were composed of layers that are caused to slide over each
other.
2. Shear transformations are very useful in creating italic letters and slanted letters from regular
letters.
3. Shear transformation changes the shape of the object to a slant position.
26
1. Program for translation:
#include<stdio.h
>
#include<conio.h
>
#include<graphi
cs.h>
#include<math.h
> void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI"
); printf("Enter the starting point of line
segment:"); scanf("%d %d",&x1,&y1);
printf("Enter the ending point of line
segment:"); scanf("%d %d",&x2,&y2);
printf("Enter translation distances
tx,ty:\n"); scanf("%d%d",&tx,&ty);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Origi
nal line"); x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=y2+ty;
setcolor(7);
line(x3,y3,x4
,y4);
outtextxy(x4+2,y4+2,"Line after
translation"); getch();
}
27
Output:
28
2. Program for
scaling: #include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,sx,sy,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after
scaling"); getch();
}
Output:
29
30
3. Program for Rotation:
#include<stdio.h
>
#include<conio.h
>
#include<graphi
cs.h>
#include<math.h
> void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,x3,y3,x4,y4,a,t;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI
"); printf("Enter coordinates of starting
point:\n"); scanf("%f%f",&x1,&y1);
printf("Enter coordinates of ending
point\n"); scanf("%f%f",&x2,&y2);
printf("Enter angle for
rotation\n"); scanf("%f",&a);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Origi
nal line"); t=a*(3.14/180);
x3=(x1*cos(t))-(y1*sin(t));
y3=(x1*sin(t))+(y1*cos(t));
x4=(x2*cos(t))-(y2*sin(t));
y4=(x2*sin(t))+(y2*c
os(t)); setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after
rotation"); getch();
}
Output:
31
32
4. Program for reflection along x-axis:
# include
<stdio.h> #
include
<conio.h>
# include
<graphics.h> #
include <math.h>
char IncFlag;
int PolygonPoints[3][2] ={{10,100},{110,100},{110,200}};
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,2
40);
line(320,0,320,4
80);
for (iCnt=0; iCnt<3; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);
}
}
void Reflect()
{
float
Angle; int
iCnt;
int Tx,Ty;
printf("endl");;
for (iCnt=0; iCnt<3; iCnt++)
PolygonPoints[iCnt][1] = (480 - PolygonPoints[iCnt][1]);
}
void main()
{
int gDriver = DETECT,
gMode; int iCnt;
initgraph(&gDriver, &gMode,
"C:\\TurboC3\\BGI"); for (iCnt=0;
iCnt<3; iCnt++)
33
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine()
; getch();
Reflect();
PolyLine()
; getch();
34
}
Output:
Object before reflection about x-axis
35
5. Program for Reflection about y-axis:
# include
<stdio.h> #
include
<conio.h>
# include
<graphics.h> #
include <math.h>
char IncFlag;
int PolygonPoints[3][2] =
{{10,100},{110,100},{110,200}};
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,2
40);
line(320,0,320,4
80);
for (iCnt=0; iCnt<3; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);
}
}
void Reflect()
{
float
Angle; int
iCnt;
int Tx,Ty;
for (iCnt=0; iCnt<3; iCnt++)
PolygonPoints[iCnt][0] = (640 - PolygonPoints[iCnt][0]);
}
void main()
{
int gd =
DETECT, gm;
int iCnt;
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); for
(iCnt=0; iCnt<3; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
36
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine()
; getch();
Reflect();
PolyLine()
; getch();
}
37
Output:
Object before reflection:
38
6. Program for X-
shear: #include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int
gd=DETECT,
gm; float
shx,shy;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI"
); printf("Enter shear factor shx along x-
axis :"); scanf("%f",&shx);
line(100,0,200,0);
line(200,0,200,200)
;
line(200,200,100,2
00);
line(100,200,100,0)
;
printf("X-shear");
setcolor(12);
line((100+(0*shx)),0,(200+(0*shx)),0);
line((200+(0*shx)),0,(200+(200*shx)),200);
line((200+(200*shx)),200,(100+(200*shx)),200);
line((100+(200*shx)),200,(100+(0*shx)),0);
getch();
}
39
In above output, red lined rectangle denotes object after x-shear transformation.
40
Experiment-08
Simple animations using transformations
Aim: To develop programs for making simple animations using transformations like
rotation, scaling and translation. The simple animations are given below:
i. Circle moving from left to right and vice versa
ii. Man object moving
iii. Wind mill rotation
iv. Man walking
v. Simple animation of football goal
Description: For moving any object, we incrementally calculate the object coordinates and redraw the
The below programs illustrate the movement of objects by using for loop
42
{
setcolor(3);
setfillstyle(SOLID_
FILL,9);
circle(i,50,50);
floodfill(i+2,52,3);
delay(20);
cleardevice();
}
//for moving circle from top to bottom,the following loop
works for(i=50;i<=getmaxy();i++)
{
setcolor(3);
setfillstyle(SOLID_
FILL,9);
circle(50,i,50);
floodfill(52,i+2,3);
delay(20);
cleardevice();
}
//for moving circle from bottom to top,the
following loop works for(i=getmaxy();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID_
FILL,9);
circle(50,i,50);
floodfill(52,i+2,3);
delay(20);
cleardevice();
}
//for moving circle in diagonal direction,the following loop
works for(i=50;i<=getmaxx();i++)
{
setcolor(3);
setfillstyle(SOLID_
FILL,9);
circle(i,i,50);
floodfill(i+2,i+2,3);
delay(20);
cleardevice();
}
//for moving circle in reverse diagonal direction,the
following loop works for(i=getmaxx();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID_
FILL,9);
43
circle(i,i,50);
floodfill(i+2,i+2,3);
delay(20);
cleardevice();
}
getch();
}
44
Output for moving circle:
45
ii. program for man object
moving: #include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h
> void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI
");
//creation of man object by using
circle,line. setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(50,50,30); // drawing
head floodfill(52,52,7);
setcolor(13);
line(50,80,50,200); //drawing
body line(50,110,20,140); //left
hand line(50,110,80,140);
//right hand
line(50,200,20,230); //left leg
line(50,200,80,230); //right leg
// for loop for moving man
for(i=50;i<=getmaxx();i++)
{
setcolor(7);
setfillstyle(SOLID_FILL,1
0); circle(i,50,30); //
drawing head
floodfill(i+2,52,7);
setcolor(13);
line(i,80,i,200); //drawing
body line(i,110,i-30,140);
//left hand
line(i,110,i+30,140);
//right hand line(i,200,i-
30,230); //left leg
line(i,200,i+30,230);
//right leg cleardevice();
delay(10);
}
//doing simple animation using
translation
for(i=50;i<=getmaxx()/2;i++)
{
setcolor(7);
setfillstyle(SOLID_FILL,1
0); circle(i,50,30); //
46
drawing head
floodfill(i+2,52,7);
setcolor(13);
line(i,80,i,200); //drawing
body line(i,110,i-30,140);
//left hand
line(i,110,i+30,140);
//right hand line(i,200,i-
30,230); //left leg
line(i,200,i+30,230);
//right leg cleardevice();
delay(10);
}
47
getch();
}
output for man moving:
48