Computer Graphics Class Ch 4
Computer Graphics Class Ch 4
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.
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