lab report on computer graphics
lab report on computer graphics
These algorithms form the basis of many advanced graphical applications. Learning and
implementing them in lab exercises enhances understanding of fundamental rendering
techniques, symmetry utilization, and algorithm optimization in computer graphics.
NECESSARY STEPS TO FOLLOW TO IMPLEMENT COMPUTER
GRAPHICS PROGRAM IN C-PROGRAMMING LANGUAGE WITH
AN SIMPLE EXAMPLE:
Open Dev-C++.
Go to Tools > Compiler Options > Linker.
Add the following linker options:
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
OUTPUT:
To implement the Digital Differential Analyzer (DDA) algorithm for line drawing in C
using graphics.
Algorithm(Steps):
1) Start
2) Get the input of two end points (x1 , y1) and (x2 , y2)
3) Calculate dx, dy and slope(m):
dx = x2 – x1
dy = y2 – y1
Slope(m) = dy/ dx
4) Find the next point by following 3 cases:
Case 1: ∣m∣<1
yk+1 = yk + m, xk+1 = xk + 1
Case 2: ∣m∣>1
Case 3: |m|=1
xk+1 = xk + 1 , yk+1 = yk + 1
5) We will repeat the step(4) until we find the ending point of the line.
6) Stop
Advantages of DDA Algorithm:
Program code:
#include <graphics.h>
#include <math.h>
#include <stdio.h>
int main() {
int x1, y1, x2, y2;
printf("Enter the starting point (x1, y1): ");
scanf("%d%d", &x1, &y1);
printf("Enter the ending point (x2, y2): ");
scanf("%d%d", &x2, &y2);
drawLineDDA(x1, y1, x2, y2);
return 0;
}
OUTPUT:
Conclusion:
The DDA algorithm is a simple and efficient method for drawing straight lines in
computer graphics. By calculating incremental changes based on the slope (mm), it
ensures accurate pixel plotting for all line orientations. Despite using floating-point
calculations, it remains a fundamental and easy-to-implement technique, ideal for
learning the basics of computer graphics.
3. Implementation of Bresenham’s Line Drawing Algorithm.
Objectives:
To implement the Bresenham's Line Drawing Algorithm for drawing a straight line
between two points using integer arithmetic.
Algorithm(Steps):
1) Start
2) Input the line ends point (x1, y1) to (x2 , y2) .we can perform the following steps:
i. Slope(m) = dx / dy
ii. y = y2 – y1
iii. x = x2 - x1
3) Calculate constants dx , dy and 2dy – 2dx and obtaining the storing value for
decision parameters as,
P0 = 2dy – dx
4) Check the condition of slope and decision parameter then,
i. xk+1 = xk + 1
ii. yk+1 = yk (No change)
iii. P1 = P0 + 2dy
OR, P0 >= 0 ;
i. xk+1 = xk + 1
ii. yk+1 = yk + 1
iii. P1 = P0 + 2dy – 2dx
5) We will repeat the step(4) until we fnd the ending points of the line.
6) Stop
Program code:
#include <graphics.h>
#include <stdio.h>
int main() {
int x1, y1, x2, y2;
printf("Enter the starting point (x1, y1): ");
scanf("%d%d", &x1, &y1);
printf("Enter the ending point (x2, y2): ");
scanf("%d%d", &x2, &y2);
drawLineBresenham(x1, y1, x2, y2);
return 0;
}
OUTPUT:
Conclusion:
The Bresenham's Algorithm efficiently draws accurate lines using integer arithmetic,
avoiding floating-point calculations. It is faster and suitable for rendering lines with
precise pixel placement in raster graphics.
4. Implementation of mid-point Circle Drawing Algorithm.
Objectives:
To implement the Midpoint Circle Drawing Algorithm to draw a circle using symmetry
and incremental calculations.
Algorithm(Steps):
1) Start
2) Let r, (xc , yc) and (x0 , yo) = (0,r)
3) Calculate P0 = 1 – r
4) If Pk < 0 then ,
i. (xk+1 , yk ) and
ii. Pk+1 = Pk + 2xk+1 + 1
Program code:
#include <graphics.h>
#include <stdio.h>
void drawCircleMidpoint(int xc, int yc, int r) {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x = 0, y = r, p = 1 - r;
while (x <= y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
x++;
if (p < 0) {
p += 2 * x + 3;
} else {
y--;
p += 2 * (x - y) + 5;
}
}
getch();
closegraph();
}
int main() {
int xc, yc, r;
printf("Enter the center of the circle (xc, yc): ");
scanf("%d%d", &xc, &yc);
printf("Enter the radius of the circle: ");
scanf("%d", &r);
drawCircleMidpoint(xc, yc, r);
return 0;
}
OUTPUT:
Conclusion:
The Midpoint Circle Drawing Algorithm efficiently plots circles by leveraging symmetry
and integer calculations. It avoids floating-point operations, ensuring fast and accurate
rendering, making it ideal for raster graphics.
5. Implementation of mid -point Ellipse Drawing Algorithm.
Objectives:
To implement the Midpoint Circle Drawing Algorithm to draw a circle using symmetry
and incremental calculations.
Algorithm(Steps):
1) Start
2) Take input radius along x-axis and y-axis and obtain centre of ellipse at (0,0).
3) Initially, we assume ellipse to be centered at origin and the first point as
(x , y0) = (0 , ry) .
4) Obtain the initial decision parameter for region ‘1’ as :
For every ‘xk’ position in region 1, if P1k < 0 then next point along the ,
a) (xk+1 , yk) and
b) P1k+1 = P1k + 2.ry2 .xk+1 + ry2
7) Now, obtain the symmetric points in the three quadrants and plot the coordinate
value as :
x = x + xc
y = y + yc
9) Stop
Program code:
#include <graphics.h>
#include <stdio.h>
// Region 1
while (2 * b2 * x <= 2 * a2 * y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
x++;
if (p1 < 0) {
p1 = p1 + 2 * b2 * x + b2;
} else {
y--;
p1 = p1 + 2 * b2 * x - 2 * a2 * y + b2;
}
}
// Region 2
float p2 = b2 * (x + 0.5) * (x + 0.5) + a2 * (y - 1) * (y - 1) - a2 * b2;
while (y >= 0) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
y--;
if (p2 > 0) {
p2 = p2 - 2 * a2 * y + a2;
} else {
x++;
p2 = p2 + 2 * b2 * x - 2 * a2 * y + a2;
}
}
getch();
closegraph();
}
int main() {
int xc, yc, a, b;
printf("Enter the center of the ellipse (xc, yc): ");
scanf("%d%d", &xc, &yc);
printf("Enter the semi-major axis (a): ");
scanf("%d", &a);
printf("Enter the semi-minor axis (b): ");
scanf("%d", &b);
drawEllipseMidpoint(xc, yc, a, b);
return 0;
}
OUTPUT:
Conclusion:
The Midpoint Ellipse Drawing Algorithm efficiently plots ellipses using symmetry and
integer-based calculations. It avoids floating-point operations, ensuring fast and accurate
rendering for raster graphics.