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

Computer Graphics Class Ch 4

Chapter 4 discusses graphics output primitives, including points, lines, and curves, which are essential for creating visual representations in computer graphics. It details line drawing algorithms such as the Digital Differential Analyzer (DDA) and Bresenham's algorithm, which efficiently approximate straight lines using integer arithmetic. Additionally, the chapter introduces circle generating algorithms, specifically the midpoint circle drawing algorithm, to create circles based on a given radius and center.

Uploaded by

Abebe Tigabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Computer Graphics Class Ch 4

Chapter 4 discusses graphics output primitives, including points, lines, and curves, which are essential for creating visual representations in computer graphics. It details line drawing algorithms such as the Digital Differential Analyzer (DDA) and Bresenham's algorithm, which efficiently approximate straight lines using integer arithmetic. Additionally, the chapter introduces circle generating algorithms, specifically the midpoint circle drawing algorithm, to create circles based on a given radius and center.

Uploaded by

Abebe Tigabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter-4

Graphics Output Primitives


• Output primitives are the basic geometric
structures which facilitate or describe a
scene/picture. Example of these include:

– points, lines, curves (circles, conics


etc), surfaces, fill colour, character
string etc.

1
Points
• A point is shown
by illuminating
a pixel on the
screen

2
Lines
• A line segment is completely defined in
terms of its two endpoints.
• A line segment is thus defined as:
Line_Seg = { (x1, y1), (x2, y2) }

3
Lines
y
• A line is
produced by
means of
illuminating a
y2
set of
intermediary
pixels between y1

the two
endpoints.
x
x1 x2

4
Lines
• Lines is digitized into a set of discrete
integer positions that approximate the
actual line path.
• Example: A computed line position of
(10.48, 20.51) is converted to pixel
position (10, 21).

5
Line
• The rounding of coordinate values to
integer causes all but horizonatal and
vertical lines to be displayed with a stair
step appearance “the jaggies”.

6
Line Drawing Algorithms
 • A straight line segment is defined by
the coordinate position for the end
points of the segment.
 • Given Points (x1, y1) and (x2, y2)

7
DDA Algorithm (Digital Differential Analyzer)
 The Digital Differential Analyzer (DDA) algorithm is a basic line drawing
algorithm used in computer graphics. It is a simple and efficient method
for generating a series of points to approximate a straight line between
two given endpoints.

 The DDA algorithm works by calculating the incremental change in the x


and y coordinates and then using these increments to determine the
next pixel position along the line.
 Here is a step-by-step explanation of the DDA algorithm:
1. Input the coordinates of the two endpoints (x0, y0) and (x1, y1).
2. Calculate the differences between the x and y coordinates: dx = x1 - x0
and dy = y1 - y0. 8
3. Determine the number of steps required to reach from (x0, y0) to (x1, y1)
using the larger of |dx| and |dy|: steps = max(|dx|, |dy|).
4. Calculate the increments for each step along the x and y axes:
x_increment = dx / steps and y_increment = dy / steps.
5. Initialize the current coordinates to (x0, y0): x = x0 and y = y0.
6. Round off the current coordinates to the nearest integer values: x_rounded
= round(x) and y_rounded = round(y).
7. Plot the rounded coordinates (x_rounded, y_rounded) as the first point on
the line.
8. Repeat steps 9 and 10 until the desired endpoint (x1, y1) is reached.
9. Update the current coordinates by adding the increments: x = x +
x_increment and y = y + y_increment.
10. Round off the updated coordinates: x_rounded = round(x) and y_rounded
= round(y).
11. Plot the rounded coordinates (x_rounded, y_rounded) as the next point9 on
the line.
12. Repeat steps 9-11 until the desired endpoint is reached.

Example 1: Drawing a line from (1, 2) to (7, 5)


Step 1: Input the coordinates: (x0, y0) = (1, 2) and (x1, y1) = (7, 5).
Step 2: Calculate the differences: dx = 7 - 1 = 6 and dy = 5 - 2 = 3.
Step 3: Determine the number of steps: steps = max(|dx|, |dy|) = 6.
Step 4: Calculate the increments: x_increment = dx / steps = 6 / 6 = 1
and y_increment = dy / steps = 3 / 6 = 0.5.
Step 5: Initialize current coordinates: x = x0 = 1 and y = y0 = 2.
Step 6: Round off current coordinates: x_rounded = round(x) = 1 and
y_rounded = round(y) = 2.
Step 7: Plot the rounded coordinates: (1, 2).
Step 8: Repeat steps 9 and 10 until the desired endpoint is reached.
10
Step 9: Update current coordinates: x = x + x_increment = 1 + 1 = 2
and y = y + y_increment = 2 + 0.5 = 2.5.
Step 10: Round off updated coordinates: x_rounded = round(x) = 2
and y_rounded = round(y) = 3.
Step 11: Plot the rounded coordinates: (2, 3).
Step 12: Repeat steps 9-11 until the desired endpoint is reached.
Continuing this process, we get the following points on the line: (3, 3),
(4, 4), (5, 4), (6, 5), (7, 5).
Exercise: Draw a vertical line from (3, 2) to (3, 7)
Bresenham’s line drawing algorithm
The algorithm works by determining the most suitable pixel positions to
approximate a straight line between two given points. It avoids the
need for floating-point calculations and uses only integer arithmetic
11
operations, making it computationally efficient.
Here's a high-level overview of how Bresenham's algorithm works:
1.Given two points P1(x1, y1) and P2(x2, y2), calculate the differences
in x and y coordinates:
dx = x2 - x1
dy = y2 - y1
2. Determine the increments (positive or negative) for x and y:
incX = 1 if dx > 0, otherwise -1
incY = 1 if dy > 0, otherwise -1
3. Calculate the decision parameter (d) based on the differences in x
and y:
d = 2 * dy - dx

