0% found this document useful (0 votes)
33 views

Lecture 5 Output Primitives

perfect notes
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Lecture 5 Output Primitives

perfect notes
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

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

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
• A set of line segments joined end to end.
• Attributes: Fill colour, Thickness, Fill pattern

Note: Polygons can be filled.

glBegin(GL_POLYGON);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
Output Primitives
Output Primitive Attributes
Point Size, Colour

Line Thickness (1pt, 2pt …), Type (Dashed, Dotted,


Solid), Colour

Text Font (Arial, Courier, Times Roman…)


Size (12pt, 16pt ..)
Spacing
Orientation (Slant angle)
Style (Bold, Underlined, Double lined)
Colour
Filled Region Fill Pattern
Fill Type (Solid Fill, Gradient Fill)
Fill Colour
Images Image Size, Image Type, Colour Depth (Number of
bits/pixel)
Line Drawing Algorithms
Lines
• A line segment is completely defined in terms of its two
endpoints.
• A line segment is thus defined as:
Line_Seg = { (x1, y1), (x2, y2) }
Lines
y
• A line is
produced by
means of
illuminating a
y2
set of
intermediary
pixels between y1
the two
endpoints.
x
x1 x2
Basic Line Algorithms
1. Must compute integer coordinates of pixels which lie on or near a
line or circle.
2. Pixel level algorithms are invoked hundreds or thousands of times
when an image is created or modified.
3. Lines must create visually satisfactory images.
• Lines should appear straight
• Lines should terminate accurately
• Lines should have constant density
• Line density should be independent of line length and angle.
4. Line algorithm should always be defined.
Line
• The rounding of coordinate values to integer causes all
but horizontal and vertical lines to be displayed with a stair
step appearance “the jaggies”.
• 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 colours or gray scales.

Setting antialiasing option for lines:


glEnable (GL_LINE_SMOOTH);
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.
Line Drawing Algorithm
• Slope-intercept line equation
• y = mx + b
• Given two end points (x0,y0), (x1, y1), how to compute m
and b? dy y1  y 0
(x1,y1) m 
dy
dx x1  x0
(x0,y0)
dx
b  y 0  m * x0
y mx
• Numerical example of finding slope m:
• (Ax, Ay) = (23, 41), (Bx, By) = (125, 96) y
x 
m
By  Ay 96  41 55
m   0.5392
Bx  Ax 125  23 102
Line Drawing Algorithm
• The slope- intercept equation for a straight line forms
the basic for determining deflection voltage in analog
devices.

y
x 
y mx m

|m|<1 |m|>1
Digital Differential Analyzer (DDA): Line Drawing Algorithm

DDA algorithm is an incremental algorithm


Walk through the line, starting at (x0,y0)
Constrain x, y increments to values in [0,1] range
Case a: x is incrementing faster (m < 1)
 Step in x=1 increments, compute and round y
Case b: y is incrementing faster (m > 1)
 Step in y=1 increments, compute and round x

(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

Illuminate pixel (x, round(y))

x=x+1 y=y+1*m

Illuminate pixel (x, round(y))

(x0, y0) Until x == x1


DDA Line Drawing Algorithm
(Case b: m > 1)
x = x0 y = y0
1
x k 1  xk  (x1,y1) Illuminate pixel (round(x), y)
m
y = y0 + 1 x = x0 + 1 * 1/m

Illuminate pixel (round(x), y)

y=y+1 x = x + 1 /m

Illuminate pixel (round(x), y)

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)

(i) If dx >=dy then


length = dx
else
length = dy
endif
(ii) dx = (x2-x1)/length
dy = (y2-y1)/length
DDA Line Drawing Algorithm
(iii) x = x1 + 0.5
y = y1 + 0.5
(iv) i=0
(v) Plot ((x), (y))
(vi) x = x + dx
y = y + dy
(vii) i=i+1
(viii) If i < length then go to step (v)
(ix) Stop
DDA Line Drawing Algorithm : Example

Scan convert a line having end points (3,2) & (4,7) using
DDA.

Solution: dx= x2 - x1 = 4-3 = 1


dy= y2 - y1 = 7-2 = 5

As, dx < dy then


length = y2-y1 = 5
dx = (x2-x1)/ length = 1/5 =0.2
dy = (y2-y1)/ length = 5/5 = 1
DDA Line Drawing Algorithm : Example

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.

(2) Round-off error can cause the calculated pixel position


to drift away from the true line path for a long line
segment.
Bresenham’s algorithm
• Step 1: Enter the 2 end points for a line and store the left
end point in (X0,Y0).
• Step 2: Plot the first point by loading (X0,Y0) in the frame buffer.
• Step 3: determine the initial value of the decision parameter by
calculating the constants dx, dy, 2dy and 2dy-2dx
P0 = 2dy –dx
• Step 4: for each Xk, conduct the following test, starting from k= 0

• If Pk<0, then the next point to be plotted is at (Xk+1, Yk) and


• Pk+1 = Pk + 2dy
• Else, the next point is (Xk+1, Yk+1) and
• Pk+1 = Pk + 2dy –2dx
• Step 5: iterate through step (4) dx times.
Bresenham’s algorithm: Example
• Let the given end points for the line be (20,10) and (30,18)
• M = dy = y2 – y1 = 18 – 10 = 8
• dx x2 – x1 30 – 20 = 10
• m = 0.8
• dy = 8 and dx = 10
• The initial decision parameter P0 is
• P0 = 2dy – dx = 2(8) – 10 = 16 – 10 = 6
• P0 = 6
• The constants 2dy and 2dy-2dx are
• 2dy = 2(8) = 16 2dy-2dx = 2(8)- 2(10) =16 – 20=-
4
• 2dy = 16 2dy – 2dx = - 4
Bresenham’s algorithm: Example
• End points (20,10) (30,18)
• dx= x -x = 30-20 = 10 dy= y -y = 18-10=8
2 1 2 1
• P = 2dy-dx = 2x8-10 = 6 >0 pt(21,11)
0
• P = p +2dy-2dx = 6+16-20 =2 >0 (22,12)
k+1 k
• P =2+16-20 =-2 <0 (23,12)
k+1
• P = p +2dy = -2+16 = 14 >0 (24,13)
k+1 k
• P = p +2dy-2dx = 14+16-20 = 10>0 (25,14)
k+1 k
• P = 10+16-20 = 6 >0 (26,15)
k+1
• P = 6+16-20 = 2>0 (27,16)
k+1
• P = 2+16-20 = -2 <0 (28,16)
k+1
• P = p +2dy = -2+16 = 14>0 (29,17)
k+1 k
• P =p +2dy-2dx = 14+16-20= 10>0 (30,18)
k+1 k
Example
• We plot the initial point (x0 , y0)=(20,10) and determine
successive pixel positions along the line path from the
decision parameter as
K pk (xk +1, yk +1) K pk (xk +1, yk +1)

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

Comparing this to the DDA algorithm, DDA has the


following problems:
• Accumulation of round-off errors can make the pixelated
line drift away from what was intended
• The rounding operations and floating point arithmetic
involved are time consuming

You might also like