Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CHAPTER 3
2D GRAPHICS ALGORITHMS
2D Graphics Algorithms
Output Primitives
Output Primitives
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
Output Primitives
Points
Output Primitives
Lines
Output Primitives
Polylines (open)
glLineWidth(p); glBegin(GL_LINE_STRIP); glVertex2d(x1, y1); glVertex2d(x2, y2); glVertex2d(x3, y3); glVertex2d(x4, y4); glEnd()
Output Primitives
Polylines (closed)
A polyline with the last point connected to the first point . Attributes: Color, Thickness, Type
Note: A closed polyline cannot be filled.
glBegin(GL_LINE_LOOP); glVertex2d(x1, y1); glVertex2d(x2, y2); glVertex2d(x3, y3); glVertex2d(x4, y4); glEnd()
Output Primitives
Polygons
Output Primitives
Text
10
Output Primitives
Images
Monochrome
Color.
Color Depth:
Number of bits used to represent color.
11
TCS2111
Output Primitives
Output Primitive Point Line Attributes Size Color Thickness (1pt, 2pt ) Type (Dashed, Dotted, Solid) Color Font (Arial, Courier, Times Roman) Size (12pt, 16pt ..) Spacing Orientation (Slant angle) Style (Bold, Underlined, Double lined) Color Fill Pattern Fill Type (Solid Fill, Gradient Fill) Fill Color Color Depth (Number of bits/pixel)
12
Text
Filled Region
Images
13
Line Drawing
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.
by a y bx ax
B(bx,by)
c a y max
y=mx+c
A(ax,ay) ax
bx
Incremental Algorithms
I have got a pixel on the line (Current Pixel). How do I get the next pixel on the line? Compute one point based on the previous point:
(xk+1, yk+1) .
Incrementing along x
Current Pixel (xk, yk) (6,3) (5,2) (6,2) (6,1)
xk+1 = xk+1
yk+1 = ?
Assumes that the next pixel to be set is on the next column of pixels (Incrementing the value of x !) Not valid if slope of the line is large.
Assumption: Slope is less than 1 (Increment along x). Current Pixel = (xk, yk). (xk, yk) lies on the given line. yk = m.xk + c Next pixel is on next column. xk+1 = xk+1 Next point (xk+1, yk+1) on the line yk+1 = m.xk+1 + c = m (xk+1) +c = yk + m
Given a point (xk, yk) on a line, the next point is given by xk+1 = xk+1 yk+1 = yk + m
Midpoint Algorithm
Midpoint algorithm is an incremental algorithm
Candidate Pixels
Midpoint
Current Pixel ( x k , yk )
( xk+1, yk)
Midpoint Algorithm:
Choice of the next pixel
If the midpoint is below the line, then the next pixel is (xk+1, yk+1). If the midpoint is above the line, then the next pixel is (xk+1, yk).
x ax by a y bx ax
B(bx,by)
y ay
A(ax,ay)
Midpoint Algorithm:
Regions below and above the line.
F(x,y) = 0
Midpoint Algorithm
Decision Criteria
f ( x, y) 0
F(MP) > 0
F(MP) < 0
Midpoint Algorithm
Decision Criteria
Decision Parameter
(Notation)
If Fk < 0 : The midpoint is above the line. So the next pixel is (xk+1, yk). If Fk 0 : The midpoint is below or on the line. So the next pixel is (xk+1, yk+1).
Fk > 0
Fk < 0 yk+1 = yk
Next pixel = (xk+1, yk)
yk+1 = yk+1
Next pixel = (xk+1, yk+1)
Midpoint Algorithm
Update Equation
Fk = F(xk+1, yk+ ) = h (xk+1 ax) w (yk+ ay)
Update Equation
But, Fk+1 = Fk + h w (yk+1 yk). (Refer notes) So, Fk< 0 : Fk 0 : yk+1 = yk. yk+1 = yk+1. Hence, Fk+1 = Fk + h . Hence, Fk+1 = Fk + h w.
F0 = h w/2.
Midpoint Algorithm
int h = by-ay; int w = bx-ax; float F=h-w/2; int x=ax, y=ay; for (x=ax; x<=bx; x++){ setPixel(x, y); if(F < 0) F+ = h; else{ F+ = h-w; y++; } }
30
Bresenhams Algorithm
int h = by-ay; int w = bx-ax; int F=2*h-w; int x=ax, y=ay; for (x=ax; x<=bx; x++){ setPixel(x, y); if(F < 0) F+ = 2*h; else{ F+ = 2*(h-w); y++; } }
31
32
To determine the closest pixel position to the specified circle path at each step. For given radius r and screen center position (xc, yc), calculate pixel positions around a circle path centered at the coodinate origin (0,0). Then, move each calculated position (x, y) to its proper screen position by adding xc to x and yc to y.
Along the circle section from x=0 to x=y in the first quadrant, the gradient varies from 0 to -1.
33
TCS2111
34
TCS2111
fcircle (x,y) =
> 0, (x,y) outside the circle < 0, (x,y) inside the circle = 0, (x,y) is on the circle boundary
35
TCS2111
yk
midpoint
yk-1
yk-1
Fk < 0 yk+1 = yk
Next pixel = (xk+1, yk)
Fk >= 0 yk+1 = yk - 1
Next pixel = (xk+1, yk-1)
36
TCS2111
-------- (1)
-------- (2)
If Fk < 0,
If Fk >= 0,
TCS2111
38
TCS2111
f0 = 5 r 4
= 5 10 4 = -8.75 9
39
TCS2111
0
1 2 3
-9
-9+2+1=-6 -6+4+1=-1 -1+6+1=6
1
2 3 4
10
10 10 9
2
4 6 8
20
20 20 18
4
5 6
6+8+1-18=-3
-3+10+1=8 8+12+1-16=5
5
6 7
9
8 7
10
12 14
18
16 14
40
TCS2111
41
TCS2111
42
Antialiasing
43
Antialiasing
Antialiasing is a technique used to diminish the jagged edges of an image or a line, so that the line appears to be smoother; by changing the pixels around the edges to intermediate colors or gray scales. Eg. Antialiasing disabled:
44
Antialiasing (OpenGL)
Antialiasing disabled
Antialiasing enabled
46
Fill-Area algorithms are used to fill the interior of a polygonal shape. Many algorithms perform fill operations by first identifying the interior points, given the polygon boundary.
47
4-connected pixels
48
50
Color is not the same as border color Color is not the same as fill color
TCS2111
TCS2111
54
Enabling polygon fill (Default): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); Disabling polygon fill: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);