0% found this document useful (0 votes)
61 views13 pages

CG Lab Manual

The document provides programming assignments for computer graphics lab involving drawing various shapes and images using different algorithms. The assignments include drawing a straight line, circle, triangle, pie chart, histogram, clipping a triangle, animating a walking man, and rotating an object on the screen using basic graphics functions.

Uploaded by

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

CG Lab Manual

The document provides programming assignments for computer graphics lab involving drawing various shapes and images using different algorithms. The assignments include drawing a straight line, circle, triangle, pie chart, histogram, clipping a triangle, animating a walking man, and rotating an object on the screen using basic graphics functions.

Uploaded by

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

6thSem BCA

Paper: BCACsP6.8: COMPUTER GRAPHICS LAB

PRACTICAL

3Hrs /Week 50 Marks

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 pie chart

9. Write a program to draw Histogram

10. Write a program to clip a triangle against a given window.

11. Write a program to animate a man walking with an 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 functions.


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

# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void DDALINE(int x1, int y1, int x2, int y2);
void main()
{
int gd=DETECT, gm;
int x1,y1,x2,y2;
clrscr();
cout <<endl<<"Enter first point ";
cin >> x1>>y1;
cout<<endl<<"Enter second point ";
cin>>x2>>y2;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cleardevice();
DDALINE(x1,y1,x2,y2);
getch();
closegraph();
}
void DDALINE(int x1, int y1, int x2, int y2)
{
int dx,dy,x,y,i,step;
float xinc,yinc;
dx = abs(x2-x1);
dy = abs(y2-y1);
if (dx > dy)
step = dx;
else
step = dy;
xinc = dx / step;
yinc = dy / step;
x = x1;
y = y1;
putpixel(x,y,RED);
for (i=1; i<step; i++)
{
x = x + xinc;
y = y + yinc;
putpixel(ceil(x), ceil(y), BLUE);
}
putpixel(x2,y2,RED);
}

2. Write a program to draw a straight line using Bresenham’s technique.

# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void BLINE(int x1, int y1, int x2, int y2);
void main()
{
int gd=DETECT, gm;
int x1,y1,x2,y2;
clrscr();
cout <<endl<<"Enter first point ";
cin >> x1>>y1;
cout<<endl<<"Enter second point ";
cin>>x2>>y2;
initgraph(&gd,&gm,"");
cleardevice();
BLINE(x1,y1,x2,y2);
getch();
closegraph();
}
void BLINE(int x1, int y1, int x2, int y2)
{
int dx,dy,x,y,xend,p;
dx = abs(x2-x1);
dy = abs(y2-y1);
if (x1<x2)
{
x = x1;
y = y1;
xend = x2;
}
else
{
x = x2;
y = y2;
xend = x1;
}
putpixel(x,y,RED);
p = 2 *(dx-dy);
while(x <= xend)
{
x++;
if (p < 0)
p = p + 2 * dx;
else
{
y++;
p = p + 2 *(dx-dy);
}
putpixel(x,y,BLUE);
}
putpixel(x2,y2,RED);
}
3. Write a program to draw a Circle using Bresenham’s technique.

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
void plotpoints(int xc,int yc, int x, int y);
void BCircle(int,int,int);
void main()
{
int gd=DETECT, gm;
int xc,yc,r;
clrscr();
cout <<endl<<"Enter centre of the circle : ";
cin>>xc>>yc;
cout<<endl<<"Enter radius of the circle : ";
cin>>r;
initgraph(&gd,&gm,"d:\\tcc\bgi");
cleardevice();
BCircle(xc,yc,r);
getch();
closegraph();
}
void BCircle(int xc,int yc,int r)
{
int p, x,y;
x = 0;
y = r;
p = 1-r;
plotpoints(xc,yc,x,y);
while(x <= y)
{
if(p<0)
p = p + 2 * x + 1;
else
{
y--;
p = p + 2*(x-y) + 1;
}
x++;
plotpoints(xc,yc,x,y);
}
}
void plotpoints(int xc,int yc, int x, int y)
{
putpixel(xc+x , yc+y , 1);
putpixel(xc+x , yc-y , 2);
putpixel(xc-x , yc+y , 3);
putpixel(xc-x , yc-y , 4);
putpixel(xc+y , yc+x , 5);
putpixel(xc+y , yc-x , 6);
putpixel(xc-y , yc+x , 7);
putpixel(xc-y , yc-x , 8);
}
4. Write a program to draw a circle using DDA technique.

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
void DDACircle(int,int,int);
void main()
{
int gd=DETECT, gm;
int xc,yc,r;
clrscr();
cout <<endl<<"Enter centre of the circle : ";
cin>>xc>>yc;
cout<<endl<<"Enter radius of the circle : ";
cin>>r;
initgraph(&gd,&gm,"d:\\tcc\bgi");
cleardevice();
DDACircle(xc,yc,r);
getch();
closegraph();
}
void DDACircle(int xc,int yc,int r)
{
int t, x,y;
for(t=0;t<=360; t++)
{
x = xc + r * cos(t);
y = yc + r * sin(t);
putpixel(x,y,RED);
}
}
5. Write a program to draw pie chart

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <stdlib.h>
void piechart(int data[]);
void main()
{
int gd= DETECT, gm;
int i, data[4];
clrscr();
for(i =0; i<4; i++)
{
cout <<"Enter number of votes ";
cin >>data[i];
}
initgraph(&gd,&gm, "c:\\tcc\bgi");
cleardevice();
piechart(data);
getch();
closegraph();
}
void piechart(int data[])
{
int xc,yc,r, i, stangle, endangle;
float total=0,x,y;
cout <<"Enter radius of the pie chart ";
cin >>r;
xc = getmaxx()/2.0;
yc = getmaxy()/2.0;
for( i=0; i<4; i++)
total = total + data[i];
stangle = 0;
int l = 0;
char st[20];
for(i=0; i<4; i++)
{
itoa(data[i],st,10);
outtextxy(l,50,st);
l=l+50;
endangle = 360.0 * data[i] / total + stangle;
if (i == 3)
endangle = 360;
setfillstyle(i+5, CYAN);
pieslice(xc,yc,stangle, endangle, r);
stangle = endangle;
getch();
}
}
6. Write a program to draw a triangle to perform translation

