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

Computer Graphics

Computer Graphics
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Computer Graphics

Computer Graphics
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Graphics

Line drwaing algorithm:


A commonly used line drawing algorithm in computer graphics is Bresenham's Line
Drawing Algorithm. It's an efficient way to determine which pixels to plot to form a
straight line between two points, using only integer arithmetic, avoiding floating-
point operations.

Bresenham's Line Drawing Algorithm

Explanation:

Bresenham's algorithm works by determining the points that need to be plotted


between the starting and ending coordinates of a line. It decides which pixel is the
closest to the ideal line and increments either the x or y coordinate to follow that
path.

Algorithm Steps:

1. Input: Two endpoints of the line (x1, y1) and (x2, y2).

2. Initialize:

 Compute dx = x2 - x1 (change in x) and dy = y2 - y1 (change in y).


 Set the initial decision variable d = 2dy - dx.
 Choose the starting point (x1, y1).

3. Iterate:

 Plot the starting point.


 At each step:

 If d > 0, increment the y-coordinate (or y-coordinate if the


slope is steep), and update d = d + 2(dy - dx).
 Otherwise, increment the x-coordinate (or x-coordinate if the
slope is steep), and update d = d + 2dy.

 Repeat this process until the endpoint is reached.


DDA algorithm

The DDA (Digital Differential Analyzer) Algorithm is another line-drawing algorithm


used in computer graphics. It works by calculating intermediate points between two
endpoints of a line, based on the slope of the line. Unlike Bresenham's algorithm,
which works with integer calculations, DDA uses floating-point arithmetic, making it
conceptually simpler but potentially slower due to the use of floating-point
operations.

Steps of the DDA Algorithm:

1. Input: Two endpoints of the line (x1, y1) and (x2, y2).

2. Compute:

 The difference between the endpoints: dx = x2 - x1, dy = y2 - y1.


 Compute the steps to generate the line:

 If |dx| > |dy|, then the number of steps will be steps = |dx|.
 Otherwise, steps = |dy|.

3. Increment: Compute the increment in x and y for each step:

 x_increment = dx / steps
 y_increment = dy / steps

4. Initialize the starting point: Start with (x = x1, y = y1) and then keep adding the
increments to x and y in each step.

5. Iterate: For each step, plot the current (x, y) position and increment x and y until
the endpoint is reached.
Circle drwaing algorithm:
A circle drawing algorithm is a computational method used to draw a circle on a
grid-like structure, such as pixels on a screen, where ideal circular shapes may not
exist due to the discrete nature of the grid. These algorithms are designed to
approximate a circle's curve in a visually accurate way, ensuring symmetry and
smoothness.

Midpoint Circle Algorithm

The Midpoint Circle Algorithm is an efficient way to draw a circle on a pixel-based


display. It is a refinement of Bresenham’s Circle Algorithm and uses the same
principle of symmetry and incremental calculations but is slightly different in how it
calculates the next pixel to draw.

Key Concept of Midpoint Circle Algorithm:

The midpoint circle algorithm uses a decision parameter to determine the closest
pixel to the actual circle. It starts at the top of the circle (on the positive y-axis) and
proceeds outward, using the decision parameter to choose between moving
horizontally or diagonally to the next pixel. It calculates only one-eighth of the circle
(one octant) and mirrors the points into the other seven octants.

Circle Equation:

For a circle centered at (xc, yc) with radius r, the equation of the circle is:

(x−xc)2+(y−yc)2=r2(x - xc)^2 + (y - yc)^2 = r^2(x−xc)2+(y−yc)2=r2

Rather than calculating each point directly using this equation (which would require
square roots and floating-point operations), the midpoint algorithm uses
incremental integer arithmetic to determine the next point efficiently.

Steps in the Midpoint Circle Algorithm:

1. Input: The center of the circle (xc, yc) and the radius r.

2. Initialize:

 Start at the top of the circle at (x = 0, y = r).


 Set the initial decision parameter p = 1 - r.

3. Iterate: For each point (x, y) in one octant:


 Plot the point (x, y) and use symmetry to plot the points in the other
seven octants.
 Update the decision parameter p:

 If p < 0: The midpoint is inside the circle, so move horizontally


(increment x).
 If p >= 0: The midpoint is outside or on the circle, so move
diagonally (increment x and decrement y).

4. Repeat: Continue until x >= y.

Characteristics of the Midpoint Circle Algorithm:

