Documente Academic
Documente Profesional
Documente Cultură
GRAPHICS
CHAPTER 3
2D GRAPHICS ALGORITHMS
1
2D Graphics Algorithms
Output Primitives
Line Drawing Algorithms
DDA Algorithm
Midpoint Algorithm
Bersenhem’s Algorithm
3
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
Each of the output primitives has its
own set of attributes.
4
Output Primitives
Points
Attributes: Size, Color.
glPointSize(p);
glBegin(GL_POINTS);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glEnd()
Output Primitives
Lines
Attributes: Color, Thickness, Type
glLineWidth(p);
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
Output Primitives
Polylines (open)
A set of line segments joined end to end.
Attributes: Color, Thickness, Type
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
glBegin(GL_LINE_LOOP);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
Output Primitives
Polygons
A set of line segments joined end to end.
Attributes: Fill color, Thickness, Fill pattern
10
Output Primitives
Images
Attributes: Image Size, Image
Type, Color Depth.
Image Type:
Binary (only two levels)
Monochrome
Color.
Color Depth:
Number of bits used to represent
color.
11
TCS2111
Output Primitives
Output Primitive Attributes
Point Size
Color
Line Thickness (1pt, 2pt …)
Type (Dashed, Dotted, Solid)
Color
Text Font (Arial, Courier, Times Roman…)
Size (12pt, 16pt ..)
Spacing
Orientation (Slant angle)
Style (Bold, Underlined, Double lined)
Color
Filled Region Fill Pattern
Fill Type (Solid Fill, Gradient Fill)
Fill Color
Images Color Depth (Number of bits/pixel) 12
Line Drawing Algorithms
13
Line Drawing
• Line drawing is fundamental to computer graphics.
• We must have fast and efficient line drawing functions.
y=mx+c
A(ax,ay)
ax bx x
Line Drawing - Analytical Method
double m = (double)(by-ay)/(bx-ax);
double c = ay - m*ax;
double y;
int iy;
for (int x=ax ; x <=bx ; x++) {
y = m*x + c;
iy = round(y);
setPixel (x, iy);
}
Current (6,3)
Pixel To find (xk+1, yk+!):
(xk, yk) (5,2) (6,2)
xk+1 = xk+1
(6,1) yk+1 = ?
Assumption:
Slope < 1
Current
Pixel
xk+1 = xk+1
yk+1 = Either yk or yk+1
Midpoint Algorithm - Notations
Line
( xk+1, yk+1)
Candidate Pixels
Midpoint
Current Pixel
( xk, yk)
( xk+1, yk)
•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).
Equation of a line revisited.
y ay x ax
Equation of the line:
by a y bx ax
F (x,y) > 0
(for any point below line)
Midpoint Algorithm
Decision Criteria
f ( x, y ) 0 F(MP) < 0
F(MP) > 0
Update Equation
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
Bresenham’s 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
Circle Drawing
Algorithms
32
Midpoint Circle Drawing Algorithm
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
{
> 0, (x,y) outside the circle
35
TCS2111
yk-1 yk-1
Fk < 0 Fk >= 0
yk+1 = yk yk+1 = yk - 1
Next pixel = (xk+1, yk) Next pixel = (xk+1, yk-1)
36
TCS2111
38
TCS2111
Solution:
f0 = 5 – r
4
= 5 – 10
4
= -8.75
≈ –9
39
TCS2111
40
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)
46
Fill Area Algorithms
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
Basic Filling Algorithm
The basic filling algorithm is commonly used
in interactive graphics packages, where the
user specifies an interior point of the region to
be filled.
4-connected pixels
48
Basic Filling Algorithm
[1] Set the user specified point.
[2] Store the four neighboring pixels in a
stack.
[3] Remove a pixel from the stack.
[4] If the pixel is not set,
Set the pixel
Push its four neighboring pixels into
the stack
[5] Go to step 3
[6] Repeat till the stack is empty.
49
Basic Filling Algorithm (Code)
50
Basic Filling Algorithm
51
Types of Basic Filling Algorithms
Boundary Fill Algorithm
For filling a region with a single
boundary color.
Condition for setting pixels:
Color is not the same as border color
Color is not the same as fill color