#include<conio.h>
#include<iostream.h>
#include<Graphics.h>
void main(){
int gd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int x1[3]={300,300,200};
int y1[3]={100,100,200};
int x2[3]={200,400,400};
int y2[3]={200,200,200};
int i;
for (i=0; i<3; i++)
line(x1[i],y1[i],x2[i],y2[i]);
int tx1[3], ty1[3], tx2[3], ty2[3];
int vx,vy;
cout<< "Enter Translation Vector: ";
cin>>vx>>vy;
for(i=0;i<3;i++)
{
       tx1[i] = x1[i] + vx;
       ty1[i] = y1[i] + vy;
       tx2[i] = x2[i] + vx;
       ty2[i] = y2[i] + vy;
}
setcolor(14);
for(i=0;i<3;i++)
line(tx1[i], ty1[i], tx2[i], ty2[i]);
getch();
closegraph();
}
7. Write a program to draw a triangle to perform scaling

int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm,” “);
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();
}

8. Write a program to draw a triangle to perform scaling

#include<stdio.h>  
#include<graphics.h>  
#include<math.h>  
int main()  
{  
  intgd=0,gm,x1,y1,x2,y2;  
double s,c, angle;  
 initgraph(&gd, &gm, "C:\\TC\\BGI");  
setcolor(RED);  
printf("Enter coordinates of line: ");  
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);  
cleardevice();  
setbkcolor(WHITE);  
line(x1,y1,x2,y2);  
getch();  
setbkcolor(BLACK);  
 printf("Enter rotation angle: ");      
scanf("%lf", &angle);  
setbkcolor(WHITE);  
 c = cos(angle *3.14/180);  
 s = sin(angle *3.14/180);  
 x1 = floor(x1 * c + y1 * s);  
 y1 = floor(-x1 * s + y1 * c);  
 x2 = floor(x2 * c + y2 * s);  
  y2 = floor(-x2 * s + y2 * c);  
cleardevice();  
line(x1, y1 ,x2, y2);  
 getch();  
closegraph();  
return 0;  
}  
9. Write a program to animate a man walking with an umbrella.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int rhx,rhy,j,i;
clrscr();
initgraph(&gd,&gm,”D:\\TC\\BGI”);
for(i=0;i<500;i+=5)
{
line(20,380,580,380); //platform
if(i%2==0)
{
line(25+i,380,35+i,340); //leftleg
line(45+i,380,35+i,340);//right leg
line(35+i,310,25+i,330);//left hand
delay(20);
}
else
{
line(35+i,380,35+i,340);
line(35+i,310,40+i,330);
delay(20);
}
line(35+i,340,35+i,310); //body
circle(35+i,300,10); //head
line(35+i,310,50+i,330); // hand
line(50+i,330,50+i,280); //umbrella stick
line(15+i,280,85+i,280); //umbrella right
arc(50+i,280,0,180,35); //umbrella body
arc(55+i,330,180,360,5);//umbrella handle
rhx=getmaxx();
rhy=getmaxy();
for(j=0;j<100;j++)
{
outtextxy(random(rhx),random(rhy-50),”|”);
setcolor(WHITE);
}
delay(150);
cleardevice();
}
getch();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy