Practical File
Practical File
Practical file
On
COMPUTER GRAPHICS & MULTIMEDIA
APPLICATION
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
circle(250, 200, 50);
getch();
closegraph();
return 0;
}
Output:-
2. Write a program to implement dda line drawing
algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
intx,y,x1,x2,y1,y2,dx,dy,step,m,xinc,yinc;
x=x1;
y=y1;
printf("Enter Ist end point of line");
scanf("%d%d",&x1,&y1);
printf("Enter2ndendpointofline");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=(y2-y1)\(x2-x1);
if(abs(dx)>abs(dy)) step=abs(dx);
else step=abs(dy);
xinc=dx/float(steps);
yinc=dy/float(steps);
putpixel(x,y,2);
for(k=0;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
getch();
closegraph();
}
Output:-
Enter 1st end point of line:
100,200
Enter 2nd end point of line:
200,300
3. Write a program to draw a line using bresenham’s
Algorithm.
#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 gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "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;
}
Output:-
4. Write a program to draw a circle using bresenham’s
circle algorithm.
#include <stdio.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){
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
x++;
drawCircle(xc, yc, x, y);
delay(50);
}
}
int main()
{
int xc = 50, yc = 50, r = 30;
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}
Output:-
5. Write a program for boundry fill algorithm.
#include <graphics.h>
void boundaryFill8(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
rectangle(50, 50, 100, 100);
boundaryFill8(55, 55, 4, 15);
delay(10000);
getch();
closegraph();
return 0;
}
Output:-
6. Write a program for flood fil algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
voidfloodFill(intx,inty,intncolor,intocolor)
{
if(getpixel(x,y)==ocolor)
{
putpixel(x,y,ncolor);
floodFill(x+1,y,ncolor,ocolor);
floodFill(x-1,y,ncolor,ocolor);
floodFill(x,y+1,ncolor,ocolor);
floodFill(x,y-1,ncolor,ocolor);
}
delay(1);
}
voidmain()
{
intx,y,ncolor=BLUE,ocolor=WHITE;
int midx,midy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C://tc//bgi");
cleardevice();
printf("enter seed point:");
scanf("%d%d",&x,&y);
midx=getmaxx()/2;
midy=getmaxy()/2;
setbkcolor(RED);
setpalette(ocolor,GREEN);
fillellipse(midx,midy,50,25);
fillellipse(midx+100,midy+100,50,25);
floodFill(x,y,ncolor,ocolor);
getch();
closegraph();
}
Output:-
Enter Radius 200
7. Write a program to draw circle using midpoint
algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
intgd=DETECT,gm;
int r,x,y,p,xc=320,yc=420;
initgraph(&gd,&gm,"C://tc//bgi");
cleardevice();
printf("enterradius");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=1-r;
for(x=0;x<=y;x++)
{
if(p<0)
{
y=y; p=(p+2*x)+3;
}
else
{
y=y-1;
p=p+((2*(x-y)+5));
}
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);
}
getch();
closegraph();
}
Output:-
8. Write a program showing translation of a
triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
intx1,y1,x2,y2,x3,y3,tx,ty;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C://tc//bgi");
printf(."enter x1 and y1");
scanf("%d%d",&x1,&y1);
printf("enter x2 and y2");
scanf("%d%d",&x2,&y2);
printf("enter x3 and y3");
scanf("%d%d",&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
printf("entertranslationvectoralongx-axis");
scanf("%d",&tx);
printf("entertranslationvectoralongy-axis");
scanf("%d",&ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x3+tx,y3+ty);
line(x1+tx,y1+ty,x3+tx,y3+ty);
getch();
closegraph();
}
Output:-
enterx1andy1 100
250
enterx2andy2 200
250
enterx3andy3 150
150
Enter translation vector along x-axis 100
Enter translation vector along y-axis 100
9. Write a program to showing scaling of a
triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
Void main()
{
intc,x1,y1,x2,y2,x3,y3,sx,sy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Enter coord of 1st pixel");
scanf("%d%d",&x1,&y1);
printf("Entercoordinateof2ndpixel");
scanf("%d%d",&x2,&y2);
printf("Enter coordinate of 3rd pixel");
scanf("%d%d",&x3,&y3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x2,y2);
printf("Enter the scaling vector along x-axis");
scanf("%d",&sx);
printf("Enter the scaling vector along y-axis");
scanf("%d",&sy);
printf("Enter your choice\n Increment \n Decrement");
scanf("%d",&c);
switch(c)
{
case 1:
line(x1*sx,y1*sy,x2*sx,y2*sy);
line(x1*sx,y1*sy,x3*sx,y3*sy);
line(x3*sx,y3*sy,x2*sx,y2*sy);
case 2:
line(x1/sx,y1/sy,x2/sx,y2/sy);
line(x1/sx,y1/sy,x3/sx,y3/sy);
line(x3/sx,y3/sy,x2/sx,y2/sy);
break; default:
printf("You have entered a wrong choice");
goto i;
}
getch();
closegraph();
}
Output:-
Enter coordinates of 1st pixel: 100100
Enter coordinates of 2nd pixel: 50150
Entercoordinatesofthirdpixel:150150
Enter scaling vector(Sx,Sy):
Enter your choice
1. Increment
2. Decrement
10.Write a program to showing translation of a
circle.
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
Int x,y,r,tx,ty;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\tc");
printf("enter center coordinates of circle");
scanf("%d%d",&x,&y);
printf("enter radius of circle");
scanf("%d",&r);
circle(x,y,r);
printf("enter translation vector along x-axis");
scanf ("%d",&tx);
printf("enter translation vector along y-axis");
scanf ("%d",&ty);
circle(x+tx,y+ty,r);
getch();
closegraph();
}
Output:-
Enter center coordinates of circle 300
300
Enter the radius of circle 30
Enter translation vector along x-axis 100
Enter translation vector along y-axis 100
11. Write a program to showing scaling of circle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
Void main()
{
ints,r,c,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\tc");
printf("Enter centre coordinates of circle:");
scanf("%d%d",&x,&y);
printf("Enter radius");
scanf("%d",&r);
circle(x,y,r);
printf("Enter the scaling vector:");
scanf("%d",&s);
i:
printf("Enteryourchoice\n1.Increment\n2.Decrement");
scanf("%d",&c);
switch(c)
{
case1:
circle(x,y,r*s);
break;
case2:
circle(x,y,r/s);
break;
default:
printf("You have entered a wrong choice");
goto i;
}
getch();
closegraph();
}
Output:-
Entercoordinatesofcircle:300300
Enter radius of circle: 100
Enterscalingvectors:2
Enter your choice
1. Increment
2. Decrement
12. Write a program to showing rotation of a line.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
intgdriver=DETECT,gmode;
float theta;
int x1,y1,x2,y2,x11,y11,x22,y22;
initgraph(&gdriver,&gmode,"c://tc//bgi");
printf("enter thecoordinate of1st point");
scanf("%d%d",&x1,&y1);
printf("enterthecoordinateof2ndpoint");
scanf("%d%d",&x2,&y2);
line(x1,y1,x2,y2);
getch();
printf("enter the rotation angle");
scanf("%f",&theta);
theta=theta*3.14/180;
x11=(x1*cos(theta))-(y1*sin(theta));
y11=(x1*sin(theta))+(y1*cos(theta));
x22=(x2*cos(theta))-(y2*sin(theta));
y22=(x2*sin(theta))+(y2*cos(theta));
line(x11,y11,x22,y22);
getch();
closegraph();
}
Output:-
Enter the first co-ordinate
100 100
Enter the second co-ordinate
200 200
Enter the rotation angle
45
13. Write a program showing rotation of a triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
intgddriver=DETECT,gmode;
float theta;
floatx1,y1,x2,y2,x3,y3,x11,y11,x22,y22,x33,x44;
initgraph(&gdriver,&gmode,"c://tc//bgi");
printf("enterthe1stcoordinatepointofatriangle");
scanf("%d%d",&x1,&y1);
printf("enterthe2ndcoordinatepointoftriangle");
scanf("%d%d",&x2,&y2);
printf("enterthe3rdcoordinatepointoftriangle");
scanf("%d%d",&x3,&y3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x2,y2);
getch();
printf("enter the rotation angle");
scanf("%f",&theta);
theta=theta*3.14/180;
x11=(x1*cos(theta))-(y1*sin(theta));
y11=(x1*sin(theta))+(y1*cos(theta));
x22=(x2*cos(theta))-(y2*sin(theta));
y22=(x2*sin(theta))+(y2*cos(theta));
x33=(x3*cos(theta))-(y3*sin(theta));
y33=(x3*sin(theta))+(y3*cos(theta));
line(x11,y11,x22,y22);
line(x11,y11,x33,y33);
line(x33,y33,x22,y22);
getch();
closegraph();
}
Output:-
Enter the 1st co-ordinate point of triangle
50 100
Enter the 2nd co-ordinate point of triangle
100 150
Enter the 3rd co-ordinate point of triangle
200 150
Enter the rotation angle
45
14. Write a program to make a hut.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
set color(6);
rectangle(50,180,150,300);
rectangle(150,180,320,300);
rectangle(80,250,120,300);
line(100,100,50,180,);
line(100,100,300,100);
line(100,100,300,100);
line(300,100,320,180);
getch();
closegraph();
}
Output:-
15. Write a program to animating a fish.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"c:/tc/bgi");
clrscr();
setcolor(2);
for(i=500;i>=0;i--)
{
clearviewport();
ellipse(200+i,200,0,360,50,30);
line(250+i,200,280+i,170);
line(280+i,170,280+i,230);
line(280+i,230,250+i,200);
circle(160+i,190,3);
}
setcolor(1);
getch();
closegraph();
}
Output:-
16. Write a program to make a kite.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
inta,b,gd=DETECT,gm,i;
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
line(100,100,50,180);
line(100,100,150,180);
line(50,180,100,250);
line(150,180,100,250);
line(100,100,100,250);
line(50,180,150,180);
line(100,250,70,300);
line(100,250,130,300);
line(70,300,130,300);
line(100,300,120,320);
line(120,320,80,340);
line(80,340,120,360);
line(120,360,80,380);
set color(4);
getch();
closegraph();
}
Output:-
17. Write a program to Implement Ellipse Drawing
Algorithm.
#include<stdio.h>
#include <graphics.h>
#include<conio.h>
int main()
{
int gd = DETECT, gm;
int x = 250, y = 200;
int start_angle = 0;
int end_angle = 360;
int x_rad = 100;
int y_rad = 50;
initgraph(&gd, &gm, "");
ellipse(x, y, start_angle,
end_angle, x_rad, y_rad);
getch();
closegraph();
return 0;
}
Output:-