Lect04 Slides
Lect04 Slides
Lect04 Slides
COMPUTER GRAPHICS
LECTURE 4
2D GRAPHICS ALGORITHMS 1
Note for the students:
These slides are meant for the lecturers to
conduct lectures only. It is NOT suitable to
be used as a study material.
Students are expected to study by reading
the textbook for this course:
4
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.
5
Output Primitives
Points
Attributes: Size, Color.
v1 v2
glPointSize(p);
v3
glBegin(GL_POINTS);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glEnd()
6
Output Primitives
Lines
Attributes: Color, Thickness, Type
v1
v4 glLineWidth(p);
glBegin(GL_LINES);
v2 v3 glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
7
Output Primitives
Polylines (open)
A set of line segments joined end to end.
Attributes: Color, Thickness, Type
v1
v3
glLineWidth(p);
glBegin(GL_LINE_STRIP);
glVertex2d(x1, y1);
v2 v4
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
8
Output Primitives
Polylines (closed)
A polyline with the last point connected to the
first point .
Attributes: Color, Thickness, Type
v1
v3 Note: A closed polyline cannot be filled.
glBegin(GL_LINE_LOOP);
v2 v4 glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
9
Output Primitives
Polygons
A set of line segments joined end to end.
Attributes: Fill color, Thickness, Fill pattern
Note: Polygons can be filled
v4
v1 v3 glBegin(GL_POLYGON);
glVertex2d(x1, y1);
v2
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
10
Output Primitives
Text
Attributes: Font, Color, Size,
Spacing, Orientation.
Font:
Type (Helvetica, Times, Courier etc.)
Size (10 pt, 14 pt etc.)
Style (Bold, Italic, Underlined)
11
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.
12
TCS2111
Output Primitives (Summary)
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) 13
Line Drawing Algorithms
14
Line Drawing
• Line drawing is fundamental to computer graphics.
• We must have fast and efficient line drawing functions.
15
Line Drawing - Analytical Method
by a y
m
y bx ax
c a y max B(bx,by)
y=mx+c
A(ax,ay)
ax bx x
16
Line Drawing - Analytical Method
//Given (ax, ay) and (bx, by)
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);
}
• Directly based on the analytical equation of a line.
• Involves floating point multiplication and addition
• Requires round-off function.
17
Incremental Algorithms
I have got a pixel on the line (Current Pixel).
How do I get the next pixel on the line?
Current (6,3)
Pixel To find (xk+1, yk+!):
(xk, yk) (5,2) (6,2)
xk+1 = xk+1
(6,1)
yk+1 = ?
21
Midpoint Algorithm
Midpoint algorithm is an incremental algorithm
Assumption:
Slope < 1
Current
Pixel
xk+1 = xk+1
yk+1 = Either yk or yk+1 22
Midpoint Algorithm - Notations
Line
(xk+1, yk+1)
Candidate Pixels
Midpoint
Current Pixel
(xk, yk)
(xk+1, yk)
F (x,y) > 0
(for any point below line)
26
Midpoint Algorithm:
Decision Criteria
f ( x, y ) 0 F(MP) < 0
F(MP) > 0
27
Midpoint Algorithm
Decision Criteria
Decision Parameter
Update Equation
Summary:
Fk+1 = Fk + h w (yk+1 yk)
F0 = h w/2
31
Midpoint Algorithm
int h = by-ay;
int w = bx-ax;
float F = h-w/2;
int y = ay;
for (int x=ax; x<=bx; x++) {
setPixel(x, y);
if(F < 0)
F += h;
else {
F += h-w;
y++;
}
} 32
Bresenham’s Algorithm
(Improved Midpoint Algorithm)
int h = by-ay;
int w = bx-ax;
int F = 2*h-w;
int y = ay;
for (int x=ax; x<=bx; x++) {
setPixel(x, y);
if(F < 0)
F += 2*h;
else {
F += 2*(h-w);
y++;
}
} 33
Circle Drawing
Algorithms
(Extra notes for self-study!)
34
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.
35
TCS2111
36
TCS2111
{
> 0, (x,y) outside the circle
37
TCS2111
yk yk
midpoint midpoint
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)
38
TCS2111
40
TCS2111
Solution:
f0 = 5 – r
4
= 5 – 10
4
= -8.75
≈ –9
41
TCS2111
42
TCS2111
44
Anti-aliasing
45
Anti-aliasing
Anti-aliasing 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.
E.g. Anti-aliasing disabled:
46
Anti-aliasing (OpenGL)
48
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.
49
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
50
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.
51
Basic Filling Algorithm (Code)
52
Basic Filling Algorithm: Conditions
Requires an interior point.
Involves considerable amount of
stack operations.
The boundary has to be closed.
Not suitable for self-intersecting
polygons
53
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
57
Reading Assignment
Edward Angel, Interactive Computer
Graphics (5th Edition):
Chapter 7: 7.8 – 7.10
(From Vertices to Fragments)
58
Further Readings
Hearn and Baker, Computer Graphics with
OpenGL (3rd Edition):
Chapter 3: 3.1–3.5, 3.9, 3.14-3.17
(Graphics Output Primitives)
Chapter 4: 4.1-4.9, 4.13-4.14, 4.17-4.18
(Attributes of Graphics Primitives)
59