Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 50

CHAPTER 3

2D GRAPHICS ALGORITHMS
1
Output Primitives
Line Drawing Algorithms
DDA Algorithm
Midpoint Algorithm
Bersenhems Algorithm
Circle Drawing Algorithms
Midpoint Circle Algorithm
Antialising
Fill-Area Algorithms
2
3
The basic objects out of which a
graphics display is created are called.
Describes the geometry of objects and
typically referred to as geometric
primitives.
Examples: point, line, text, filled
region, images, quadric surfaces, spline
curves
Each of the output primitives has its
own set of attributes.
4
Points
Attributes: Size, Color.
glPointSize(p);
glBegin(GL_POINTS);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glEnd()

Lines
Attributes: Color, Thickness, Type
glLineWidth(p);
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()

Output Primitive Attributes
Point Size
Color
Line Thickness (1pt, 2pt )
Type (Dashed, Dotted, Solid)
Color
7
8
Line drawing is fundamental to computer graphics.
We must have fast and efficient line drawing functions.
Rasterization Problem: Given only the two end points, how
to compute the intermediate pixels, so that the set of pixels
closely approximate the ideal line.
The DDA Algorithm
The digital differential
analyzer (DDA) algorithm
takes an incremental
approach in order to speed
up scan conversion
Simply calculate y
k+1

based on y
k

The original differential analyzer
wa s a p h ys i c a l ma c h i n e
developed by Vannevar Bush at
MIT in the 1930s in order to
sol ve ordi nary di fferent i al
e q u a t i o n s .
Mo r e i n f o r ma t i o n h e r e .
11
The Digital Differential Analyzer
(DDA) Algorithm
m
x
y
=
A
A
x y
0 1
1 4
2 7
3 10
4 13
5 16
means that for a unit (1) change in x there is
m-change in y.
i.e. y = 3x + 1 m = 3
m y
x 1
=
A
A
means that for a unit (1) change in y there is
1/m change in x.
Do not use
y = 3x + 1
to calculate y.
Use m
12
The DDA Method
Uses differential equation of the line : m
If slope |m| s 1 then increment x in steps of 1 pixel
and find corresponding y-values.
If slope |m| > 1 then increment y in steps of 1 pixel
and find corresponding x-values.













step through in x step through in y
13
The DDA Method
Desired line
(x
i
,round(y
i
))
(x
i
,y
i
)
(x
i
+1,round(y
i
+m))
(x
i
+1,y
i
+m)
14
if slope m > 0
if |m| s 1
x
i+1
= x
i
+ 1
y
i+1
= y
i
+ m
if |m| > 1
y
i+1
= y
i
+ 1
x
i+1
= x
i
+ 1/m
Left
Right
Left
Right
15
Proceeding from right-endpoint to left-endpoint
if slope m > 0
if |m| s 1
x
i+1
= x
i
- 1
y
i+1
= y
i
- m
if |m| > 1
y
i+1
= y
i
- 1
x
i+1
= x
i
- 1/m
Left
Right
Left
Right
16
if slope m < 0
if |m| s 1
x
i+1
= x
i
+ 1
y
i+1
= y
i
+ m
if |m| > 1
y
i+1
= y
i
- 1
x
i+1
= x
i
- 1/m
Left
Right
Left
Right
17
Proceeding from right-endpoint to left-endpoint
if slope m < 0
if |m| s 1
x
i+1
= x
i
- 1
y
i+1
= y
i
- m
if |m| > 1
y
i+1
= y
i
+ 1
x
i+1
= x
i
+ 1/m
Left
Right
Left
Right
18
Example (DDA)

+ =
+ =
s s
+ =
+
+
3
1
1
1
3
1
1
1 0
1
i i
i i
y y
x x
m
x y
x y round(y)
0 1 1
1 4/3 1
2 5/3 2
3 2 2
4 7/3 2
5 8/3 3
6 3 3
7 10/3 3
8 11/3 4
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
y
x
19
Example (DDA)

