1
Lesson 2: Graphics Output
Primitives
Shima Mohammad qafor
UHD
College Of Science & Technology
Computer Science Department
Computer Graphics - Theory
Output Primitives
2
Primitives are basic objects (or structures) which
describe a graphics display (or scene).
Examples: point, line, text, filled region, circle, surfaces,
spline curves, etc.
Primitives which describes the geometry of objects are
typically referred to as geometric primitives.
Each of the output primitives has its own set of attributes.
Output Primitives
3
Rectangle Rounded rectangles
Point Line
Ellipse
Circle Triangle
Polyline Polygon
Arc
Modeling & Coordinate Systems
4
How do we represent objects using the primitives?
First, decide a convenient Cartesian coordinate system.
2D
3D
Modeling & Coordinate Systems (Cont.)
5
A 2D object can be represented by a set of points.
Edges that connect these points
represent the object.
In 3D modeling, instead of the edges
of an object, we represent its 3d
surface.
a set of small triangles is used to model
surfaces.
Output Primitives – Points
6
A point is shown by illuminating a pixel on the screen
We represent points as pairs
of X and Y values.
The red point is represented as
(1, 2)
What is the representation of
the blue point?
Output Primitives – Lines
7
Line drawing is fundamental to computer graphics.
A line segment is defined in terms of its two endpoints.
A line segment: given points (x1, y1) and (x2, y2)
(x2, y2)
(x1, y1)
Line Drawing (1)
8
A straight-line segment in a scene is defined by the
coordinate positions for the endpoints of the segment.
Example: line end points (1, 2) and (8, 6).
The graphics system must first
project the endpoints to
(8, 6)
integer screen coordinates.
Determine the nearest pixel
positions along the line path
(1, 2)
between the two endpoints.
Line Drawing (2)
y
A line is produced by illuminating
a set of intermediary pixels
between the two endpoints.
y2
Lines is digitized into a set of
discrete integer positions that
approximate the actual line path.
y1
Rasterization: process of
determining which pixels provide
the best approximation to a x1 x2 x
desired line on the screen
Scan Conversion: Combination of
rasterization and generating the
picture in scan line order
Line Drawing (3)
10
Straight lines must appear as straight lines, so:
They must start and end accurately.
Lines should have constant brightness along their length.
Lines drawing should be fast and efficient.
Output Primitives – Polylines & Polygons
11
Polylines
A set of line segments joined with open end.
Polygons
A set of line segments joined end to end.
Polygons are the areas enclosed by single closed loops of line
segments.
Line Drawing Algorithms
12
Programmer specifies (x, y) values of end pixels
Need algorithm to figure out which intermediate pixels
are on the line path.
There are several algorithms that can be used to draw
Lines:
Linear Line Equation Algorithm (Slope-Intercept Line Equation).
Digital Differential Analyzer Algorithm (DDA).
Bresenham’s Line Algorithm.
.
.
. etc.
Line Equation Algorithm
13
y xm b Cartesian slope-intercept equation
where m is the slope and b is the y intercept
yend y0
m
where (x0, y0) and (xend, yend) are the two endpoints of a
xend x0 line segment
b y0 m x0
Example1 (Line Equation)
14
Example1 (Line Equation) (Cont.)
15
Example2 (Line Equation)
16
Example3 (Line Equation)
17
By using line equation :
Example3 (Line Equation) (Cont.)
18
Exercise
19
Line start and end points: (4, 6) (14, 17)
Which intermediate pixels to turn on?
Find the pixel when x is 9
Digital Differential Analyzer (DDA) Algorithm
20
DDA is based on calculating either ∆y or ∆x.
A line is sampled at unit intervals in one coordinate and the corresponding
integer values nearest the line path are determined for the other
( xend , yend )
coordinate.
∆x = xend – x0 and ∆y = yend – y0 ( xk , y k ) ( xk 1, yk k )
( x0 , y0 )
If |m| <= 1 If |m| > 1
….. Eq. 1 1 ….. Eq. 3
xk 1 xk 1 x k 1 x k
….. Eq. 2 m
yk 1 yk m
k 1 yk 1
y ….. Eq. 4
where k = 0, 1, 2, …, ∆x where k = 0, 1, 2, …, ∆y
pixel [x, round(y)] pixel [round(x), y]
Example (DDA)
21
Line points(-2, 3) (8, 10)
x0 = -2, y0 = 3
xend = 8, yend = 10
∆x = 10 and ∆y = 7
m = 0.7
Use equations 1 and 2 to
compute x and y
Example (DDA) (Cont.)
22
Exercise (DDA):
23
Line points(2, -3) (8, 4)
x0 = ?, y0 = ?
xend = ?, yend = ?
∆x = ? and ∆y = ?
m=?
Use equations 3 and 4 to
compute x and y
DDA Pros and Cons
24
Pros:
Faster than line equation
Cons:
pixel may go away from the line path due to round-off error
time consuming due to rounding and floating-point operations
Bresenham’s Line Algorithm
25
It is an efficient line-generating algorithm.
It uses only incremental integer calculations.
The lines drawn are of superior quality as compared to
DDA method.
Adaptable to circles, other curves.
Faster than DDA.
Bresenham’s Line Algorithm (Cont.)
26
1. Input two end points, store left line endpoint in (x0, y0 )
2. Plot pixel (x0, y0)
3. Calculate ∆x, ∆y
4. Obtain the starting value for the decision parameter p0 =
2∆ y − ∆ x
5. At each xk , perform test:
If pk < 0 then plot next point is (xk+1, yk); pk+1 = pk + 2∆y
Otherwise plot next pixel (xk+1, yk+1); pk+1= pk + 2∆y - 2∆x
6. Repeat step 5 (∆x − 1) times.
Example (Bresenham’s Algorithm)
27
Endpoints (20, 10) and (30, 18).
(x0 = 20 , y0 = 10)
m = 0.8
∆x = 10, ∆y =8
p0 = 2∆y − ∆x = 6
P0 >= 0 next point is (xk+1, yk+1) = (21, 11)
Calculate pk+1 = pk + 2∆y - 2∆x
Example (Bresenham’s Algorithm) (Cont.)
28
K pk (xk +1, yk +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)
Exercise: Apply Bresenham Algorithm with end points (20, 41) and (30, 44)
Parallel Line Algorithms
29
The line-generating algorithms we have discussed so far
determine pixel positions sequentially.
Using parallel processing, we can calculate multiple
pixel positions along a line path simultaneously
Partitioning the computations among the processors
available.
Pixel positions can be calculated efficiently in parallel.
Given np processors we can set up a parallel Bresenham
line algorithm:
subdivide the line path into np partitions and
simultaneously generate line segments in each of the
subintervals.
Circle-Generating Algorithms
30
Circles and ellipses are common components in many pictures
and graphs.
Circle generation routines are often included in graphics
packages
Properties of Circles
A circle is defined as a set of points that
are all at a given distance r from a
center position (xc, yc).
For any circle point (x, y), this distance is
expressed by:
Circle-Generating Algorithms
31
We could solve for y in terms of x
This is not the best method:
Considerable amount of
computation
Spacing between plotted pixels
is not uniform
Circle Symmetry
32
The circle sections in adjacent octants within one quadrant are
symmetric with respect to the 45 degree line dividing the two
octants
Still requires a good
deal of computation
time.
Efficient Solutions
Midpoint Circle Algorithm
Midpoint Circle Algorithm
33
Circle Function: fcircle (x,y) = x + y – r
2 2 2
(xc, yc) yc
Assume that we have just plotted
midpoint
point (xc, yc).
Which is next?
(xc+1, yc) OR (xc+1, yc-1). yc-1
Choose the point that is nearest to
the actual circle.
How to make this choice?
Decision Parameter: is the circle at the midpoint
between the pixels yc and yc-1
Midpoint Circle Algorithm Steps (1)
34
1. Input radius r and circle centre (xc, yc), then set the first point
on the circumference of the circle centered on the origin as:
(x0, y0) = (0, r)
2. Calculate the initial value of the decision parameter as:
3. At each xk position, starting with k = 0, perform the
following test:
If pk < 0, the next point is (xk+1, yk) and pk+1 = pk + 2xk+1 +1
Otherwise, the next point is (xk+1,yk – 1) and
pk+1 = pk + 2xk+1 +1 – 2yk+1
Where 2xk+1 = 2xk + 2 and 2yk+1 = 2yk – 2
Midpoint Circle Algorithm Steps (2)
35
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the
circular path centered at (xc, yc ) and plot the coordinate
values:
x = x + xc, y = y + yc
6. Repeat step 3 to 5 until x >= y.
Example
36
Given radius r = 10. Initial point is (0, 10)
Determine positions from x = 0 to x = y
p0 = 1 − r = − 9
2x0 = 0, 2y0 = 20
k pk Xk+1 yk+1 (Xk+1, yk+1)
0 -9 1 10 (1, 10)
1 -9 + 2 + 1 = -6 2 10 (2, 10)
2 -6 + 4 + 1 = -1 3 10 (3, 10)
3 -1 + 6 + 1 = 6 4 9 (4, 9)
4 6 + 8 + 1 – 18 = -3 5 9 (5, 9)
5 -3 + 10 + 1 = 8 6 8 (6, 8)
6 8 + 12 + 1 – 16 = 5 7 7 (7, 7)
Exercises
37
1. Find circle pixels if you
know r = 8 k pk Xk+1 yk+1 (Xk+1, yk+1)
2. In the previous examples
the circles centered at (0,
0). What about a circle
centered at (15,10) with
r = 14
Ellipse
38
A precise definition of an ellipse can be given in terms of the
distances from any point on the ellipse to two fixed positions,
called the foci of the ellipse.
The sum of these two distances d1 and d2 is the same value
for all points on the ellipse.
d1 + d2 = constant
References
39
Edward Angel and Dave Shreiner, “Interactive
computer graphics: A Top-Down Approach With
Shader-Based OpenGL”, 6th Ed., 2012, Ch.2, P. 56.
Hearn Baker Carithers, "Computer Graphics with
OpenGL", 4th Ed., Ch.3 P. 45, Ch.3 131 – 147.
40