Programs For Computer Graphics
Programs For Computer Graphics
Programs For Computer Graphics
PROGRAM-1
The fixed-point integer operation requires two additions per output cycle, and in case of fractional part
overflow, one additional increment and subtraction. The probability of fractional part overflows is
proportional to the ratio m of the interpolated start/end values.
DDAs are well suited for hardware implementation and can be pipelined for maximized throughput.
Algorithm-
Program-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,”c:\\tc\\bgi:”);
printf(“\n Enter the x and y value for starting point:\n”);
scanf(“%d%d”,&x1,&y1);
printf(“\n Enter the x and y value for ending point:\n”);
scanf(“%d%d”,&x2,&y2);
printf(“\n The Line is shown below: \n”);
draw(x1,y1,x2,y2);
getch();
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,1);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
}
3|Page
OUTPUT-
4|Page
PROGRAM-2
While algorithms such as Wu's algorithm are also frequently used in modern computer graphics
because they can support antialiasing, the speed and simplicity of Bresenham's line algorithm mean
that it is still important. The algorithm is used in hardware such as plotters and in the graphics chips of
modern graphics cards. It can also be found in many software graphics libraries. Because the algorithm
is very simple, it is often implemented in either the firmware or the hardware of modern graphics
cards.
The label "Bresenham" is used today for a whole family of algorithms extending or modifying
Bresenham's original algorithm.
Algorithm-
and obtain the starting value for the decision parameter as-
p=2dy-dx
Otherwise-
Program-
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
6|Page
OUTPUT-
7|Page
PROGRAM-3
THEORY:- Using Bresenham’s circle algorithm,for drawing circle ,we avoid the floating point
arithmetic and trigonometric calculation by using the integer form of arithmetic only.Using this
method a part of circle,may be from 0` to 45`angle is generated and the other seven segments are
obtained by getting the reflection image of the same.
If points are computed for the range of angle from 90` to 45` then the individual new point close to the
new circle can be found by either of the given two ways :
1. The value of x-co-ordinates will be increased by +1 whereas the y-direction image unchanged.
2. The value of x-co-ordinates will be increased by +1 and determined the value in y-direction by
one unit.
ALGORITHM:-
The following steps are used to draw the circle using bresenham’s circle algorithm.
Step1: Start.
Program-
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
printf("Enter the radius ");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
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();
}
9|Page
OUTPUT-
10 | P a g e
PROGRAM-4
Program-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,j;
int a[20][20]=
{{0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0},
{0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0}};
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<19;i++)
{
for(j=0;j<19;j++)
{
if(a[i][j]==1)
putpixel(100+j,200+i,WHITE);
}
}
getch();
}
11 | P a g e
OUTPUT-
12 | P a g e
PROGRAM-5
Theory-
Program-
#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://tc//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);
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();
}
13 | P a g e
OUTPUT-
15 | P a g e
PROGRAM-6
Program-
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#define round(a)(int(a+0.5))
void main()
{
double total=0.0,a=0.0;
double x2,y2;
int i,n;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"PIE CHART"<<endl;
cout<<"Enter the no. of regions"<<endl;
cin>>n;
double values[10];
double per[10];
double angle[10]={0,0,0,0,0,0,0,0,0,0};
double b[10];
cout<<"Enter the values of the regions"<<endl;
circle(300,300,100);
line(300,300,400,300);
for(i=0;i<n;i++)
{
cin>>values[i];
total=total+values[i];
}
for(i=0;i<n;i++)
{
per[i]=((values[i]/total)*100);
a=((per[i]/100)*360);
if(i==0)
b[i]=a;
else
16 | P a g e
b[i]=b[i-1]+a;
angle[i]=(3.14*b[i])/180;
x2=(300+100*cos(angle[i]));
y2=(300-100*sin(angle[i]));
line(300,300,round(x2),round(y2));
setfillstyle(1,i+1);
if(x2>300&&y2< 300)
floodfill(x2+2,y2+2,15);
else
floodfill(x2-2,y2-2,15);
}
getch();
closegraph();
}
17 | P a g e
OUTPUT-
18 | P a g e
PROGRAM-7
PROGRAM-
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void main()
{
int i=0,j=0,k=0,l=0,m=0,ch;
float pi=3.1424,a,b,c,d,e;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\nEnter 1 or 2 ");
scanf("%d",&ch);
printf("\n\nYou have entered %d",ch);
getch();
clrscr();
switch(ch)
{
case 1 : while(i<360)
{
a=(pi/180)*i;
setcolor(3);
circle(120+100*sin(a),150-100*cos(a),10);
i++;
delay(5);
}
while(j<360)
{
b=(pi/180)*j;
setcolor(0);
circle(280+100*sin(b),150-100*cos(b),10);
j++;
delay(5);
}
while(k<360)
{
c=(pi/180)*k;
setcolor(4);
circle(440+100*sin(c),150-100*cos(c),10);
k++;
delay(5);
}
19 | P a g e
while(l<360)
{
d=(pi/180)*l;
setcolor(14);
circle(200+100*sin(d),300-100*cos(d),10);
l++;
delay(5);
}
while(m<360)
{
e=(pi/180)*m;
setcolor(2);
circle(370+100*sin(e),300-100*cos(e),10);
m++;
delay(5);
}
break;
case 2 : while(i<360)
{
a=(pi/180)*i;
setcolor(3);
circle(120+100*sin(a),150-100*cos(a),10);
i++;
delay(5);
}
while(l<360)
{
d=(pi/180)*l;
setcolor(14);
circle(200+100*sin(d),300-100*cos(d),10);
l++;
delay(5);
}
while(j<360)
{
b=(pi/180)*j;
setcolor(0);
circle(280+100*sin(b),150-100*cos(b),10);
j++;
delay(5);
}
while(k<360)
{
c=(pi/180)*k;
setcolor(4);
circle(440+100*sin(c),150-100*cos(c),10);
k++;
delay(5);
}
20 | P a g e
while(m<360)
{
e=(pi/180)*m;
setcolor(2);
circle(370+100*sin(e),300-100*cos(e),10);
m++;
delay(5);
}
break;
default:
setcolor(13);
outtextxy(190,220,"YOU HAVE ENTERED THE WRONG CHOICE!!");
}
getch();
}
21 | P a g e
OUTPUT-
PROGRAM-8
22 | P a g e
PROGRAM-
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(int i=300;i>140;i--)
ellipse(305,140,90,89,15,2);
ellipse(335,140,90,89,15,2);
line(290,240,290,140);
line(320,240,320,140);
line(350,240,350,140);
circle(320,i,10);
delay(15);
cleardevice();
for(int j=0,k=0;j<100,k<70;j++,k++)
ellipse(305-k,138-j,90,89,15,2);
ellipse(350,140+j,90,89,15,2);
line(290,240,290,140);
line(320,240,320+k,140+j);
23 | P a g e
line(350,240,350,140);
delay(15);
cleardevice();
getch();
closegraph();
PROGRAM-9
24 | P a g e
PROGRAM-
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd,gm;
int x,y;
int i,j,kk;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(WHITE);
line(0,400,640,400);
rectangle(300,330,340,400);
rectangle(310,320,330,330);
setcolor(4);
line(319,280,319,398);
line(320,280,320,398);
rectangle(320,280,330,300);
outtextxy(340,280,"PRESS ANY KEY TO IGNITE THE ROCKET");
getch();
for(j=400;j<640;j++)
{
cleardevice();
setcolor(WHITE);
line(0,j,640,j);
rectangle(300,j-70,340,j);
rectangle(310,j-80,330,j-70);
setcolor(RED);
line(319,280,319,400);
line(320,280,320,400);
rectangle(320,280,330,300);
setcolor(YELLOW);
circle(325,300,2);
delay(5);
25 | P a g e
for(i=400;i>340;i--)
{
cleardevice();
setcolor(RED);
line(319,i,319,i-120);
line(320,i,320,i-120);
rectangle(320,i-120,330,i-100);
setcolor(YELLOW);
circle(325,i-100,2);
delay(25);
}
cleardevice();
kk=0;
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(kk);
kk=kk+3;
delay(50);
}
ellipse(320,30,0,360,j+100,j+0);
}
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(BLACK);
delay(2);
}
ellipse(320,30,0,360,j+100,j+0);
}
cleardevice();
for(i=0;i<70;i++)
{
setcolor(i);
settextstyle(GOTHIC_FONT,HORIZ_DIR,6);
outtextxy(110,150,"HAPPY NEWYEAR");
delay(90);
26 | P a g e
}
getch();
}
OUTPUT-
27 | P a g e
PROGRAM-10
28 | P a g e
THEORY-
ALGORITHM-
PROGRAM-
# include <stdio.h>
# include <dos.h>
# include <iostream.h>
# include <conio.h>
# include <stdlib.h>
# include <graphics.h>
void FloodFill(int,int,int,int);
void MidPoint(int);
void main()
{
int xCenter=320;
int yCenter=240;
int gDriver = DETECT, gMode, errorcode;
initgraph(&gDriver, &gMode, "c:\\tc\\bgi");
cleardevice();
MidPoint(49);
FloodFill(xCenter+1,yCenter+1,0,8);
getch();
return;
}
p0=(5/4)-Radius;
30 | P a g e
putpixel(xCenter+x,yCenter+y,15);
putpixel(xCenter-x,yCenter+y,15);
putpixel(xCenter+x,yCenter-y,10);
putpixel(xCenter-x,yCenter-y,10);
putpixel(xCenter+y,yCenter+x,12);
putpixel(xCenter-y,yCenter+x,12);
putpixel(xCenter+y,yCenter-x,9);
putpixel(xCenter-y,yCenter-x,9);
while(x<=y)
{
if(p0 < 0)
{
p0=p0 + 2*(x+1) + 1;
x=x+1;
}
else
{
p0=p0 + 2*(x+1) + 1 - 2*(y-1);
x=x+1;
y=y-1;
}
putpixel(xCenter+x,yCenter+y,15);
putpixel(xCenter-x,yCenter+y,15);
putpixel(xCenter+x,yCenter-y,10);
putpixel(xCenter-x,yCenter-y,10);
putpixel(xCenter+y,yCenter+x,12);
putpixel(xCenter-y,yCenter+x,12);
putpixel(xCenter+y,yCenter-x,9);
putpixel(xCenter-y,yCenter-x,9);
}
void FloodFill(int pointx,int pointy,int OldColor,int NewColor)
{
int Intensity=getpixel(pointx,pointy);
if(Intensity==OldColor)
{
putpixel(pointx,pointy,NewColor);
FloodFill(pointx+1,pointy,OldColor,NewColor);
FloodFill(pointx-1,pointy,OldColor,NewColor);
31 | P a g e
FloodFill(pointx,pointy+1,OldColor,NewColor);
FloodFill(pointx,pointy-1,OldColor,NewColor);
}
}