=
=
<
+ =
+
+
) (
1
1
8 3
3
1
1
1
i i
i i
x x
y y
m
x y
y x round(x)
8 0 0
7 1/3 0
6 2/3 1
5 1 1
4 4/3 1
3 5/3 2
2 2 2
1 7/3 2
0 8/3 3
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
y
x
20
void LineDDA(int x0, int y0, int x1, int y1)
{
int dx = x1 x0, dy = y1 y0, steps;

if (abs(dx)>abs(dy)) steps = abs(dx);
else steps = abs(dy);

// one of these will be 1 or -1
double xIncrement = (double)dx / (double )steps;
double yIncrement = (double)dy / (double )steps;

double x = x0;
double y = y0;
setPixel(round(x), round(y));

for (int i=0; i<steps; i++) {
{
x += xIncrement;
y += yIncrement;
setPixel(round(x), round(y));
}
}
Note: The DDA algorithm is faster than the direct use of y = mx + c.
It eliminates multiplication; only one addition.
21
Example
Draw a line from point (2,1) to (12,6)
Draw a line from point (1,6) to (11,0)

7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12
22
Parallel Line Algorithms
Take advantage of multiple processors.
Given n
p
processors, subdivide the line path into n
p
Bresenham
segments.
For a line with slope 0 < m < 1 and leftpoint (x
0
,y
0
) the distance to
the right endpoint (left endpoint for next segment) is



where
Ax = width of the line
Ax
p
is computed using integer division

Numbering the segments, and the processors, as 0, 1, 2, , n
p
-1,
starting x-coordinate for the k
th
partition is
x
k
= x
0
+ kAx
p

p
p
p
n
n x
x
1 + A
= A
23
Parallel Line Algorithms (continue)
i.e. Ax = 15 , n
p
= 4 processors



Starting x-values at x
0
, x
0
+ 4, x
0
+ 8, x
0
+ 12

Ay
p
= mAx
p

At the k
th
segment, the starting y-coordinates is
y
k
= y
0
+ round(kAy
p
)
Also, the initial decision parameter for Bresenhams
algorithm at the start of the k
th
subinterval is:
p
k
= (kAx
p
)(2Ay) round(kAy
p
)(2Ax) + 2Ay Ax
4
4
18
4
1 4 15
= =
+
= A
p
x
Bresenham Line Algorithm
Jack Bresenham worked for
27 years at IBM before
e n t e r i n g a c a d e mi a .
Bresenham developed his
famous algorithms at IBM
i n t h e e a r l y 1 9 6 0 s

What you need to know about Bresenham LDA
1) Why we use it
2) Major idea of integer-izing a decision point
3) How this reduces things to just integer form.
(17,8)
(2,2)
(0,0)
(18,0)
(0,9)
26
Bresenham Line Algorithm
A more efficient approach
Basis of the algorithm:






From start position decide A or B next
A
B
Start position
27
Bresenham Line Algorithm
For a given value of x
one pixel lies at distance t
i
above the line, and
one pixel lies at distance s
i
below the line
True line
s
i

t
i
28
Bresenham Line Algorithm
Decision parameter

d
i
= (s
i
- t
i
)

If d
i
< 0, then closest pixel is below true line (s
i
smaller)
If d
i
> 0, then closest pixel is above true line (t
i
smaller)


