Lecture 5 Output Primitives
Lecture 5 Output Primitives
LINE DRAWING
ALGORITHMS
CCS 2208 COMPUTER GRAPHICS
Output Primitives
• The basic geometric structures which facilitate or
describe a scene/picture
• 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.
Output Primitives
• A point is shown by illuminating a pixel on the screen
• Points
• Attributes: Size, Colour.
glPointSize(p);
glBegin(GL_POINTS);
glVertex2d(x1,
y1);
glVertex2d(x2,
y2);
glVertex2d(x3,
y3);
Output Primitives
• Lines
• Attributes: Colour, 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: Colour, Thickness, Type
glLineWidth(p);
glBegin(GL_LINE_STRIP);
glVertex2d(x1,
y1);
glVertex2d(x2,
y2);
glVertex2d(x3,
y3);
glVertex2d(x4,
y4);
Output Primitives
• Polylines (closed)
• A polyline with the last point connected to the first point
• Attributes: Colour, 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 colour, Thickness, Fill pattern
glBegin(GL_POLYGON);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
Output Primitives
Output Primitive Attributes
Point Size, Colour
y
x
y mx m
|m|<1 |m|>1
Digital Differential Analyzer (DDA): Line Drawing Algorithm
(x1,y1)
dy
(x0,y0)
dx
DDA Line Drawing Algorithm
(Case a: m < 1)
x = x0 y = y0
y k 1 y k m
Illuminate pixel (x, round(y))
(x1,y1)
x = x0 + 1 y = y0 + 1 * m
x=x+1 y=y+1*m
y=y+1 x = x + 1 /m
Until y == y1
(x0,y0)
DDA Line Drawing Algorithm
(x1,y1) (x2,y2) are the end points and dx, dy are the float
variables.
Where dx= abs(x2 - x1) and dy= abs(y2 - y1)
Scan convert a line having end points (3,2) & (4,7) using
DDA.
x1 y1 x2 y2 L dx dy i x y Result Plot
3 2 4 7 5 .2 1 0 3.5 2.5 3.5, 2.5 3,2
1 3.7 3.5 3.7,3.5 3,3
2 3.9 4.5 3.9,4.5 3,4
3 4.1 5.5 4.1,5.5 4,5
4 4.3 6.5 4.3,6.5 4,6
5 4.5 7.5 4.5,7.5 4,7
Limitations of DDA Algorithm
(1) The rounding operation & floating point arithmetic are
time consuming procedures.
0 6 (21,11) 5 6 (26,15)
1 2 (22,12) 6 2 (27,16)
2 -2 (23,12) 7 -2 (28,16)
3 14 (24,13) 8 14 (29,17)
4 10 (25,14) 9 10 (30,18)
Example
Bresenham Line Algorithm Summary
The Bresenham line algorithm has the following
advantages:
• A fast incremental algorithm
• Uses only integer calculations