Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programs For Computer Graphics

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

1|Page

PROGRAM-1

Object- Write a program in C/C++ to implement DDA Line Drawing Algorithm.


Theory-In computer graphics, a hardware or software implementation of a digital differential
analyzer (DDA) is used for linear interpolation of variables over an interval between start and end
point. DDAs are used for rasterization of lines, triangles and polygons. In its simplest implementation
the DDA algorithm interpolates values in interval [(x1, y1), (x2, y2)] by computing for each xi the
equations xi = xi−1+1, yi = yi−1 + Δy/Δx, where Δx = x2 – x1 and Δy = y2 – y1.
The DDA method can be implemented using floating-point or integer arithmetic. The native floating-
point implementation requires one addition and one rounding operation per interpolated value (e.g.
coordinate x, y, depth, color component etc.) and output result. This process is only efficient when
an FPU with fast add and rounding operation is available.

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-

Procedure line DDA (x1, y1, x2, y2:integer);


Var
dx, dy, step, k:integer;
xInc, yInc, x, y : real;
begin
dx=abs(x2-x1)
dy=abs(y2-y1)
if abs(dx)>abs(dy)
then step=abs(dx)
else
step=abs(dy)
xInc=dx/step;
yInc=dy/step;
x=x1;
y=y1;
setpixel(round(x), round(y), 1);
for k=1 to steps
do
begin
x=x+xInc;
y=y+yInc;
end;
end
2|Page

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

Object- Write a program in C/C++ to implement Bresenhem’s Line DrawingAlgorithm.

Theory- The Bresenham line algorithm is an algorithm which determines which points in an n-


dimensional raster should be plotted in order to form a close approximation to a straight line between
two given points. It is commonly used to draw lines on a computer screen, as it uses only integer
addition, subtraction and bit shifting, all of which are very cheap operations in standard computer
architectures. It is one of the earliest algorithms developed in the field of computer graphics. A minor
extension to the original algorithm also deals with drawing circles.

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-

Step-1 Input the end points of a line

Step-2 Load end points in the frame buffer.

Step-3 Calculate constants-

dx, dy, 2dx, 2dx-2dy

and obtain the starting value for the decision parameter as-

p=2dy-dx

Step-4 If p<0, the next point to plot is-

X=x+1, y=y, p=p+2dy;

Otherwise-

X=x+1, y=y+1, p=p+2dy-2dx

Step-5 Repeat step 4 dx times.


5|Page

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

Object- Write a program in C/C++ to implement a Bresenhem’s Circle Drawing Algorithm.

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.

Step2: Accept the value of radius r.

Step3: Initialize x=0;

Y=r; and d=3-2r;

Step4: Draw the pixels at the co-ordinate (x,y).

Step5: check,if d<0 then compute new d as:d+4x+6: goto Step6.

Step6: compute d=d+4(x-y)+10 and decrement the value y by 1,i.e. y=y-1.

Step7: increament the value of x by +1.i.e. :x=x+1.

Step8: check,if x<=y is satisfied, goto Step3.

Step9: Stop the process.


8|Page

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

Object- Write a program in C/C++ to implement Character Generation.

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

Object- Write a program in C/C++ to implement rotation in 2-D transformation.

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

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);
}
14 | P a g e

OUTPUT-
15 | P a g e

PROGRAM-6

Object- Write a program in C/C++ to draw a pie chart

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

Object- Write a program in C/ C++ to draw Olympic symbol.

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

OBJECT- Write a program in C/C++ to implement any animation.

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

OBJECT- Write a program in C/C++ to display any animation.

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

OBJECT- Write a program in C/C++ to implement Flood Fill Algorithm.

THEORY-

Flood fill, also called seed fill, is an algorithm that determines the area connected to a given


node in a multi-dimensional array. It is used in the "bucket" fill tool of paint programs to
determine which parts of a bitmap to fill with color, and in games such
as Go and Minesweeper for determining which pieces are cleared. When applied on an image
to fill a particular bounded area with color, it is also known as boundary fill. The flood fill
algorithm takes three parameters: a start node, a target color, and a replacement color. The
algorithm looks for all nodes in the array which are connected to the start node by a path of the
target color, and changes them to the replacement color. There are many ways in which the
flood-fill algorithm can be structured, but they all make use of a queue or stack data structure,
explicitly or implicitly.

ALGORITHM-

Flood Fill Algorithm


procedure Fill ( x, y : in teger; oldVal, new Val:color);
begin
push (x, y );
while stack is not empty
begin
pop (x , y);
open_u p = FALSE;
open_d own = FALSE;
while color [x--, y] == oldVal ;
/* mo ve to most l eft pi xel */
whi le col or [x+ +,y] = = oldV al
begin
color [x,y] = newVal;
if ope n_up = = FALSE
begin
if color [x, y +1] == oldVal
begin
push (x, y+ 1);
open_ up = TRUE;
end ;
end
else if color [x, y+1] < > oldVal
ope n_up = FALSE ;
if ope n_down == FA LSE
begin
if color [x, y -1] == oldVa l
beg in
push (x, y- 1);
29 | P a g e

open_ down = TRUE;


end ;
end
else i f color [x, y-1] < > oldV al
ope n_down = FALSE;
end ;
end;
end;
end ;

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

void MidPoint(int Radius)


{
int iCntr, x, y,p0;
x=0;
y=Radius;
int xCenter=320;
int yCenter=240;

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

You might also like