Professional Documents
Culture Documents
1890362420CG Lab Record-1
1890362420CG Lab Record-1
INTRODUCTION
COMPONENTS OF VDU
1) A video screen on which we actually see the images either in text or graphics.
2) A video display adapter which is a special printed circuit board that plugs into
one expansion slot present on the motherboard of the computer. A video
display adapter is sometimes referred to as a video card.
A video display adapter acts as an agent between the processor and video screen.
The adapter consists of a special memory called VDU memory and circuitry which
transfer the contents of the video memory on the screen.
1
Department of CSE
CS010 806 Computer Graphics Lab
Text mode
Graphics mode
In text mode the video adapters provide built in support for ASCII characters
which are constructed by using pixels. In the graphics mode, the user has the freedom to
manipulate individual pixels on the screen, to form figures and text.
We get very sharp images on the screen if we use monitors having higher
resolution, i.e., monitors having more number of pixels. So it is essential to know the
resolution of the monitor one uses.
GRAPHIC FUNTIONS
To execute the graphic functions “graphic.h” library file should be included in the
C program. Some of the graphic functions are given below:
i) initgraph()
This function is used to initialize the graphics system and load the
appropriate specified graphic driver and the mode used by the graphic function.
The general form is
2
Department of CSE
CS010 806 Computer Graphics Lab
where gd – The video driver to be used. gd=DETECT is used to find the current
video driver in use.
path – Pathname is given if the drivers files are not present in the current
location.
ii) restorerectmode()
iii) closegraph()
This function de-allocates all the memory allocated by the graphic system.
iv) cleardevice()
This function clears the graphics screen and moves the current position to
upper left corner of the screen corresponding to point (0, 0)
v) putpixel(x, y, color)
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
3
Department of CSE
CS010 806 Computer Graphics Lab
LIGHT GRAY 7
DARK GRAY 8
LIGHT BLUE 9
LIGHT GREEN 10
LIGHT CYAN 11
LIGHT RED 12
LIGHT MAGENTA 13
YELLOW 14
WHITE 15
vi) getpixel(x, y)
This function is used to get the color value of the pixel (x, y)
vii) getmaxx()
viii) getmaxy()
ix) getx()
x) gety()
This function draws a line between two specified points (x1, y1) and (x2,
y2)
4
Department of CSE
CS010 806 Computer Graphics Lab
This function draws a rectangle from (left, top) point to (right, bottom)
point
This function draws an arc at the center (x, y) with specified starting
angle, ending angle and radius
xvii) setcolor(color)
xviii) getcolor()
5
Department of CSE
CS010 806 Computer Graphics Lab
CYCLE – I
FAMILIARIZATION OF GRAPHIC
ALGORITHMS
6
Department of CSE
CS010 806 Computer Graphics Lab
Exp 1:
DATE:
AIM
To study various graphic primitives
ALGORITHM
Step 1: Start
Step 2: Initialize graphic system
Step 3: Display the menu listing Circle, Rectangle, Ellipse, Arc, Line and Polygon
Step 4: If choice is for Circle
i) Read the center and radius
ii) Draw circle using circle(x, y, radius)
Step 5: If choice is for Rectangle
i) Read the coordinates (left, top) and (right, bottom)
ii) Draw rectangle using rectangle(left, top, right, bottom)
Step 6: If choice is for Ellipse
i) Read the center (x, y)
ii) Read the starting angle and ending angle
iii) Read radius in x-direction and y-direction
iv) Draw ellipse using ellipse(x, y, stang, endang, xrad, yrad)
Step 7: If choice is for Arc
i) Read the center (x, y)
ii) Read the starting angle and ending angle
iii) Read radius
iv) Draw arc using arc(x, y, stang, endang, radius)
Step 8: If choice is for Line
i) Read the coordinates of first point and second points
ii) Draw the line using line (x1, y1, x2, y2)
Step 9: If choice is for Polygon
i) Read the number of coordinates and each coordinate points
ii) Draw polygon using drawpoly(no._of_coordinates, coordinate_array)
Step 10: Stop
7
Department of CSE
CS010 806 Computer Graphics Lab
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
#include<process.h>
void main()
{
int gd=0,gm,x1,y1,r,x2,y2,ch,stang,endang;
int xrad,yrad,n,a[50],i;
int opt;
initgraph(&gd,&gm,"");
do
{
gotoxy(1,1);
cleardevice();
printf("\n1. Circle\n2. Rectangle\n3. Ellipse\n4. Arc \n5. Line\n6.
Polygon\n7.EXIT\n");
printf("\n Enter your choice :");
scanf("%d", &ch);
switch(ch)
{
case 1://CIRCLE
printf("\n Enter the coordinates of center :");
scanf("%d%d", &x1,&y1);
printf("\nEnter the radius :");
scanf("%d",&r);
circle(x1,y1,r);
getch();
break;
case 2://RECTANGLE
printf("\nEnter the first coordinates :");
scanf("%d%d",&x1,&y1);
printf("Enter the second coordinates :");
scanf("%d%d",&x2,&y2);
rectangle(x1,y1,x2,y2);
getch();
break;
case 3://ELLIPSE
printf("\nEnter the center coordinates :");
scanf("%d%d", &x1,&y1);
printf("Enter the start angle and end angle :");
scanf("%d%d",&stang,&endang);
printf("Enter the xradius and yradius :");
8
Department of CSE
CS010 806 Computer Graphics Lab
scanf("%d%d",&xrad,&yrad);
ellipse(x1,y1,stang,endang,xrad,yrad);
getch();
break;
case 4://ARC
printf("\nEnter the center coordinates :");
scanf("%d%d", &x1,&y1);
printf("Enter the start angle and end angle :");
scanf("%d%d",&stang,&endang);
printf("Enter the radius:");
scanf("%d",&xrad);
arc(x1,y1,stang,endang,xrad);
getch();
break;
case 5://LINE
printf("Enter the first coordinates:");
scanf("%d%d",&x1,&y1);
printf("Enter the second coordinates:");
scanf("%d%d",&x2,&y2);
line(x1,y1,x2,y2);
getch();
break;
case 6://POLYGON
printf("\nEnter the no. of sides of polygon:");
scanf("%d",&n);
printf("Enter the coordinates :");
for(i=0;i<(n+1)*2;i++)
scanf("%d",&a[i]);
drawpoly(n+1,a);
getch();
break;
default:printf("\n**** THANK YOU ****");
getch();
exit(0);
}
}while(ch<7);
getch();
closegraph();
}
9
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
10
Department of CSE
CS010 806 Computer Graphics Lab
11
Department of CSE
CS010 806 Computer Graphics Lab
RESULT
12
Department of CSE
CS010 806 Computer Graphics Lab
Exp 2:
Date:
DDA LINE DRAWING ALGORITHM
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize graphic sytem
Step 3: Read the end points of line (x1, y1) and (x2, y2)
Step 4: Find dx = x2-x1 and dy = y2-y1
Step 5: If dx==0 and dy==0, plot the point (x1, y1)
Step 6: Else if dx>=dy, step = dx
Step 7: Else step = dy
Step 8: Find xinr = dx/step and yinr = dy/step
Step 9: Repeat the following steps in „step‟ times
i) plot pixel (x1, y1)
ii) find x1 = x1+xinr
iii) find y1 = y1+yinr
Step 10: Stop
PROGRAM
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
void main()
{
float i,x1,y1,x2,y2,dx,dy,x,y,step,xinr,yinr;
int gd=0,gm;
initgraph(&gd,&gm,"");
printf("Enter the end values:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(dx==0&&dy==0)
{
putpixel(x1,y1,15);
getch();
exit(0);
13
Department of CSE
CS010 806 Computer Graphics Lab
if(abs(dx)>=abs(dy))
step=abs(dx);
else
step=abs(dy);
xinr=dx/step;
yinr=dy/step;
x=x1;y=y1;
for(i=1;i<=step;i++)
{
putpixel(x,y,15);
x=x+xinr;
y=y+yinr;
}
getch();
closegraph();
}
OUTPUT
14
Department of CSE
CS010 806 Computer Graphics Lab
RESULT
15
Department of CSE
CS010 806 Computer Graphics Lab
Exp 3:
Date:
AIM
To draw a line on the screen using Bresenham‟s line drawing algorithm
ALGORITHM
Step 1: Start
Step 2: Initialize graphic system
Step 3: Read the end points (x1, y1) and (x2, y2)
Step 4: Calculate dx= x2-x1 and dy=y2-y1
Step 5: If dx>dy,
i) Calculate p = 2*dy-dx
ii) If x1>x2, xend x1 else xend x2
Step 6: Repeat the following xend times
i) x x+1
ii) If p<0, p p+2*dy
iii) Else, y y+/-1 and p p+2*(dy-dx)
iv) Plot the point (x, y)
Step 7: If dx<dy, Repeat the step 5 and step 6 interchanging x and y.
Step 8: Stop
PROGRAM
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
void main()
{
float x1,y1,x2,y2,x,y,xend,yend,dx,dy,p;
int gd=0,gm,f=0;
initgraph(&gd,&gm,"");
gotoxy(1,1);
printf("Enter the coordinates of line:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dy<dx) // if m<1
{
p=2*dy-dx;
if(x1>x2)
{
16
Department of CSE
CS010 806 Computer Graphics Lab
x=x2;
y=y2;
xend=x1;
if(y1<y2)
f=1;
}
else
{
x=x1;
y=y1;
xend=x2;
if(y1>y2)
f=1;
}
putpixel(x,y,15);
while(x<xend)
{
x=x+1;
if(p<0)
p=p+2*dy;
else
{
if(f==1)
y=y-1;
else
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,15);
}
}
else // if m>1
{
p=2*dx-dy;
if(y1>y2)
{
x=x2;
y=y2;
yend=y1;
if(x1<x2)
f=1;
}
else
{
x=x1;
y=y1;
17
Department of CSE
CS010 806 Computer Graphics Lab
yend=y2;
if(x1>x2)
f=1;
}
putpixel(x,y,15);
while(y<yend)
{
y=y+1;
if(p<0)
p=p+2*dx;
else
{
if(f==1)
x=x-1;
else
x=x+1;
p=p+2*(dx-dy);
}
putpixel(x,y,15);
}
}
getch();
closegraph();
}
OUTPUT
18
Department of CSE
CS010 806 Computer Graphics Lab
RESULT
19
Department of CSE
CS010 806 Computer Graphics Lab
Exp 4:
Date:
AIM
To draw a circle using bresenham‟s circle drawing algorithm.
ALGORITHM
Step 1: Start
Step 2: Initialize graphic system
Step 3: Read the center point (x1, y1) and radius r
Step 4: Initialize x 0 and y r
Step 5: Set p 3-(2*r)
Step 6: Repeat the following r times
i) Plot the point (x1+x, y1+y)
ii) Plot the point (x1-x, y1+y)
iii) Plot the point (x1+x, y1-y)
iv) Plot the point (x1-x, y1-y)
v) Plot the point (x1+y, y1+x)
vi) Plot the point (x1+y, y1-x)
vii) Plot the point (x1-y, y1+x)
viii) Plot the point (x1-y, y1-x)
ix) x x+1
x) If p<0, set p p+4*x+6
xi) Else set p p+4*(x-y)+10 and y y-1
Step 7: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,r,p;
int gd=0,gm;
clrscr();
initgraph(&gd,&gm,"");
printf("Enter the coordinates of center\n\t");
scanf("%d%d",&x1,&y1);
printf("Enter the value of radius\n\t");
scanf("%d",&r);
x=0;
20
Department of CSE
CS010 806 Computer Graphics Lab
y=r;
p=3-(2*r);
while(x<=y)
{
putpixel(x1+x,y1+y,WHITE);
putpixel(x1-x,y1+y,WHITE);
putpixel(x1+x,y1-y,WHITE);
putpixel(x1-x,y1-y,WHITE);
putpixel(x1+y,y1+x,WHITE);
putpixel(x1+y,y1-x,WHITE);
putpixel(x1-y,y1+x,WHITE);
putpixel(x1-y,y1-x,WHITE);
x=x+1;
if(p<0)
p=p+4*(x)+6;
else
{
p=p+4*(x-y)+10;
y=y-1;
}
}
getch();
}
OUTPUT
RESULT
Exp 5:
Date:
MID POINT CIRCLE DRAWING ALGORITHM
AIM
To draw a circle using Mid Point circle drawing algorithm.
ALGORITHM
Step 1: Start
Step 2: Input radius r and circle center (Xc , Yc ) from the user.
Step 3: Plot first set of points on the circumference of a circle centered at (Xc , Yc ).
(Xc , Yc + r)
(Xc , Yc - r)
(Xc + r , Yc )
(Xc - r , Yc )
Step 4: Find the initial value of the decision parameter P0 = (5/4) - r
Step 5: If Pk < 0, the subsequent point along the circle centered at (0,0) is Yk + 1 = Xk + 1
Pk + 1= Pk+( 2*X-(2*y)+1)
Step 6: Otherwise the subsequent point along the circle is
Xk+1 = Yk +1
Yk+1 = Yk -1
and Pk+1= Pk+2Xk+1-2Yk+1
where 2Xk+1=2Xk+1 & 2Yk+1= 2Yk-2
Step 6: Find the symmetric points in all other octants
Step 7: Plot the above calculated pixel (x,y) on to the circle. Path of the circle centered at
(Xc,Yc) and plot the cordinates
Step 9: Stop
22
Department of CSE
CS010 806 Computer Graphics Lab
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void pixel(int xc,int yc,int x,int y);
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,pk;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the coordinates of center\n\t");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius\n\t");
scanf("%d",&r);
x=0;
y=r;
pk=1-r;
pixel(xc,yc,x,y);
while(x<y)
{
if(pk<0)
{
x=x+1;
pk=pk+(2*x)+1;
}
else
{
x=x+1;
y=y-1;
pk=pk+(2*x)-(2*y)+1;
}
pixel(xc,yc,x,y);
getch();
closegraph();
}
{
putpixel(xc+x,yc+y,7);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,7);
putpixel(xc-x,yc+y,7);
23
Department of CSE
CS010 806 Computer Graphics Lab
putpixel(xc-x,yc-y,7);
putpixel(xc+y,yc-x,7);
putpixel(xc+x,yc-y,7);
OUTPUT
RESULT
24
Department of CSE
CS010 806 Computer Graphics Lab
Exp 6:
Date:
2D TRANSFORMATION
6.1) TRANSLATION
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize graphic system
Step 3: Read the end points of line (x1, y1) and (x2, y2)
Step 4: Read the translation vector (tx, ty)
Step 5: Set x1 x1+tx, y1y1+ty, x2x2+tx and y2y2+ty
Step 6: Draw the line using line(x1, y1, x2, y2)
Step 7: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2,tx,ty;
initgraph(&gd,&gm,"");
printf("\n\t\t TRANSLATION OF A LINE");
printf("\n\tEnter the first coordinate of a line\n\t");
scanf("%d%d",&x1,&y1);
printf("\n\tEnter the second coordinate of a line\n\t");
scanf("%d%d",&x2,&y2);
line(x1,y1,x2,y2);
printf("\n\tEnter the translation vectors\n\t");
scanf("%d%d",&tx,&ty);
setcolor(RED);
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
line(x1,y1,x2,y2);
getch();
}
25
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
RESULT
26
Department of CSE
CS010 806 Computer Graphics Lab
6.2) ROTATION
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize graphic system
Step 3: Read the end points of line (x1, y1) and (x2, y2)
Step 4: Read the angle of rotation, ang
Step 5: Set angle ang*3.14/180
Step 6: Set x4x2-(((x2-x1)*cos(angle))-((y2-y1)*sin(angle))) and
y4y2-(((x2-x1)*sin(angle))+((y2-y1)*cos(angle)))
Step 7: Draw the line using line(x2, y2, x4, y4)
Step 8: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x4,y4;
float angle=0,ang;
clrscr();
initgraph(&gd,&gm,"");
printf("\nROTATION OF A LINE\n");
printf("\nEnter the first coordiantes\n\t");
scanf("%d%d",&x1,&y1);
printf("\nEnter the second coordinates\n\t");
scanf("%d%d",&x2,&y2);
line(x1,y1,x2,y2);
printf("\nEnter the angle\n");
scanf("%f",&ang);
angle=(ang*3.14)/180;
setcolor(RED);
x4=x2-(((x2-x1)*cos(angle))-((y2-y1)*sin(angle)));
y4=y2-(((x2-x1)*sin(angle))+((y2-y1)*cos(angle)));
line(x2,y2,x4,y4);
getch();
}
27
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
RESULT
28
Department of CSE
CS010 806 Computer Graphics Lab
6.3) SCALING
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize the graphic system
Step 3: Read the end points of a line (x1, y1) and (x2, y2)
Step 4: Read the scaling factor (sx, sy)
Step 5: Set x1x1*sx, y1y1*sy, x2x2*sx, and y2y2*sy
Step 4: Draw the line using line(x1, y1, x2, y2)
Step 5: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,sx,sy;
initgraph(&gd,&gm,"");
printf("\n\tSCALING OF A LINE\n");
printf("\nEnter the first coordiantes\n\t");
scanf("%f%f",&x1,&y1);
printf("\nEnter the second coordinates\n\t");
scanf("%f%f",&x2,&y2);
line(x1,y1,x2,y2);
printf("\nEnter the scaling factors\n");
scanf("%f%f",&sx,&sy);
setcolor(RED);
x1=x1*sx;
y1=y1*sy;
x2=x2*sx;
y2=y2*sy;
line(x1,y1,x2,y2);
getch();
}
29
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
RESULT
30
Department of CSE
CS010 806 Computer Graphics Lab
6.4) SHEARING
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize the graphic system
Step 3: Read the four coordinates of polygon and shearing parameters (shx, shy)
Step 3: Display the menu
Step 4: If the choice is for shearing about x-axis
i) Set px1x1+shx*y1, py1y1, px2x2+shx*y2, py2y2
ii) Draw four lines using line(px1, py1, px2, py2), line(px2,py2,x3,y3),
line(x3,y3,x4,y4), line(x4,y4,px1,py1)
Step 5: If the choice is for shearing about y-axis
i) Set px3x2, py3y2+shy*x2, px4x2, py4y3+shy*x3
ii) Draw four lines using line(x1,y1,px3,py3), line(px3,py3,px4,py4),
line(px4,py4,x4,y4), line(x4,y4,x1,y1)
Step 6: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3,x4,y4,shx,shy,ch;
float px1,py1,px2,py2,px3,py3,px4,py4;
initgraph(&gd,&gm,"");
do
{
cleardevice();
gotoxy(1,1);
printf("\n\tCHOICE\n\n1.Shearing about x axis\n2.Shearing about y
axis\n3.Exit\nEnter your choice\n");
scanf("%d",&ch);
if((ch==2)||(ch==1))
{
printf("\nEnter the value of x1,y1,x2,y2,x3,y3,x4,y4\n");
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
printf("\nEnter the shearing parameters\n");
31
Department of CSE
CS010 806 Computer Graphics Lab
scanf("%d%d",&shx,­);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
line(x4,y4,x1,y1);
}
switch(ch)
{
case 1:
px1=x1+shx*y1;
py1=y1;
px2=x2+shx*y2;
py2=y2;
line(px1,py1,px2,py2);
line(px2,py2,x3,y3);
line(x3,y3,x4,y4);
line(x4,y4,px1,py1);
getch();
break;
case 2:
px3=x2;
py3=y2+shy*x2;
px4=x2;
py4=y3+shy*x3;
line(x1,y1,px3,py3);
line(px3,py3,px4,py4);
line(px4,py4,x4,y4);
line(x4,y4,x1,y1);
getch();
break;
case 3:
printf("\n\t\t THANK U!!!\n");
delay(200);
exit(0);
break;
default:
printf("\n Wrong choice");
delay(200);
break;
}
}while(ch<4);
getch();
}
OUTPUT
32
Department of CSE
CS010 806 Computer Graphics Lab
RESULT
33
Department of CSE
CS010 806 Computer Graphics Lab
6.5) REFLECTION
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize the graphic system
Step 3: Read the coordinates of a triangle
Step 4: Draw the reflection axis and triangle.
Step 5: Display the menu
Step 6: If the choice is for x-axis
i) Set y1getmaxy()-y1, y2getmaxy()-y2, y3getmaxy()-y3
ii) Draw the lines using line(x1,y1,x2,y2), line(x2,y2,x3,y3),
line(x3,y3,x1,y1)
Step 7: If the choice is for y-axis
i) Set x1getmaxx()-x1, x2getmaxx()-x2, x3getmaxx()-x3
ii) Draw the lines using line(x1,y1,x2,y2), line(x2,y2,x3,y3),
line(x3,y3,x1,y1)
Step 8: If the choice is for origin
i) Set x1getmaxx()-x1, x2getmaxx()-x2, x3getmaxx()-x3,
y1getmaxy()-y1, y2getmaxy()-y2, y3getmaxy()-y3
ii) Draw the lines using line(x1,y1,x2,y2), line(x2,y2,x3,y3),
line(x3,y3,x1,y1)
Step9: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
int ch;
clrscr();
initgraph(&gd,&gm,"");
do{
cleardevice();
gotoxy(1,1);
if((ch<4)&&(ch>0))
{
printf("\nEnter the value of x1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
switch(ch)
{
case 1:
y1=getmaxy()-y1;
y2=getmaxy()-y2;
y3=getmaxy()-y3;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
break;
case 2:
x1=getmaxx()-x1;
x2=getmaxx()-x2;
x3=getmaxx()-x3;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
break;
case 3:
x1=getmaxx()-x1;
x2=getmaxx()-x2;
x3=getmaxx()-x3;
y1=getmaxy()-y1;
y2=getmaxy()-y2;
y3=getmaxy()-y3;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
break;
case 4:
printf("\n\t\t THANK U!!!!\n");
delay(200);
35
Department of CSE
CS010 806 Computer Graphics Lab
break;
default:
printf("\n\n WRONG CHOICE\n");
delay(200);
break;
}
}while(ch!=4);
getch();
}
OUTPUT
36
Department of CSE
CS010 806 Computer Graphics Lab
37
Department of CSE
CS010 806 Computer Graphics Lab
RESULT
CYCLE – II
39
Department of CSE
CS010 806 Computer Graphics Lab
Exp 7
Date:
3D ROTATION ON A CUBE
AIM
ALGORITHM
Step 1: Start
Step 2: Set maxx ← getmaxx
maxy ← getmaxy
midx ← maxx/2
midy ← maxy/2
Step 3: Draw the cube using bar3d
Step 3: Read the rotation angle
Step 4: Calculate x1, y1, x2, y2
Step 5: Read the sides of the cube and store into array
Step 6: Draw the cube using line functions
Step 7: Calculate a1 ← 3.14 * 60/180
a2 ← 3.14 * 60/180
Step 8: Apply rotation to the coordinates
Step 9: Draw the cube using line function
Step 6: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy,o;
void axis()
{
getch();
cleardevice();
line(midx,o,midx,maxy);
line(o,midy,maxx,midy);
}
void main()
{
int gd=0,gm,x,y,z,o,x1,x2,y1,y2,ch;
//detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");
maxx=getmaxx();
40
Department of CSE
CS010 806 Computer Graphics Lab
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("\n\tEnter rotating angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)*sin(0*3.14/180);
y1=50*cos(o*3.14/180)+100*sin(0*3.14/180);
x2=60*sin(o*3.14/180)-90*cos(0*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(0*3.14/180);
while(1)
{
printf("\nMenu\n1.Rotation about z axis\n2.Rotation about x axis\n3.Rotation about y
axis\n4.Exit");
printf("Enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Rotation about z axis");
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("After rotation about z axis");
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
break;
case 2:
printf("Rotation about x axis");
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("After rotation about x axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
break;
case 3:
printf("Rotation about y axis");
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("After rotation about y axis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
break;
case 4:
exit(0);
}
getch();
}
41
Department of CSE
CS010 806 Computer Graphics Lab
closegraph();
}
OUTPUT
42
Department of CSE
CS010 806 Computer Graphics Lab
RESULT
43
Department of CSE
CS010 806 Computer Graphics Lab
Exp 8:
Date:
2D COMPOSITE TRANSFORMATIONS
AIM:
To Perform 2D Composite Transformation
ALGORITHM
Step 1: Start
Start 2: Read the number of coordinates ,n
Step 3: read (n+1) *2 coordinates.
Step 4: Draw polygon using drawpoly(n+1,pt)
Step 5: Call function trans( ).
Step 6: Stop
Function trans( )
Step 1: Start
Step 2: Read the translation vector tx , ty
Step 3: for i=0 to (n+1)*2 coordinates do
(i) t[i]=pt[i] +tx
(ii) t[i=1]=pt[i+1]+ty
Step 4: Trace the line polygon
Step 5: call function Scale(int ref[])
Function inverse( )
Step 1: Set tx=0, ty=150 do
Step 2: For i=0 to (n+1)*2 do
(i) in[i]=s[i]-tx;
(ii) in[i+1]=s[i+1]-ty;
Step3: Draw the polygon
Step 4: Stop
44
Department of CSE
CS010 806 Computer Graphics Lab
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int i,pt[20],t[20],s[20],in[20],n;
int refpt[2]={100,100};
void trans();
void scale(int *);
void invtrans();
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the no: of coordinates");
scanf("%d",&n);
printf("Coordinates are");
for(i=0;i<n;i++)
scanf("%d",&pt[i]);
for(i=0;i<n;i++)
{
drawpoly(n+1,pt);
}
getch();
trans();
closegraph();
}
void trans()
{
int i,tx,ty;
printf("Translation\n");
printf("Translation Vectors");
scanf("%d%d",&tx,&ty);
for(i=0;i<2*(n+1);i=i+2)
{
t[i]=pt[i]+tx;
t[i+1]=pt[i+1]+ty;
}
drawpoly(n+1,t);
getch();
scale(refpt);
}
void scale(int ref[])
{
45
Department of CSE
CS010 806 Computer Graphics Lab
int i;
int sx,sy;
printf("\tScaling\n");
printf("Sacling Vectors");
scanf("%d%d",&sx,&sy);
for(i=0;i<2*(n+1);i=i+2)
{
s[i]=ref[0]+(t[i]-ref[0])*sx;
s[i+1]=ref[1]+(t[i+1]-ref[1])*sy;
}
drawpoly(n+1,s);
getch();
invtrans();
cleardevice();
}
void invtrans()
{
int i,tx=0,ty=150;
printf("\n\n\nInverse translation");
for(i=0;i<2*(n+1);i=i+2)
{
in[i]=s[i]-tx;
in[i+1]=s[i+1]-ty;
}
drawpoly(n+1,in);
getch();
}
OUTPUT
46
Department of CSE
CS010 806 Computer Graphics Lab
47
Department of CSE
CS010 806 Computer Graphics Lab
RESULT
48
Department of CSE
CS010 806 Computer Graphics Lab
EXP 9:
Date:
BEZIER CURVE
AIM:
ALGORITHM
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void bezier(int x[4],int y[4])
{
int gd=DETECT,gm;
int i;
double t;
initgraph(&gd,&gm,"...\\bgi");
for(t=0.0;t<1.0;t+=0.0005)
{
double xt=pow(1-t,3)*x[0]+3*t*pow(1-t,2)*x[1]+3*pow(t,2)*(1-t)*x[2]+pow(t,3)*x[3];
double yt=pow(1-t,3)*y[0]+3*t*pow(1-t,2)*y[1]+3*pow(t,2)*(1-t)*y[2]+pow(t,3)*y[3];
putpixel(xt,yt,WHITE);
}
for(i=0;i<4;i++)
putpixel(x[i],y[i],YELLOW);
getch();
closegraph();
return;
}
void main()
{
int x[4],y[4];
int i;
printf("\nEnter the x and y coordinates of the four control points\n");
for(i=0;i<4;i++)
scanf("%d%d",&x[i],&y[i]);
bezier(x,y);
}
49
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
RESULT
50
Department of CSE
CS010 806 Computer Graphics Lab
Exp 10:
Date:
FRACTAL CONSTRUCTION
AIM
To generate Fractal images.
ALGORITHM
Step 1: Start
Step 2: The triangle is created by infinite removals.
Step 3: Each triangle is divided into 4 smaller upside down triangles.
Step 3: The center of the triangle is removed.
Step 4: As the process is iterated infinite number of times, total area of the set goes to
infinity as the size of the new triangle goes to zero.
Step 5: After closer examination magnification factor is 2.
Step 6: With each magnification these are 3 division of a triangle dimension
D = In(3) / In(2)
D = 1.5850
Step 6: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
void DrawSierpinski(void);
void main(void)
{
int gd=VGA;
int gm=VGAHI;
initgraph(&gd,&gm,"\\tc\\bgi");
DrawSierpinski();
getch();
}
void DrawSierpinski(void)
{
char Direct;
int iterate;
unsigned int x1,y1,x2,y2;
x1=x2=320;
y1=y2=0;
for(iterate=0;iterate<10000;iterate++)
51
Department of CSE
CS010 806 Computer Graphics Lab
{
Direct=random(3);
if(Direct==0)
{
x1=(x2+320)/2;
y1=(y2+0)/2;
}
else if(Direct==1)
{
x1=(x2+0)/2;
y1=(y2+480)/2;
}
else if(Direct==2)
{
x1=(x2+640)/2;
y1=(y2+480)/2;
}
putpixel(x1,y1,WHITE);
x2=x1;
y2=y1;
}
}
OUTPUT
RESULT
52
Department of CSE
CS010 806 Computer Graphics Lab
CYCLE – III
ANIMATIONS
53
Department of CSE
CS010 806 Computer Graphics Lab
Exp 11:
Date:
BUCKET FILLING
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize graphic system
Step 3: Draw pipe using line functions
Step 4: Draw bucket using line functions
Step 5: Repeat for i= 155 to 296
i) draw circle with the center (278,i) and radius 2
Step 6: Repeat for j=298 to 198
i) Set i190
ii) Increment the value of i until the color of pixel (i, j) become WHITE
iii) Set ii+1
iv) Set x1i
v) Increment the value of i until the color of pixel (i, j) become WHITE
vi) Set ii-1
vii) Set x2i
viii) Draw the line using line(x1,j,x2,j)
ix) Call delay of 75ms
Step 7: Show the overflow of bucket with line functions
Step 8: Stop
PROGRAM
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
int gdriver=0,gmode;
int i,j,x1,x2;
initgraph(&gdriver,&gmode,"");
setfillstyle(1,7);
rectangle(0,301,400,485);
floodfill(5,400,15);
line(180,0,180,301);
54
Department of CSE
CS010 806 Computer Graphics Lab
line(180,143,282,143); //PIPE
line(180,150,275,150);
line(282,143,282,155);
line(275,150,275,155);
line(275,135,275,143);
line(271,135,271,143);
setfillstyle(1,WHITE);
fillellipse(273,135,6,2);
line(200,200,250,300); //BUCKET
line(250,300,300,300);
line(300,300,350,200);
setcolor(1);
for(i=155;i<=296;i++)
{
circle(278,i,2);
delay(20);
}
line(200,200,200,300); //OVERFLOW
line(350,200,350,300);
delay(250);
line(200,200,199,300);
line(350,200,351,300);
delay(250);
line(200,200,198,300);
line(350,200,352,300);
getch();
closegraph();
}
55
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
RESULT
Exp 12:
Date:
HATCHING OF AN EGG
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize the graphic system
Step 3: Draw an egg using sector functions with WHITE color
Step 4: Display the breaking using line functions and delay function.
Step 5: Show the broken pieces of egg using sector
Step 6: Draw the chicken using fillellipse, line etc. functions.
Step 7: Stop
PROGRAM
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
void main()
{
int gdriver=0,gmode;
initgraph(&gdriver,&gmode,"");
setbkcolor(0);
sector(200,200,90,280,40,37);
sector(200,200,270,360,65,37);
sector(200,200,0,90,65,37);
getch();
setcolor(0);
circle(225,175,1);
sleep(1);
circle(225,175,2);
delay(250);
line(225,175,200,163);
line(225,175,200,200);
delay(100);
line(225,175,215,220);
line(225,175,225,170);
57
Department of CSE
CS010 806 Computer Graphics Lab
delay(350);
line(215,220,240,240);
delay(200);
setfillstyle(1,0);
floodfill(230,180,0);
setcolor(15);
setfillstyle(1,15);
sector(250,200,280,360,65,37);
line(245,247,235,300);
line(235,300,251,315);
line(251,315,245,247);
floodfill(240,300,15);
setcolor(14);
setfillstyle(1,14);
fillellipse(275,150,40,30);
fillellipse(300,135,17,20);
setcolor(0);
pieslice(272,173,265,275,8);
pieslice(285,175,265,275,5);
setfillstyle(1,0);
sector(320,135,140,190,5,5);
circle(307,125,1);
sleep(1);
setcolor(14);
setfillstyle(1,14);
fillellipse(300,130,17,20);
setcolor(0);
circle(299,124,1);
circle(310,124,1);
putpixel(299,124,0);
putpixel(310,124,0);
setfillstyle(1,0);
sector(305,137,65,115,5,5);
getch();
closegraph();
}
58
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
RESULT
59
Department of CSE
CS010 806 Computer Graphics Lab
Exp 13:
Date:
BALL BOUNCING
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize the graphic system and other variables
Step 3: Set flag0
Step 4: Repeat until any key is pressed
i) set drawing color to RED using setcolor()
ii) Draw a line representing the floor
iii) If flag=0
a. yy+2
b. xx+1
c. if y>=385, set flag1
iv) if flag=1
a. yy-2
b. xx+1
c. if y<=upperlimit, set flag0, upperlimitupperlimit+20
v) draw the ball using fillellipse function
Step 5: Stop
PROGRAM
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=0,gm,x=20,flag=0,y=250,uplimit=250;
initgraph(&gd,&gm,"");
while(!kbhit())
{
setcolor(4);
line(0,400,679,400);
if(flag==0)
{
y+=2;
x+=1;
if(y>=385)
flag=1;
}
if(flag==1)
60
Department of CSE
CS010 806 Computer Graphics Lab
{
y-=2;
x+=1;
if(y<=uplimit)
{
flag=0;
uplimit+=20;
}
}
setcolor(15);
fillellipse(x,y,15,15);
delay(15);
setcolor(0);
setfillstyle(1,0);
fillellipse(x,y,15,15);
}
getch();
}
OUTPUT
RESULT
61
Department of CSE
CS010 806 Computer Graphics Lab
Exp 14:
Date:
ALGORITHM
Step 1: Start
Step 2: Initialize the graphic system
Step 3: Set k0
Step 4: Repeat while no key is pressed
i) rain(0, k)
ii) man(k)
iii) kk+1
iv) rain(3, k)
v) man(k)
vi) kk+1
vii) rain(15, k)
viii) man(k)
ix) kk+1
x) Clear the screen
Step 5: Stop
Function rain(m, x)
Function man(x)
Step 1: Draw the man at correct place using line , circle and sector functions
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
62
Department of CSE
CS010 806 Computer Graphics Lab
void man(int x)
{
int i,j;
circle(x+15,350,14);
line(x+15,364,x+15,424);
line(x+15,424,x+5,getmaxy());
line(x+15,424,x+25,getmaxy());
line(x+15,384,x+30,394);
line(x+30,394,x+35,384);
line(x+35,384,x+35,325);
sector(35+x,325,0,180,50,10);
}
void rain(int m,int x)
{
int i,j;
for(i=m;i<getmaxy();i+=10)
for(j=0;j<getmaxx();j+=10)
{
if((i>=317)&&(j>=x-15)&&(j<=85+x))
{
}
else
line(j,i,j,i+3);
}
delay(50);
}
void main()
{
int gd=DETECT,gm,k=0;
initgraph(&gd,&gm,"");
while(!kbhit())
{
rain(0,k);
man(k);
k++;
rain(3,k);
man(k);
k++;
rain(15,k);
man(k);
k++;
cleardevice();
}
getch();
63
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
RESULT
64
Department of CSE
CS010 806 Computer Graphics Lab
Exp 15:
Date
CAR SIMULATION
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize the variables
Step 3: Initialize the graphic system.
Step 4: Using rectangle and circle coordinates draw it.
Step 5: Using rectangle and circle and press the corresponding key for moving the car.
Step 6: Use the clear view part function.
Step 7: Increment the x value of both rectangle and circle.
Step 8: Repeat step 6 and step 7.
Step 9: Check if the kbhit()! = 0.
if it is true goto step 11 to 13
Step 10: Otherwise goto step 13
Step 11: If ch = „a‟ then assign y = 40
Step 12: If ch == „r‟ then assign y = -20
Step 13: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int i,j;
char ch;
clrscr();
initgraph(&gd,&gm," ");
rectangle(60,75,100,100);
rectangle(30,100,130,150);
circle(50,160,10);
circle(100,160,10);
printf("\nPress the following keys for moving the car");
65
Department of CSE
CS010 806 Computer Graphics Lab
clearviewport();
if(kbhit()!=0)
{
ch=getch();
if(ch=='a')
{
j=40;
delay(200);
}
if(ch=='r')
{
j=-20;
delay(200);
}
if(ch=='e')
{
exit(0);
}
i=i+j;
}
}
}
getch();
}
66
Department of CSE
CS010 806 Computer Graphics Lab
OUTPUT
RESULT
67
Department of CSE
CS010 806 Computer Graphics Lab
Exp 16:
Date
ROTATING WHEEL
AIM
ALGORITHM
Step 1: Start
Step 2: Initialize the graphic system.
Step 3: Set xc = 639/2, yc = 479/2, rad = M_PI/n
Step 4: Draw circle using circle(xc, yc, rad)
Step 5: Set rad2 = rad
Step 6: For I = 0 to n do
(i) draw line(xc+r*sin(rad2),yc-r*cos(rad2),xc-r*sin(rad2),yc+r*cos(rad2))
(ii) rad2=rad2+M_PI/n
(iii) Increment
Step 7: rad=rad+M_PI/100
Step 8: Draw line using line(200,yc+r,450,yc+r)
Step 9: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int xc,yc,r=50;
int n=4,i;
int gd=DETECT,gm;
float rad=M_PI/n, rad1=M_PI/100,rad2;
xc=639/2;
yc=479/2;
initgraph(&gd,&gm,”D:\TC\BGI”);
while(!kbhit())
{
circle(xc,yc,r);
rad2=rad;
for(i=0;i<n;i++)
{
line(xc+r*sin(rad2),yc-r*cos(rad2),xc-r*sin(rad2),yc+r*cos(rad2));
rad2=rad2+M_PI/n;
68
Department of CSE
CS010 806 Computer Graphics Lab
}
rad=rad+M_PI/100;
line(200,yc+r,450,yc+r);
delay(5);
clearviewport();
}
getch();
}
OUPUT
RESULT
69
Department of CSE