Professional Documents
Culture Documents
Lines Algorithm
Lines Algorithm
COMPUTER GRAPHICS
Algorithms for Graphics Primitives
Material based on
- Computer Graphics with OpenGL, Fourth Edition by Donald Hearn, M. Pauline Baker, and Warren R. Carithers
- Fundamentals of Computer Graphics, Third Edition by by Peter Shirley and Steve Marschner
- Computer Graphics by F. S. Hill
Jaggies in line-drawing
Figure 6-1 Stair-step effect (jaggies) produced when a line is generated as a
series of pixel positions.
Line equations
Figure 6-2 Line path between endpoint positions (x0, y0) and (x end, y end).
(1)
(2)
(3)
step
through in x
step through in y
Desired line
(xi+1,round(yi+m))
(xi,yi)
(xi,round(yi))
(xi+1,yi+m)
if slope m 0
if |m| 1
xi+1 = xi + 1
if |m| 1
yi+1 = yi + 1
xi+1 = xi + 1/m
yi+1 = yi + m
Right
Right
Left
Left
if |m| 1
xi+1 = xi - 1
if |m| 1
yi+1 = yi - 1
xi+1 = xi - 1/m
yi+1 = yi - m
Right
Right
Left
Left
10
if slope m < 0
if |m| 1
xi+1 = xi + 1
if |m| 1
yi+1 = yi - 1
xi+1 = xi - 1/m
yi+1 = yi + m
Left
Left
Right
Right
11
if slope m 0
if |m| 1
xi+1 = xi - 1
if |m| 1
yi+1 = yi + 1
xi+1 = xi + 1/m
yi+1 = yi - m
Left
Left
Right
Right
12
yk+1=yk-m
If we process from right to left and m>1, then we have y=-1 and
xk+1=xk-1/m
For negative slopes, look at the the magnitude of m
13
DDA
14
Problems of DDA
Accumulation of round-off errors
Calculated pixel positions may drift away from the true line path for
long segments
Rounding operations and floating point arithmetic are
time-consuming
15
16
17
18
Bresenham
19
Bresenham
We would like to use only integer calculations
Define a decision parameter pk. Letting m=y/x:
x>0
Parameter c is constant and is 2y+x(2b-1) which is
independent of the pixel position and will be eliminated in
the recursive calculation
20
Bresenham
If pixel at yk is closer to the line path than pixel at yk+1 (i.e.
using
where yk+1-yk is either 0 or 1 depending on the sign of pk
First parameter is calculated as
21
Bresenham algorithm
22
Example
23
Example
24
Circle-generating algorithms
Figure 6-11 Circle with center
coordinates (xc , yc) and radius r .
25
Circle-generating algorithms
One could use
Sqrt is time-consuming
26
27
28
29
30
31
Recursive expression
32
33
Example
34
Example
35
Example
Figure 6-15 Pixel positions (solid circles) along a circle path centered on the origin and with radius r = 10, as
calculated by the midpoint circle algorithm. Open (hollow) circles show the symmetry positions in the first
quadrant.
Midpoint function
void plotpoints(int x,
{
setpixel(xcenter+x,
setpixel(xcenter-x,
setpixel(xcenter+x,
setpixel(xcenter-x,
setpixel(xcenter+y,
setpixel(xcenter-y,
setpixel(xcenter+y,
setpixel(xcenter-y,
}
int y)
ycenter+y);
ycenter+y);
ycenter-y);
ycenter-y);
ycenter+x);
ycenter+x);
ycenter-x);
ycenter-x);
void circle(int r)
{
int x = 0, y = r;
plotpoints(x,y);
int p = 1 r;
while (x<y) {
x++;
if (p<0) p += 2*x + 1;
else {
y--;
p += 2*(x-y) + 1;
}
plotpoints(x,y);
}
}
36
Ellipse-Generating Algorithms
37
F1
d1
P=(x,y)
F2
d2
The sum of the two distances d1 and d2, between the fixed
positions F1 and F2 (called the foci of the ellipse) to any
point P on the ellipse, is the same value, i.e.
d1 + d2 = constant
38
Ellipse Properties
ry
rx
y yc
x xc
Cartesian coordinates:
1
rx
ry
Polar coordinates:
x xc rx cos
y yc ry sin
39
Ellipse Algorithms
Symmetry between quadrants
Not symmetric between the two octants of a quadrant
Thus, we must calculate pixel positions along the
(x, y)
ry
rx
(-x, -y)
(x, -y)
40
Ellipse-generating algorithms
Figure 6-16 Ellipse generated about foci F1 and F2.
41
42
43
44
45
46
Ellipse
47
48
Ellipse (region 2)
Figure 6-22 Midpoint between candidate pixels at sampling position yk 1 along an
elliptical path.
49
Ellipse
50
51
52
53
Example
54
Example
55
Example
56
Example
Figure 6-23 Pixel positions along an elliptical path centered on the origin with rx = 8 and ry = 6, using the
midpoint algorithm to calculate locations within the first quadrant.
57
58
59
Polygon filling
For scan line y, the two edges sharing an intersection
60
61
scan line k
At kth position, edges are in sorted order from left to right.
polygon to its top, producing an active edge list (AEL) for each
scan line crossing the polygon boundaries
The active edge list for a scan line contains all edges crossed by that
scan line
62
Edge tables
Figure 6-50 A polygon and its sorted edge table, with edge DC shortened by one unit in the y direction.
63
encountered
Flood-fill
Fill in (recolor) an area that is not defined within a single color
boundary
Paint by replacing a specified interior color instead of searching for
a particular boundary color
64
Boundary-fill
Boundary-ll algorithm starts from an interior point (x, y)
65
66
67
Boundary-fill
68
Flood-fill
Sometimes we want to ll in (or recolor) an area that is
69
Flood-fill
70
Antialiasing
Aliasing: Distortion of information due to low-frequency
sampling (undersampling)
Increase sampling rate in raster systems
Simply display at higher resolution: Requires large frame-buffers!
Apple MacBook Retina Display: Over 5 million on the 15 inch model: With
so high pixel density, your eyes cant discern individual pixels at normal
viewing distance
71
Antialiasing
Blurring to smooth an image
E.g. Sharp transitions from black to white is softened by using a
mixture of gray pixels near the borders
When looking from a distance, eye blends the gracefully varying
shades together
72
73
Supersampling
Figure 6-59 Supersampling subpixel positions along a Figure 6-60 Supersampling subpixel positions
straight-line segment whose left endpoint is at screen in relation to the interior of a line of finite width.
coordinates (10, 20).
74
area
Center subpixel is weighted by a factor of 1/4; the top, bottom, and side
subpixels are each weighted by a factor of 1/8; and the corner subpixels
are each weighted by a factor of 1/16.
75
76
77
78
79
Pixel phasing
On raster systems that can address subpixel positions