We must calculate the new values for d
i
as we move
along the line.
29
3dy
2dy
dy
Example:
) 2 or 5 . 0 (i.e. 0.5 (slope) gradient Let dx dy
dx
dy
< < <
Start pixel at (x
0
,y
1
)
4dy
At x
1
:
s
1
= dy t
1
= dx - dy
d
1
= (s
i
- t
i
) = dy - (dx - dy) = 2dy - dx
but 2dy < dx d
i
< 0 y stays the same
hence next pixel is at (x
1
,y
1
)
At x
2
:
s
2
= 2dy t
2
= dx - 2dy
d
2
= (s
2
t
2
) = 2dy - (dx - 2dy) = 4dy - dx
Suppose d2 > 0 y is incremented
hence next pixel is at (x
2
,y
2
)

At x
3
:
s
3
= 3dy - dx t
2
= 2dx - 3dy
d
3
= (s
2
t
3
) = 6dy - 3dx < 0
so y stays the same
hence next pixel is at (x
3
,y
2
)
x
1
x
2
x
3
x
4
x
5
x
0
y
0
y
1
y
2
y
3
y
5
30
In General
For a line with gradient 1
d
0
= 2dy dx
if d
i
< 0 then y
i+1
= y
i
d
i+1
= d
i
+ 2dy
if d
i
0 then y
i+1
= y
i
+ 1

d
i+1
= d
i
+ 2(dy dx)
x
i+1
= x
i
+ 1
For a line with gradient > 1
d
0
= 2dx dy
if d
i
< 0 then x
i+1
= x
i
d
i+1
= d
i
+ 2dx
if d
i
0 then x
i+1
= x
i
+ 1

d
i+1
= d
i
+ 2(dx dy)
y
i+1
= y
i
+ 1
Note: For |m| 1 the constants 2dy and 2(dy-dx) can be calculated once,
so the arithmetic will involve only integer addition and subtraction.
31
Example Draw a line from (20,10) to (30,18)
19
18
17
16
15
14
13
12
11
10
20 21 22 23 24 25 26 27 28 29 30 31 32
(20,10)
(30,18)
dx = 10
dy = 8

initial decision d
0
= 2dy dx = 6
Also 2dy = 16, 2(dy dx) = -4
i d
i
(x
i+1
,y
i+1
)
0 6 (21,11)
1 2 (22,12)
2 -2 (23,12)
3 14 (24,13)
4 10 (25,14)
5 6 (26,15)
6 2 (27,16)
7 -2 (28,16)
8 14 (29,17)
9 10 (30,18)
32
void LineBres(int x0, int y0, int x1, int y1) // line for |m| < 1
{
int dx = abs(x1 x0), dy = abs(y1 y0);
int d = 2 * dy dx, twoDy = 2 * dy, twoDyMinusDx = 2 * (dy dx);
int x, y;

if (x0 > x1) { // determines which point to use as start, which as end
x = x1;
y = y1;
x1 = x0;
}
else {
x = x0;
y = y0;
}
setPixel(x,y);

while (x < x1) {
x++;
if (d < 0) d += twoDy;
else {
y++;
d += twoDyMinusDx;
}
setPixel(x, y);
}
}
33
Special cases
Special cases can be handled separately
Horizontal lines (Ay = 0)
Vertical lines (Ax = 0)
Diagonal lines (|Ax| = |Ay|)
directly into the frame-buffer without
processing them through the line-plotting
algorithms.


Raster Scan Displays
Raster Scan Displays
Raster: A rectangular array of
points or dots
Pixel: One dot or picture element
of the raster
Scan Line: A row of pixels
Raster Scan Displays
In a raster scan system, the electron
beam is swept across the screen, one
row at a time from top to bottom.
Raster Scan Displays
As the electron beam moves
across each row, the beam
intensity is turned on and off
to create a pattern of
illuminated spots.
Raster Scan Displays
Picture definition is stored in a memory area
called the refresh buffer or frame buffer.
Raster Scan Displays
Refresh buffer or frame
buffer: This memory area
holds the set of intensity
values for all the screen
points.
Raster Scan Displays
Stored intensity values then retrieved from
refresh buffer and painted on the screen
one row (scan line) at a time.
Raster Scan Displays
Intensity range for pixel positions
depends on the capability of the
raster system.
A black-and-white system: each
screen point is either on or off, so
only one bit per pixel is needed to
control the intensity of screen
positions.
Raster Scan Displays
On a black-and-white system
with one bit per pixel, the frame
buffer is called bitmap.

For system with multiple bits per
pixel, the frame buffer is called
pixmap.
Raster Scan Displays
Sometimes, refresh rates
are described in unit of
cycles per second, or Hertz
(HZ)
Raster Scan Displays
Refreshing on raster scan
displays is carried out at
the rate 60 to 80 frame per
second.
Raster Scan Displays
Horizontal retrace: The return
to the left of the screen, after
refreshing each scan line.
Raster Scan Displays
Vertical retrace: At the end of each frame
(displayed in 1/80
th
to 1/60
th
of a second)
the electron beam returns to the top left
corner of the screen to begin the next
frame.
A final stage in the implementation
procedures for line segments and other
objects is to set the frame-buffer color
values.
scan-conversion algorithms generate pixel
positions at successive unit intervals,
incremental operations can also be used to
access the frame buffer efficiently at each
step of the scan-conversion process.


suppose the frame buffer array is addressed in row major order
and that pixel positions are labeled from (0, 0) at the lower-left
screen corner to (x max , y max ) at the top-right corner.
For a bilevel system (one bit per pixel), the frame-buffer bit
address for pixel position ( x , y ) is calculated as



Moving across a scan line, we can calculate the frame-buffer address for the
pixel at ( x + 1, y ) as the following offset from the address for position ( x , y ):
Stepping diagonally up to the next scan line from ( x , y ),we get to the frame-buffer
address of ( x + 1, y + 1) with the calculation
where the constant x max + 2 is precomputed once for all line segments.
Each of the address calculations involves only a single integer addition.


Methods for implementing these procedures depend on the capabilities of a
particular system and the design requirements of the software package.
With systems that can display a range of intensity values for each pixel, frame-
buffer address calculations include pixel width (number of bits), as well as the
pixel screen location.

You might also like