0% found this document useful (0 votes)
346 views21 pages

Computer Graphics Lab Manual: MR - Shivakumar B, Lecturer, Dept of BCA, SSIBM, Tumakuru

The document is a computer graphics lab manual containing 12 programs to perform various graphics operations like drawing lines, circles, triangles and their transformations. It provides the program code and explanations to draw these shapes using techniques like DDA, Bresenham's algorithm and to perform translation, scaling and rotation of triangles. The lab manual is intended to help students learn and practice computer graphics concepts.

Uploaded by

Guru Darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views21 pages

Computer Graphics Lab Manual: MR - Shivakumar B, Lecturer, Dept of BCA, SSIBM, Tumakuru

The document is a computer graphics lab manual containing 12 programs to perform various graphics operations like drawing lines, circles, triangles and their transformations. It provides the program code and explanations to draw these shapes using techniques like DDA, Bresenham's algorithm and to perform translation, scaling and rotation of triangles. The lab manual is intended to help students learn and practice computer graphics concepts.

Uploaded by

Guru Darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Computer Graphics Lab Manual

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


Computer Graphics Lab Manual

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

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


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;
}
else
{
steps=dy;
}

dx=dx/steps;
dy=dy/steps;
x=x0;
y=y0;

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


Computer Graphics Lab Manual

i=1;
while(i<=steps)
{
putpixel(x,y,RED);
x+=dx;
y+=dy;
i=i+1;
}
getch();
closegraph();
}

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


Computer Graphics Lab Manual

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;
}
else
{
d=grtr_d+d;
y=y+1;

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


Computer Graphics Lab Manual

}
x=x+1;
}while(x<x1);
getch();
}

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


Computer Graphics Lab Manual

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();
}

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


Computer Graphics Lab Manual

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


Computer Graphics Lab Manual

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;
}
else

d = d + 4 * x + 6;

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


Computer Graphics Lab Manual

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();
}

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


Computer Graphics Lab Manual

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);
}

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


Computer Graphics Lab Manual

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);
}

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


Computer Graphics Lab Manual

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);
}

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


Computer Graphics Lab Manual

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();

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


Computer Graphics Lab Manual

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();
}

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


Computer Graphics Lab Manual

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);
}

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


Computer Graphics Lab Manual

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");

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


Computer Graphics Lab Manual

getch();
closegraph();
return 0;
}

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


Computer Graphics Lab Manual

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
}
else

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


Computer Graphics Lab Manual

{
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();
}

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


Computer Graphics Lab Manual

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

You might also like