You are on page 1of 22

Computer Graphics Lab Manual

Mr.Shivakumar B, Lecturer, Dept of BCA, SSIBM, Tumakuru Page 1


Contents

Sl Program Name Page


No Number
1 Write a program to draw a straight line using DDA
technique
2 Write a program to draw a straight line using
Bresenham’s technique
3 Write a program to draw a circle using DDA technique

4 Write a program to draw a circle using Bresenham’s


technique
5 Write a program to draw a triangle to perform
translation
6 Write a program to draw a triangle to perform scaling

7 Write a program to draw a triangle to perform rotation

8 Write a program to draw a pie chart

9 Write a program to draw a histogram

10 Write a program to clip a triangle against a given


window
11 Write a program to animate a man walking with a n
umbrella
12 Write a program to rotate an object from one end of the
screen to the other end using the built-in-line and circle
function
Computer Graphics Lab Manual

1. Write a program to draw a straight line using DDA technique


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,i;
int x0,y0,x1,y1;
float dx,dy,steps,x,y;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setbkcolor(BLUE);
printf("\n *****DDA Line Drawing Algorithm**** ");
printf("\n Enter the starting coordinate points :");
scanf("%d %d",&x0,&y0);
printf("\n Enter the Ending co-ordinate points:");
scanf("%d %d",&x1,&y1);
dx=(float)(x1-x0);
dy=(float)(y1-y0);
if(dx>=dy)
{
steps=dx;
}
els
e
{ steps=dy;

dx=dx/steps;
dy=dy/steps;
x=x0;
y=y0;
Mr.Shivakumar B, Lecturer, Dept of BCA, SSIBM, Tumakuru Page 3
i=1;
while(i<=steps)
{
putpixel(x,y,RED);
x+=dx;
y+=dy;
i=i+1;
}
getch();
closegraph();
}
2. Write a program to draw a straight line using
Bresenham’s technique
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,x1,y1,delx,dely,m,grtr_d,smlr_d,d;
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("******* BRESENHAM'S LINE DRAWING
ALGORITHM *****\n\n");
printf("enter initial coordinate = ");
scanf("%d %d",&x,&y);
printf("enter final coordinate = ");
scanf("%d %d",&x1,&y1);
delx=x1-x;
dely=y1-y;
grtr_d=2*dely-2*delx;
smlr_d=2*dely;
d=(2*dely)-delx;
do
{
putpixel(x,y,1);
if(d<0)
{
d=smlr_d+d;
}
els
e
{ d=grtr_d+d;
y=y+1;
}
x=x+1;
}while(x<x1);
getch();
}
3. Write a program to draw a circle using DDA technique
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode,errorcode,tmp,i=1,rds;
float st_x,st_y,x1,x2,y1,y2,ep;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
printf("Enter Radius:");
scanf("%d",&rds);
while(rds>pow(2,i))
i++;
ep=1/pow(2,i);
x1=rds; y1=0;
st_x=rds; st_y=0;
do
{
x2=x1+(y1*ep);
y2=y1-(x2*ep);
putpixel(x2+200,y2+200,10);
x1=x2;
y1=y2;
}while((y1-st_y)<ep || (st_x-x1)>ep);
getch();
}
4. Write a program to draw a circle using Bresenham’s technique
#include <stdio.h>
#include<conio.h>
#include <dos.h>
#include <graphics.h>
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
{
x++;
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
els
e

d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(100);
}
}
void main()
{
int xc, yc , r2;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :");
scanf("%d",&r2);
circleBres(xc, yc, r2);
getch();
}
5. Write a program to draw a triangle to perform translation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3;
void draw();
void triangle();
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the 1st point for the triangle :");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle :");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle :");
scanf("%d%d",&x3,&y3);
cleardevice();
draw();
triangle();
getch();
}

void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void triangle()
{
int x,y,a1,a2,a3,b1,b2,b3;
printf("Enter the Transaction coordinates : ");
scanf("%d%d",&x,&y);
cleardevice();
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
6. Write a program to draw a triangle to perform scaling
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:"); scanf("%d
%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:"); scanf("%d
%d",&x3,&y3);
draw();
scale();
}

void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scalling coordinates :");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();

}
7. Write a program to draw a triangle to perform rotation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>

void triangle(int x1, int y1, int x2, int y2, int x3, int y3);
void rotate(int x1, int y1, int x2, int y2, int x3, int y3);

void main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");

printf("Enter the 1st point for the triangle :");


scanf("%d%d", &x1, &y1);
printf("Enter the 2nd point for the triangle :");
scanf("%d%d", &x2, &y2);
printf("Enter the 3rd point for the triangle e:");
scanf("%d%d", &x3, &y3);
triangle(x1, y1, x2, y2, x3, y3);
getch();
cleardevice();
rotate(x1, y1, x2, y2, x3, y3);
setcolor(1);
triangle(x1, y1, x2, y2, x3, y3);
getch();
}
Void triangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

void rotate(int x1, int y1, int x2, int y2, int x3, int y3)
{
int x, y, a1, b1, a2, b2, a3, b3, p = x2, q = y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f", &Angle);
cleardevice();
Angle = (Angle * 3.14) / 180;
a1 = p + (x1 - p) * cos(Angle)-(y1 - q) * sin(Angle);
b1 = q + (x1 - p) * sin(Angle)+(y1 - q) * cos(Angle);
a2 = p + (x2 - p) * cos(Angle)-(y2 - q) * sin(Angle);
b2 = q + (x2 - p) * sin(Angle)+(y2 - q) * cos(Angle);
a3 = p + (x3 - p) * cos(Angle)-(y3 - q) * sin(Angle);
b3 = q + (x3 - p) * sin(Angle)+(y3 - q) * cos(Angle);
printf("Rotate");
triangle(a1, b1, a2, b2, a3, b3);
}
8. Write a program to draw a pie
chart #include<graphics.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int gd = DETECT, gm, x, y;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(220,10,"PIE CHART");

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

settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);
setfillstyle(SOLID_FILL, RED);
pieslice(x, y, 0, 60, 120);
outtextxy(x + 140, y - 70,
"FOOD");

setfillstyle(SOLID_FILL, YELLOW);
pieslice(x, y, 60, 160, 120);
outtextxy(x - 30, y - 170, "RENT");

setfillstyle(SOLID_FILL, GREEN);
pieslice(x, y, 160, 220, 120);
outtextxy(x - 250, y,
"ELECTRICITY");

setfillstyle(SOLID_FILL,
BROWN); pieslice(x, y, 220, 360,
120); outtextxy(x, y + 150,
"SAVINGS");
getch();
closegraph();
return 0;
}
9. Write a program to animate a man walking with a n umbrella
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gr=DETECT,gm;
int i,x,y,j;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
for(j=1;j<600;j=j+5)
{
line(0,400,800,400);
circle(30+j,280,20); //head
line(30+j,300,30+j,350); //body
line(30+j,330,70+j,330); //hand
if(j%2==0)
{
line(30+j,350,25+j,400); //left leg
line(30+j,350,10+j,400); // right
}
els
e
{
line(30+j,350,35+j,400); //transition
delay(20);
}
line(70+j,250,70+j,330);
pieslice(70+j,250,180,0,80);
for(i=0;i<300;i++)
{
x=random(800);
y=random(800);
outtextxy(x,y,"/");
}
delay(170);
cleardevice();
}
getch();
closegraph();
}

You might also like