Computer Graphics (Co-313) LAB: Submitted By: Mohit Ranjan 2K17/CO/188
Computer Graphics (Co-313) LAB: Submitted By: Mohit Ranjan 2K17/CO/188
LAB
(D1 – G3)
Submitted by:
Mohit Ranjan
2K17/CO/188
INDEX
S. No. Experiment Date Signature
Experiment-1
Aim:
Program to implement simple DDA algorithm.
Description:
In a 2-D plane if we connect two points (x0, y0) and (x1, y1), we get a
line segment. But in computer graphics we can’t directly join any two
coordinate points, for that we calculate intermediate point’s coordinate
and put a pixel for each intermediate point, of the desired color with
help of functions. For generating any line segment we need
intermediate points and for calculating them we have can use a basic
algorithm called DDA (Digital differential analyzer) line generating
algorithm.
Algorithm:
Digital Differential Analyzer (DDA) 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
Steps = absolute(dx);
else
Steps = absolute(dy);
x = x + Xincrement ;
y = y + Yincrement ;
Code:
#include<bits/stdc++.h>
#include<graphics.h>
#include<stdlib.h>
using namespace std;
void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}
void DDA(int X0, int Y0, int X1, int Y1)
{
int dx = X1 - X0;
int dy = Y1 - Y0;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float Xinc = dx / (float) steps;
float Yinc = dy / (float) steps;
float X = X0;
float Y = Y0;
for (int i = 0; i <= steps; i++)
{
putpixel (X,Y,RED);
X += Xinc;
Y += Yinc;
}
}
int main() {
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
DDA(0,0, 500, 400);
wait_for_char();
closegraph();
return 0;
}
Output:
Discussion:
Advantages:
1. It is the simplest algorithm and it does not require special skills for
implementation.
2. It is a faster method for calculating pixel positions than the direct use of equation
y = mx + b. It eliminates the multiplication in the equation by making use of raster
characteristics.
Disadvantages:
1. Floating point arithmetic in DDA algorithm is still time-consuming.
2. The algorithm is orientation dependent. Hence end point accuracy is poor.
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
x = x + Xincrement ;
y = y + Yincrement ;
Code:
#include<stdio.h>
#include<graphics.h>
int dx = X1 - X0;
int dy = Y1 - Y0;
void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
int X0 = 3, Y0 = 8, X1 =150, Y1 = 170;
DDA(X0,Y0,X1,Y1);
delay(3000);
wait_for_char();
return 0;
}
Output:
Discussion:
Advantages
1. It is more faster than Simple DDA as it reduces calculations and
rounding off is done more efficiently.
2. In symmetric DDA e (i.e. the increment factor) is chosen such
that though both the co-ordinates of the resultant points has to be
rounded off, it can be done so very efficiently, thus quickly.
Specifically e is chosen as 1/2^n where 2^(n-1) <= max(|Δx|,|Δy|) <
2^n. In other words the length of the line is taken to be 2^n aligned.
Disadvantages
Description:
An accurate and efficient raster line-generating algorithm, developed by
Bresenham, which uses only incremental integer calculations. In addition, it can
also be adapted to display circles and other curves. The basic principle is to find
the optimum raster location to represent the straight line. To accomplish this, the
algorithm always increments x/y by one unit based on the flow of the line. Then,
the increment in other variable is found on the basis of the distance between the
actual line location & the nearest pixel.
Algorithm:
1. Input the two line endpoints and store the left endpoint in (x0, y0).
2. Set the color for frame-buffer position (x0, y0); i.e., plot the first point.
3. calculate δx, δy, 2δy and 2δy-δx
4. obtain P0 as follows:
5. P0 = 2δy - δx
6. δx = |X2-X1|
7. δy = |Y2-Y1|
8. At each xk along the line, starting at k = 0, perform the following test:
9. if(Pk < 0):
10. next_point = (Xk+1,Yk)
11. Pk+1 = Pk + 2δy
12. else:
13. next_point = (Xk+1,Yk+1)
14. Pk+1 = Pk + 2δy -2δx
15. Repeat lines 9-14 for δx-1 times.
Code:
#include<bits/stdc++.h>
#include<graphics.h>
#include<stdlib.h>
using namespace std;
void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}
void bresenham(int x0, int y0, int x1, int y1) {
int dx = abs(x1-x0), dy = abs(y1 - y0);
int twoDy = 2*dy;
int twoDyDx = 2*(dy - dx);
int p = 2*dy - dx;
int x, y, xEnd;
if(x0 > x1) {
x = x1;
y = y1;
xEnd = x0;
}
else {
x = x0;
y = y0;
xEnd = x1;
}
putpixel(x, y, YELLOW);
while(x < xEnd) {
x++;
if(p > 0) {
y++;
p += twoDyDx;
}
else {
p += twoDy;
}
cout<<"("<<x<<","<<y<<")\n";
putpixel(x , y , YELLOW);
}
}
int main() {
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
bresenham(0, 0, 200, 100);
wait_for_char();
closegraph();
return 0;
}
Output:
Aim:
To draw a circle using the Mid-Point Algorithm
Description:
It runs the same screen pass as in the Bresenham’s Algorithm and in each screen
pass 8 points are drawn simultaneously. This is possible by the dual-axis symmetry
of the circle. Only the first half of the first quadrant is calculated, rest of the
points are their symmetric reflections along different axis.
Algorithm:
1. Input radius r and circle center (xc , yc )
2. Set the coordinates for first point as:
3. (X0,Y0) = (0,r)
4. Calculate the initial value of the decision parameter as:
5. P0 = 5/4 - r
6. At each xk position, starting at k = 0, perform the following test:
7. if(Pk < 0):
8. next_point = (Xk+1,Yk)
9. Pk+1 = Pk + 2Xk+1 + 1
10. else:
11. next_point = (Xk+1, Yk-1)
12. Pk+1 = Pk + 2Xk+1 + 1 - 2Yk+1
13. //2xk+1 = 2xk + 2 and 2yk+1 = 2yk − 2.
14. Determine symmetry points in the other seven octants.
15. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and plot the coordinate
values as follows:
16. x = x + Xc
17. y = y + Yc
18. repeat Lines 6-17 until x>=y
Code:
#include<bits/stdc++.h>
#include<graphics.h>
#include<stdlib.h>
using namespace std;
void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}
cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
putpixel(y+x_centre, x+y_centre, RED);
cout << "(" << -y + x_centre << ", " << x + y_centre << ")\n";
putpixel(-y+x_centre, x+y_centre, RED);
cout << "(" << -x + x_centre << ", " << y + y_centre << ") ";
putpixel(-x+x_centre, y+y_centre, RED);
cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
putpixel(x+x_centre, -y+y_centre, RED);
cout << "(" << -x + x_centre << ", " << -y + y_centre << ")\n";
putpixel(-x+x_centre, -y+y_centre, RED);
// If the generated point is on the line x = y then
// the perimeter points have already been printed
if (x != y)
{
cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
putpixel(y+x_centre, x+y_centre, RED);
cout << "(" << -y + x_centre << ", " << x + y_centre << ") ";
putpixel(-y+x_centre, x+y_centre, RED);
cout << "(" << y + x_centre << ", " << -x + y_centre << ") ";
putpixel(y+x_centre, -x+y_centre, RED);
cout << "(" << -y + x_centre << ", " << -x + y_centre << ")\n";
putpixel(-y+x_centre, -x+y_centre, RED);
}
}
}
// Driver code
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
midPointCircleDraw(getmaxx()/2, getmaxy()/2, 100);
wait_for_char();
closegraph();
return 0;
}
Output:
Aim:
To draw an ellipse using the Mid-Point Algorithm
Description:
● Ellipses in Computer Graphics:
o Just like lines, ellipses are another primitive shape used in computer
graphics.
o An ellipse is defined as the set of points such that the sum of the
distances from two fixed positions (foci) is the same for all points. If
the distances to the two foci from any point on the ellipse are labeled
d 1 and d 2 , then the general equation of an ellipse can be stated as :
o Ellipse equations are greatly simplified if the major and minor axes
are oriented to align with the coordinate axes. An ellipse in "standard
position" has major and minor axes oriented parallel to the x and y
axes respectively.
Algorithm:
1. Input radii rx and ry and ellipse center (xc, yc)
2. Set the coordinates for first point as:
3. (X0,Y0) = (0,r)
4. Calculate the initial value of the decision parameter as:
5. P10 = ry^2 + rx^2/4 - rx^2ry
6. At each xk position in octant 1, starting at k = 0, perform the following test:
7. if (P1k < 0):
8. next_point = (Xk+1,Yk)
9. P1 (k+1) = P1k + 2ry^2x(k+1) + ry^2
10. Else:
11. next_point = (Xk+1, Yk-1)
12. P1(k+1) = P1k + 2ry^2x(k+1) + ry^2 - 2rx^2y(k+1)
14. Determine symmetry points in the other three octants.
15. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and
plot the coordinate values as follows:
16. x = x + Xc
17. y = y + Yc
18. Repeat Lines 7-17 until 2ry^2x >= 2rx^2y.
19. Calculate the initial value of the decision parameter as:
20. p20 = ry^2(x0 + 1/2)^2 + rx^2(y0 - 1)^2 - rx^2ry^2
21. At each xk position in octant 2, starting at k = 0, perform the following test:
22. if(P2k > 0):
23. next_point = (Xk,Yk-1)
24. P2(k+1) = P2k - 2rx^2y(k+1) + rx^2
25. else:
26. next_point = (Xk+1,Yk-1)
27. P2(k+1) = P2k - 2rx^2y(k+1) + rx^2 - 2rx^2y(k+1)
28. Determine symmetry points in the other three octants.
29. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and
plot the coordinate values as follows:
30. x = x + Xc
31. y = y + Yc
32. Repeat lines 22 - 31 till y > 0
Code:
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;
void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}
// For region 1
while (dx < dy)
{
// Driver code
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
midptellipse(200, 150, getmaxx()/2, getmaxy()/2);
wait_for_char();
closegraph();
return 0;
}
Output:
Findings and Learnings:
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.
3. Time consumption is high.
4. The distance between the pixels is not equal so we won’t get smooth circle.
Experiment-6
Aim:
To write a program and implement Flood Fill Algorithm
Description:
Sometimes we come across an object where we want to fill the area and its
boundary with different colors. We can paint such objects with a specified interior
color instead of searching for particular boundary color as in boundary filling
algorithm.
Instead of relying on the boundary of the object, it relies on the fill color. In other
words, it replaces the interior color of the object with the fill color. When no more
pixels of the original interior color exist, the algorithm is completed.
In Flood Fill algorithm we start with some seed and examine the neighboring
pixels, however pixels are checked for a specified interior color instead of
boundary color and is replaced by a new color. It can be done using 4 connected
or 8 connected region method.
Algorithm:
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and 5 till the
boundary pixels reached.
Step 4 − Change the default color with the fill color at the seed point.
Code:
#include <graphics.h>
#include <stdio.h>
void flood(int x, int y, int new_col, int old_col)
{
if (getpixel(x, y) == old_col) {
putpixel(x, y, new_col);
flood(x + 1, y, new_col, old_col);
flood(x - 1, y, new_col, old_col);
flood(x, y + 1, new_col, old_col);
flood(x, y - 1, new_col, old_col);
flood(x-1, y - 1, new_col, old_col);
flood(x-1, y + 1, new_col, old_col);
flood(x+1, y - 1, new_col, old_col);
flood(x+1, y + 1, new_col, old_col);
}
}
int main()
{
int gd, gm = DETECT;
Aim:
To implement Boundary Fill Algorithm
Description:
The boundary fill algorithm works as its name. This algorithm picks a point inside
an object and starts to fill until it hits the boundary of the object. The color of the
boundary and the color that we fill should be different for this algorithm to work.
In this algorithm, we assume that color of the boundary is same for the entire
object. The boundary fill algorithm can be implemented by 4-connected pixels or
8-connected pixels.
Algorithm:
1. Create a function named as boundaryfill with 4 parameters
(x,y,f_color,b_color).
Code:
#include <graphics.h>
//driver code
int main()
{
1. Boundary Fill is used for the coloring figures in computer graphics. Here,
area gets colored with pixels of a chosen color as boundary this giving the
technique its name.
2. Boundary fill fills the chosen area with a color until the given colored
boundary is found.
3. This algorithm is also recursive in nature as the function returns when the
pixel to be colored is the boundary color or is already the fill color.