12
5. Initialize the starting point:
Set current pixel position: x = x1, y = y1
6. Start plotting the line:
Plot the starting point (x, y).
7. Iterate along the line until reaching the end point (x2, y2):
If d < 0, move to the next pixel to the right:
Increment x by incX: x = x + incX
Update the decision parameter: d = d + 2 * dy
If d >= 0, move diagonally to the right and up:
Increment x by incX: x = x + incX
Increment y by incY: y = y + incY
Update the decision parameter: d = d + 2 * dy - 2 * dx
Plot the current pixel position (x, y).
13
Here's an example of how Bresenham's line algorithm can be used to
draw a line on a grid:
Let's say we want to draw a line between two points: P1(2, 3) and
P2(8, 6) on a grid.
Step 1: Calculate the differences in x and y coordinates between the
two points:
dx = P2.x - P1.x = 8 - 2 = 6
dy = P2.y - P1.y = 6 - 3 = 3
Step 2: Calculate the increments (positive or negative) for x and y:
incX = 1 (since dx is positive)
incY = 1 (since dy is positive)
Step 3: Calculate the decision parameter (d):
d = 2 * dy - dx
Step 4: Initialize the starting point:
x = P1.x = 2
y = P1.y = 3

14
Now, we start drawing the line:
Step 5: Plot the starting point (2, 3) on the grid.
Step 6: Repeat until we reach the end point (P2):
If d < 0, move to the next pixel to the right:
Increment x by 1: x = x + incX
Update the decision parameter: d = d + 2 * dy
If d >= 0, move to the next pixel diagonally to the right and up:
Increment x by 1: x = x + incX
Increment y by 1: y = y + incY
Update the decision parameter: d = d + 2 * dy - 2 * dx
Here's how the algorithm progresses:
Iteration 1:
Plot the pixel at (2, 3).
Iteration 2:
Since d = 2 * 3 - 6 = 0, the line is closer to the diagonal pixel. Move diagonally.
Increment x by 1: x = 3
Increment y by 1: y = 4
Update d = d + 2 * 3 - 2 * 6 = 0 + 6 - 12 = -6

15
Iteration 3:
Since d = -6, the line is closer to the pixel on the right. Move to the
right.
Increment x by 1: x = 4
Update d = -6 + 2 * 3 = 0
Iteration 4:
Since d = 0, the line is closer to the diagonal pixel. Move diagonally.
Increment x by 1: x = 5
Increment y by 1: y = 5
Update d = 0 + 2 * 3 - 2 * 6 = -6
Iteration 5:
Since d = -6, the line is closer to the diagonal pixel. Move diagonally.
Increment x by 1: x = 6
Increment y by 1: y = 5
Update d = -6+2*3= 0

16
Iteration 6:
Since d = 0, the line is closer to the diagonal pixel. Move diagonally.
Increment x by 1: x = 7
Increment y by 1: y = 6
Update d = 0 + 2 * 3 - 2 * 6 = -6

Iteration 7:
Since d = -6, the line is closer to the diagonal pixel. Move diagonally.
Increment x by 1: x = 8
Increment y by 1: y = 6
Now, we have reached the end point (8, 6).

17
Circle Generating Algorithms
•Mid point circle drawing algorithm

To draw the circle for a given radius ‘r’ and centre(Xc,Yc) we will start
from(0,r)and move in first quadrant till x=y.
d=3-2r
X=0,y=r
1. Initial point(x,y)=(0,r)
Decision parameter p=1-r
2.If p<0,then
(Xk,Yk)=(X+1,Y)
Pk=P+2X+3
Else if P>=0,then
(Xk,Yk)=(X+1,Y-1)
Pk=P+2(X-Y)+5

18
Making a circle in a quadrant

19
Example Draw a circle where center is (5,7) and
diameter=12,r=6

20
Thank
Thank You
You ...
...

21

You might also like