Computer Graphics: (CO 313) (Lab File)
Computer Graphics: (CO 313) (Lab File)
Computer Graphics: (CO 313) (Lab File)
(CO 313)
(LAB FILE)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
EXPERIMENT 1
Aim:
Theory:
Line:
A line connects two points. It is a basic element in graphics. To draw a line, you need two points between which you
can draw a line.
There are three-line drawing algorithms:
• DDA
• Bresenham’s
• Mid-Point
Where,
We refer the one point of line as X0, Y0 and the second point of line as X1, Y1. The lines appear continuous but
they are actually made of pixels. The lines are drawn by a process known as rasterization.
DDA was a mechanical device for numerical solution of differential equations-Line y=mx+h satisfies differential
equation dy/dx=m=(y2-y2)/(x2-x1).
The solution of the finite difference approximation is:
Xi+1=Xi+x
Yi+1=Yi+delta_y*(y2-y1)/(x2-x1)
DDA line generation algorithm uses repeated addition.
DDA line generation algorithm is the simple line generation algorithm which is explained step by step here:
Step 1 – Get the input of two end points (X0, Y0) and (X1, Y1).
Step 2 – Calculate the difference between two end points.
dx=X1-X0
dy=Y1-Y0
Step 3 – Based on the calculated difference in step-2, you need to identify the number of steps to put pixels.
If dx>dy:
then you need more steps in x coordinate, otherwise in y coordinate.
If dx>dy:
Steps=absolute(dx)
Else:
Steps=absolute(dy)
Step 4 – Calculate the increment in x coordinate and y coordinate
X_inc=dx/(float)steps
Y_inc=dy/(float)steps
Step 5 – Put the pixel by successfully incrementing x and y coordinates accordingly and compete the drawing of the
line.
Code:
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
closegraph();
}
Output:
Discussion:
1. With this experiment we got to learn about DDA’s line generation algorithm.
2. A C++ program was implemented which generated the line as expected.
3. We also got to know about the advantages and drawbacks of this algorithm.
EXPERIMENT 2
Aim:
Theory:
Line:
A line connects two points. It is a basic element in graphics. To draw a line, you need two points between which you
can draw a line.
There are three-line drawing algorithms:
• DDA
• Bresenham’s
• Mid-Point
Where,
We refer the one point of line as X0, Y0 and the second point of line as X1, Y1. The lines appear continuous but
they are actually made of pixels. The lines are drawn by a process known as rasterization.
Bresenham Algorithm:
Bresenham’s line algorithm that determines the points of an n-dimensional raster that should be selected in order to
form a close approximation to a straight line between two points.it is commonly used to draw line primitives in a
bitmap image (e.g, on computer screen), as it uses only integer addition,subtraction & bit shifting ,all of which are
very cheap operations in standard computer architectures. It is an incremental error algorithm. It is one of the earliest
algorithms developed in the field of computer graphics.
Bresenham’s algorithm chooses the integer Y corresponding to the pixel center that is closest to the ideal(fraction) Y
for the same X; on successive columns Y can remain the same or increase by 1. The general equation of the line
through the endpoints is given by:
(Y - Y0) /( Y1 - Y0) = (X - X0) /( X1 - X0)
Since we know the column, X, the pixel’s row,y,is given by rounding this quantity to the nearest integer:
y= (y1-y0)*(x-x0)/(x1-x0) + y0
The slope △Y/△X depends on the endpoint coordinates only & can be precomputed , & the ideal Y for successive
integer values of X can be computed starting from & repeatedly adding the slope.
In practice, the algorithm does not keep track of the Y coordinate,which increases by m = △Y/△X each time
increases by one; it keeps an error bound at each stage,which represents the negative of the distance from (a) the
point where the line exits the pixel to (b) the top edge of the pixel.This value is first set to m-0.5 (due to using the
pixel’s center coordinates),and is incremented by m each time the x co-odinate is incremented by one .If error
becomes greater than 0.5 we know that the line has moved upwards one pixel,and that we must increment our y
coordinate and readjust the error to represent the distance from the top of the new pixel - which is done by
subtracting one from error.
Pseudocode :
plotline(x0,y0,x1,y1)
dx=x1-x0
dy=y1-y0
D=2*dy-dx
y=y0;
For x from x0 to x1
plot(x,y)
if(D>0)
y=y+1
D=D-2*dx
End if (D=D+2*dy)
CODE :
#include <iostream>
#include <graphics.h>
#include <math.h>
#include <dos.h>
using namespace std;
int main()
{
int x0, y0, x1, y1;
cout<<"Enter co-ordinates of first point: ";
cin>>x0>>y0;
cout<<"Enter co-ordinates of second point: ";
cin>>x1>>y1;
initwindow(800,800);
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;
}
delay(10);
x=x+1;
}
getch();
return 0;
}
OUTPUT :
Discussion
The main advantage of Bresenham algorithm is its speed. The disadvantage of such a simple algorithm is that it is
meant for basic line drawing. The “advanced” topic of anti-aliasing is not a part of Bresenham algorithm, so to draw
smooth lines you’d want to use a different algorithm.
4. With this experiment we got to learn about Bresenham’s line generation algorithm.
5. A C++ program was implemented which generated the line as expected.
6. We also got to know about the advantages and drawbacks of this algorithm.
EXPERIMENT 3
Theory:
P1 = (x,y)
P2 = (y.x)
P3 = (-y,x)
P4 = (-x, y)
P5 = (-x, -y)
P6 = (-y,x)
P7 = (y,-x)
P8 = (x,-y)
Equation of circle :
x2 + y2 = a2
We consider a circle in second octant from x = 0 to x y. We use the same concept of eight-way symmetry
to plot the entire circle
Principle: We have 2 pixels -
Upper-pixel (U) = (xi + 1 ,yi)
Lower-pixel (L) = (xi+1 , yi - 1)
We need to select, which of these pixels is closer to the true circle and is this algorithm we find the
midpoint between the two pixels. This mid-point, M is given as follows -
M(xi+1 , (yi+yi-1)/2)
Midpoint is Plot
1. Input the radius, centre (xc,yc) and obtain the first point on the circumference of the circle. Centre
on the origin as :
(x0 , y0) = (0 , r) i.e. initialize starting point as x=0 and y=r.
2. Calculate the initial value of the decision parameter as
p = 1-r
3. Make a while loop as follows :
Do
{
Plot(x , y)
If (p<0){
x = x+1
y=y
p = p + 2*x + 1
}
else
{
x=x+1
y=y–1
p = p + 2*x – 2*y + 1
}
} while (x < y)
4. Determine symmetry points in other octants.
5. Stop.
Code:
#include <graphics.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float xc, yc,r;
cout<<”Enter centre points x and y : “;
cin>>xc>>yc;
cout<<”Enter radius of circle : “;
cin>>r;
initwindow(600,600);
float x=0, y=r;
float p=1-y;
while(x<=y)
{
putpixel(xc+x, yc+y, 5);
putpixel(xc-x, yc-y, 5);
putpixel(xc+x, yc-y, 5);
putpixel(xc-x, yc+y, 5);
putpixel(xc+y, yc+x, 5);
putpixel(xc-y, yc-x, 5);
putpixel(xc+y, yc-x, 5);
putpixel(xc-y, yc+x, 5);
delay(10);
if(p>=0)
{
p += 2*x – 2*y + 5;
y--;
}
else
{
p += 2*x+3;
}
x++;
}
getch();
return 0;
}
Output:
Discussion:
As in Bresenham’s line algorithm, the midpoint method calculates pixel positions along the circumference
of a circle using integer additions and subtractions, assuming that the circle parameters are specified in
integer screen coordinate.
1. The midpoint method is used for deriving efficient scan-conversion algorithms to draw
geometric curves on raster displays
2. The method is general and is used to transform the nonparametric equation f(x,y) = 0, which
describes the curve, into an algorithms that draws the curve
Theory
Midpoint ellipse algorithm is a method for drawing ellipses in computer graphics. This method is
modified from Bresenham’s algorithm so it is sometimes known as Bresenham's circle algorithm.
The advantage of this modified method is that only addition operations are required in the program
loops. This leads to simple and fast implementation in all processors.
Let us consider one quarter of an ellipse. The curve is divided into two regions. In region I, the slope on
the curve is greater than –1 while in region II less than –1.
Algorithm:
Putpixel(rx+x,ry+y);
Putpixel(rx+x,ry-y);
Putpixel(rx-x,ry+y);
Putpixel(rx-x,ry-y);
5. Initialize dx and dy as
Dx=2r^2y;
Dy=2r^2x;
6. Then,
do{
Putpixel(x,y,rx,ry);
If(p1<0){
x=x+1;
y=y;
dx=dx+2r^2y;
p1=p1+dx+r^2y;}
Else{
x=x+1;
y=y-1;
dx=dx+2r^2y;
dy=dy-2r^2x;
p1=p1+dx-dy+r^2y;}
}while(dx<dy)
8. Then,
do{
Putpixel(x,y,rx,ry);
If(p2>0){
x=x;
y=y-1;
dy=dy-2r^2x;
p2=p2-dy+r^2x;}
Else{
x=x+1;
y=y-1;
dx=dx+2r^2y;
dy=dy-2r^2x;
p2=p2+dx-dy+r^2x;}
}while(y>0)
9. Determine Symmetrical Point in other three quadrants.
Code:
#include<graphics.h>
#include<stdlib.h>
#include<iostream>
#include <math.h>
//#include<conio.h>
using namespace std;
int main()
{
//clrscr();
//int gd = DETECT, gm;
int xc,yc,x,y;float p;
long rx,ry;
//Region 2
p=ry*ry*(x+0.5)(x+0.5)+rx*rx(y-1)*(y-1)-rx*rx*ry*ry;
while(y > 0)
{
if(p <= 0)
{
x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y+rx*rx;
}
else
{
y--;
p = p-2*rx*rx*y+rx*rx;
}
putpixel(xc+x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,RED);
}
getch();
return 0;
}
Output:
Discussion:
In this algorithm we first calculate the p1 then we started loop till dx<dy. In this loop if(p1<0) then
x=x+1,dx=dx+r^2y and p1=p1+dx+r^2y else y=y-1,dy=dy-r^2x and p1=p1+dx-dy+r^2y
We start a loop till (y>0), if (p2>0) then y=y-1,dy=dy-2r^2x and p2=p2-dy+r^2x else, x=x+1,y=y-
1,dx=dx+2r^2y,dy=dy-2r^2x and p2=p2+dx-dy-r^2x.
• The mid-point method for deriving efficient scan conversion algorithms to draw geometric
curves on raster displays in described. The method is general and is used to transform the non-
parametric equation f(x,y)=0, which describes the curve, into an algorithm that draws the curve.
• Time consumption is high.
• The distance between the pixels is not equals so we won’t get smooth circle.
EXPERIMENT 5
Theory: Circular arc algorithm is based on the principle of midpoint circle drawing
algorithm but difference is that here we don't determine symmetry point in all other octants.
In computer graphics, the midpoint circle algorithm is an algorithm used to determine the
points needed for rasterizing a circle. Bresenham's circle algorithm is derived from the midpoint
circle algorithm. The algorithm can be generalized to conic sections.
The objective of the algorithm is to find a path through the pixel grid using pixels which are as
close as possible to solutions of x2 + y2 = r2. At each step, the path is extended by choosing the
adjacent pixel which satisfies x2+ y2 <= r2 but maximizes x2 + y2. Since the candidate pixels are
adjacent, the arithmetic to calculate the latter expression is simplified, requiring only bit shifts
and additions. This algorithm draws only one quadrant, starting from each cardinal direction (0°)
and extends both ways to reach the nearest multiple of 45° (45°). It can determine where to stop
because when y = x, it has reached 45°. The reason for using these angles is shown in the
above picture: As y increases, it does not skip nor repeat any y value until reaching 45°. So
during the while loop, y increments by 1 each iteration, and x decrements by 1 on occasion,
never exceeding 1 in one iteration. This changes at 45° because that is the point where the
tangent is rise=run. Whereas rise>run before and rise<run after.
Algorithm:
Flowchart:
Code:
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <dos.h>
using namespace std;
while(x<y)
{
if(p<0)
p += 2*x + 1;
else
{
y--;
p += 2*x - 2*y + 1;
}
x++;
pixel(xc,yc,x,y);
}
getch();
closegraph();
return 0;
}
Output:
Discussion:
In this algorithm we first calculate the value of pk as 1-r then we start the loop and run it
while(x<y). In this if (pk<0) then x=x+1 and pk = pk + (2*x) + 1; else x=x+1, y=y-1
and pk = pk + (2*x)+ (2*y) +1 and then called function Pixel(xc,yc,x,y) to plot points.
Findings and Learnings :
• The midpoint method for deriving efficient scan - conversion algorithms to draw
geometric curves on raster displays in described. The method is general and is used to
transform the nonparametric equation f(x,y) = 0, which describes the curve, into an
algorithms that draws the curve.
• Time Consumption is high.
• The distance between the pixels is not equals so we wont get smooth circular arc.