0% found this document useful (0 votes)
18 views

lab report on computer graphics

The document provides an overview of computer graphics, focusing on key algorithms such as DDA, Bresenham's, and Midpoint Circle and Ellipse Drawing Algorithms. It includes step-by-step instructions for implementing these algorithms in C programming, along with example code and common issues. The document emphasizes the importance of these algorithms in rendering accurate and efficient graphics in various applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

lab report on computer graphics

The document provides an overview of computer graphics, focusing on key algorithms such as DDA, Bresenham's, and Midpoint Circle and Ellipse Drawing Algorithms. It includes step-by-step instructions for implementing these algorithms in C programming, along with example code and common issues. The document emphasizes the importance of these algorithms in rendering accurate and efficient graphics in various applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Introduction to Computer Graphics:


Computer graphics is a branch of computer science that focuses on creating, manipulating, and
displaying visual content using computational techniques. It plays a pivotal role in various fields,
including entertainment, design, education, and scientific research. The ability to create realistic
and interactive visual experiences relies on efficient algorithms for rendering geometric shapes.

 Key Algorithms in Computer Graphics:

1) Digital Differential Analyzer (DDA) Algorithm:

o Used to draw lines by incrementally plotting points between two endpoints.


o Utilizes floating-point calculations for precision, making it simple to implement.

2) Bresenham's Line Drawing Algorithm:

o An efficient alternative to DDA for drawing lines.


o Works with integer arithmetic, minimizing computational overhead and
improving performance.

3) Midpoint Circle Drawing Algorithm:

o A modification of Bresenham’s algorithm tailored for circle rendering.


o Utilizes symmetry and a decision parameter to efficiently determine pixel
positions for a smooth circular arc.

4. Midpoint Ellipse Drawing Algorithm:

o Extends the principles of the midpoint circle algorithm to render ellipses.


o Divides the calculation into two regions (based on slope) for accurate and
efficient plotting of points.

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:

Steps involving to implement computer graphics program include are as follow:


Steps:

To implement a computer graphics program in C using Dev-C++, follow these steps:

1. Install and Configure Dev-C++:

 Download and install Dev-C++ from its official website.


 Install the WinBGIm Graphics Library (needed for graphics.h):
1. Download WinBGIm from WinBGIm site.
2. Copy the downloaded files:
 graphics.h → Dev-C++\MinGW64\include
 libbgi.a → Dev-C++\MinGW64\lib

2. Set Up Compiler Options:

 Open Dev-C++.
 Go to Tools > Compiler Options > Linker.
 Add the following linker options:

-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

3. Write the Program:

Here's a basic example program:

#include <graphics.h>
#include <conio.h>

int main() {
int gd = DETECT, gm;

initgraph(&gd, &gm, "");

circle(200, 200, 50);


rectangle(100, 150, 300, 250);

line(100, 100, 300, 300);

getch();

closegraph();

return 0;
}

OUTPUT:

4. Compile and Run:

 Save the file with a .c extension (e.g., graphics_demo.c).


 Click Compile & Run (F11) in Dev-C++.
 If configured correctly, the graphics window should appear.

5. Common Issues and Fixes:

 Error: Can't find "bgi" or "graphics.h":


o Ensure the graphics.h file is in the include folder and libbgi.a is in the lib folder.
 Blank Graphics Window:
o Check that the graphics mode initialization (initgraph) is correct and the paths are
set properly.
2. Implementation of Digital Differential Analyzer (DDA) a line
Drawing Algorithm.
 Objective:

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

 x is incremented by 1 unit, and the corresponding y is calculated using:

yk+1 = yk + m, xk+1 = xk + 1

Case 2: ∣m∣>1

 y is incremented by 1 unit, and the corresponding x is calculated using:

xk+1 = xk + 1/m, yk+1 = yk + 1

Case 3: |m|=1

 For slope euqals to 1 both xk+1 and yk+1 increamented by 1 uint.

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:

1. Simple and easy to implement.


2. Handles all types of slopes effectively.
3. Provides smooth and continuous lines for graphics rendering.

 Program code:
#include <graphics.h>
#include <math.h>
#include <stdio.h>

void drawLineDDA(int x1, int y1, int x2, int y2) {


int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int dx = x2 - x1, dy = y2 - y1;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float x_inc = dx / (float)steps;
float y_inc = dy / (float)steps;
float x = x1, y = y1;
for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE);
x += x_inc;
y += y_inc;
}
getch();
closegraph();
}

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,

CASE 1: If m < 1 and P0 < 0 so,

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

CASE 2 : If m >= 1 and P0 < 0 so,

i. xk+1 = xk (No change)


ii. yk+1 = yk + 1
iii. P1 = P0 + 2dx
OR, P0 >= 0 ;
i. xk+1 = xk + 1
ii. yk+1 = yk + 1
iii. P1 = P0 + 2dx – 2dy

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>

void drawLineBresenham(int x1, int y1, int x2, int y2) {


int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int dx = x2 - x1, dy = y2 - y1;
int p = 2 * dy - dx;
int x = x1, y = y1;
putpixel(x, y, WHITE);
while (x < x2) {
x++;
if (p < 0) {
p = p + 2 * dy;
} else {
p = p + 2 * (dy - dx);
y++;
}
putpixel(x, y, WHITE);
}
getch();
closegraph();
}

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

OR, Pk >= 0 then,

i. (xk+1 , yk-1) and


ii. Pk+1 = Pk + 2xk+1 + 1 – 2yk+1
where,
 2xk+1 = 2xk + 2
 2yk+1 = 2yk – 2
5) Determine symmetry point on other seven octants.
6) Move each Calculated pixel position (x , y) onto (xc , yc) and
 x = x + xc
 y = y + yc
7) Repeat step(3) until “x >= y” .
8) For all points add the center point (xc , yc).
9) Stop

 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 :

 P10 = ry2 + 1/4 . rx2 – rx2.ry

 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

Or, If P1k >= 0 then next point is ,


a) (xk+1 , yk-1) and
b) P1k+1 = P1k + 2.ry2 .xk+1 - 2.rx2 .yk+1 + ry2

5) Now, Change the region if slope [ ellipse <= ‘1’].


6) Obtain the initial value in region 2 using the last point (x0 , y0) of region 1 as:

 P20 = ry2.( x0 + ½)2 + rx2.(y0 - 1) – rx2.ry2

 If P2k > 0 then next point is ,

a) (xk+1 , yk-1) and


b) P2k+1 = P2k – 2.rx2.yk+1 + rx2
Or, If P2k < o then , next point become ,

a) (xk+1 , yk-1) and


b) P2k+1 = P2k + 2.ry2.xk+1 – 2.rx2.yk+1 + rx2

7) Now, obtain the symmetric points in the three quadrants and plot the coordinate
value as :
 x = x + xc
 y = y + yc

8) Repeat the state for region 1 until ;


 2.ry2.x >= 2.rx2.y

9) Stop

 Program code:
#include <graphics.h>
#include <stdio.h>

void drawEllipseMidpoint(int xc, int yc, int a, int b) {


int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x = 0, y = b;
float a2 = a * a, b2 = b * b;
float p1 = b2 - a2 * b + a2 / 4;

// 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.

You might also like