1. Integer Arithmetic: It uses only integer addition and subtraction, which


makes it fast and efficient.
2. Symmetry: By calculating points in one octant and using symmetry to reflect
them, the algorithm reduces the number of points it needs to compute.
3. No Floating-Point Calculations: The algorithm avoids floating-point
arithmetic, making it suitable for low-power systems.
4. Accuracy: The circle is drawn accurately with a smooth appearance despite
using discrete pixels.

Advantages:

 Efficient: It only requires simple addition and subtraction operations, making


it faster than methods requiring floating-point calculations.
 Symmetry Utilization: By calculating points for only one-eighth of the circle
and reflecting them, the algorithm reduces computation.
 Easy to Implement: It is straightforward to implement in any programming
language that supports basic arithmetic and plotting.

Conclusion:

The Midpoint Circle Algorithm is an efficient and simple way to draw circles using
raster graphics. It exploits symmetry to reduce the number of points that need to be
calculated, and it uses a decision parameter to determine whether to move
horizontally or diagonally at each step. This approach avoids floating-point
arithmetic, making it well-suited for systems with limited computational power.

Bresenham's Circle Algorithm

Bresenham's Circle Algorithm is an efficient and widely used algorithm for drawing
circles on a raster display (like a computer screen) with pixels. Like Bresenham's Line
Algorithm, it uses only integer arithmetic, making it faster than algorithms that rely
on floating-point calculations. This algorithm works by stepping through one octant
of the circle and using symmetry to plot the remaining points.
Key Idea Behind Bresenham's Circle Algorithm:

A circle is defined by the equation:

(x−xc)2+(y−yc)2=r2(x - xc)^2 + (y - yc)^2 = r^2(x−xc)2+(y−yc)2=r2

Where:

 (xc, yc) is the center of the circle,


 r is the radius of the circle.

Rather than solving this equation for each pixel, Bresenham’s Circle Algorithm uses a
decision parameter to incrementally step through the circle’s points in a raster grid.
It calculates the closest pixel that approximates the ideal circle using only integer
arithmetic.

Symmetry of a Circle:

A circle has eightfold symmetry, meaning that if you calculate the points in one
octant (from 0° to 45°), you can reflect those points to cover the remaining seven
octants. This reduces the number of points that need to be calculated.

For example, if you calculate the point (x, y) in the first octant, the following points
can be reflected in the other octants:

1. (x, y) (Octant 1)
2. (-x, y) (Octant 2)
3. (x, -y) (Octant 3)
4. (-x, -y) (Octant 4)
5. (y, x) (Octant 5)
6. (-y, x) (Octant 6)
7. (y, -x) (Octant 7)
8. (-y, -x) (Octant 8)

Algorithm Steps:

1. Input: The center of the circle (xc, yc) and the radius r.

2. Initialize:

 Start at the top of the circle: (x = 0, y = r).


 Set the initial decision parameter p = 3 - 2r.
3. Iterate: For each point (x, y):

 Plot the point (x, y) and use symmetry to plot the points in the other
seven octants.
 Update the decision parameter p:

 If p < 0, the next point is (x + 1, y), and the decision parameter is


updated as p = p + 4x + 6.
 If p >= 0, the next point is (x + 1, y - 1), and the decision parameter
is updated as p = p + 4(x - y) + 10.

4. Repeat: Continue until x >= y.

Benefits of Bresenham's Circle Algorithm:

1. Efficiency: It uses only integer arithmetic, which makes it very fast and well-
suited for computer graphics.
2. No Floating-Point Calculations: The algorithm avoids floating-point
operations entirely.
3. Symmetry Utilization: By calculating points for just one-eighth of the circle
and using symmetry, the algorithm reduces the number of calculations.
4. Accurate Rendering: It produces smooth and accurate circles on raster
displays.

Summary of Key Operations:

 Decision Parameter (p): Determines whether to move horizontally or


diagonally.
 Symmetry: Each calculated point is reflected into the other octants using
symmetry, reducing the number of necessary calculations by a factor of 8.
 Integer Arithmetic: No floating-point calculations are used, making the
algorithm efficient and fast.

Conclusion:

Bresenham's Circle Algorithm is a highly efficient and fast algorithm for drawing
circles on pixel-based displays. It leverages integer arithmetic and symmetry,
avoiding complex operations like square roots and trigonometric functions. This
makes it ideal for real-time graphics applications where speed and efficiency are
important.

You might also like