Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

COMPUTER GRAPHICS

Output Primitives and their Attributes

CODE: COB 4112


FACILITATOR: FRANK
EMAIL: frankibrahim25@gmail.com
Introduction
 Shapes and colors of the objects can be described
internally with pixel arrays or with sets of basic
geometric structures, such as straight line segments
and polygon color areas.
 The scene is then displayed either by loading the pixel
arrays into the frame buffer or by scan converting the
basic geometric-structure specifications into pixel
patterns.
 Typically, graphics programming packages provide
functions to describe a scene in terms of these basic
geometric structures, referred to as output primitives,
and to group sets of output primitives into more
complex structure.
 Output primitive is the basic geometric structures
used to describe scenes.
 Each output primitive is specified with input
coordinate data and other information about the
way that object is to be displayed.
 Points and straight line segments are the simplest
geometric components of pictures.
 Additional output primitives that can be used to
construct a picture include circles and other conic
sections, quadric surfaces, spline curves and
surfaces, polygon color areas, and character
strings.
POINTS AND LINES
 Point is the fundamental element of picture representation.
 Point plotting is accomplished by converting a single
coordinate position furnished by an application program
into appropriate operations for the output device in use.
 For example, with a CRT monitor, the electron beam is
turned on to illuminate the screen phosphor at the selected
location.
 A random-scan (vector) system stores point-plotting
instructions in the display list, and coordinate values in
these instructions are converted to deflection voltages that
position the electron beam at the screen locations to be
plotted during each refresh cycle.
Line Drawing Algorithms:
 The process of ‘turning on’ pixels for line segment is
called vector generation or line generation, and the
algorithm for them are known as vector generation
algorithm or line drawing algorithm.
 Line drawing is achieved by calculating
intermediate points along a line path between two
specified end point positions.
 These intermediate points are calculated from the
equation of the line.
 The Cartesian slope-intercept equation for a straight
line is
y = mx + b

Line path between endpoint positions 𝑥1 , 𝑦 1 and 𝑥 2 , 𝑦 2


 where m - represents the slope of the line
b - represents the intercept at y-axis.

 If the two endpoints of the line are 𝑥1 , 𝑦 1and 𝑥 2, 𝑦, 2


the slope and intercept is calculated as:
𝑦2− 𝑦1
𝑚=
𝑥 2 − 𝑥1

b  y1  m.x1
 Algorithms for displaying straight lines are based on
these equations and calculations.
 For any given x interval x , we can calculate
corresponding y interval y as
y  m.x
 Similarly, we can calculate x corresponding to a
specified y as
y
x 
m

 For lines with slope value |m| < 1, x is increased


and y calculated, whereas for slopes |m| > 1, y is
increased and x calculated.
Digital Differential Analyser (DDA) Line Drawing
Algorithm
 The DDA algorithm is a line algorithm based on
calculating either x or y

We know the Slope-Intercept Equation of line is:


y = mx + c
y2  y1
m
x2  x1

 We sample the line at unit intervals in one coordinate


and determine corresponding integer values nearest
the line path for the other coordinate.
Straight line segment with five sampling positions along
the x axis between x, and x
Case 1: Slope is positive and less than one:
 Consider the line with positive slope. If the slope
value is less than or equal to 1 then x  1 and we
have to calculate y value.
y  m.x

y  m sin cex  1


 yk 1  yk  m
 yk 1  yk  m

 Here ‘k’ is integer value that starts from 1 and always


increases by 1.
 Here ‘m’ is float value so we have to round off the ‘y’
value to nearest integer value.
Case 2: Slope is positive and greater than one:
 For slope greater than 1 then y  1 and we have to
calculate x value.
y  m.x
y
x 
m

x 
1
sin cey  1
m
1
 xk 1  xk 
m
1
 xk 1  xk   
m
For negative slope:
 For negative slope the above same cases are possible
but depends of the value of x or y we have to either
increase or decrease their values.

Summary:
 In short, for | m | < 1 we have to increase or decrease
the value by 1 depends on the value of .
 If is positive then we have to increase its and if
is negative then we have to decrease its value.
 Then we have to calculate the value of y based on
If is positive then y value is incremented by ‘m’
and if is negative then y is decremented by ‘m’.
 Similar case is possible for |m| > 1 but here the
Algorithm:
1. Read (x1, y1) and (x2, y2) (First and second coordinate
of line)
2. dx = x2 – x1;
dy = y2 – y1;
3. If absolute(dx) > absolute(dy) then
s = absolute(dx)
Else
s = absolute(dy)
End if
4. xinc = dx / s
yinc = dy / s
5. x = x1;
y = y1;
6. setpixel(round(x), round(y), color)
7. For i = 1 to s
x = x + xinc
y = y + yinc
setpixel(round(x), round(y), color)
End for
8. Exit

Disadvantages:
 In DDA line drawing algorithm we have to perform
rounding of the float value and this rounding may
cause the line to change from original pixel position.
 Also this rounding requires more time. Along with the
rounding operation it also involves the floating-point
arithmetic operation, which also requires more time.
Examples
Draw a line using DDA using algorithm
1. (2,2) and (9,2)
2. (2,5) and (2,12)
3. (5,4) and (12,7)
4. (5,7) and (10,5)
Calculate the points between the starting point (5, 6) and
ending point (8, 12).
Bresenham's Line Algorithm
 To remove the drawbacks of DDA algorithm
Bresenham has given new line drawing algorithm
based on integer increment/decrement in x and y
value.
 Sampling at unit x intervals in these examples, we
need to decide which of two possible pixel positions is
closer to the line path at each sample step.

Figure a Figure b
 Starting from the left endpoint shown in Fig. a, we
need to determine at the next sample position whether
to plot the pixel at position (11, 11) or the one at (11,
12).
 Similarly, Fig. b shows-a negative slope-line path
starting from the left endpoint at pixel position (50,
50). In this one, do we select the next pixel position as
(51,501 or as (51,49)?
 These questions are answered with Bresenham's line
algorithm by testing the sign of an integer parameter,
whose value is proportional to the difference between
the separations of the two pixel positions from the
actual line path.
 Consider the line having positive slope and slope
value is less than 1.
 First the pixel is plotted at co-ordinates xk , yk
 Then the x value is always increased by 1 and find
out whether the y value is incremented by 1 or not.
 So the next pixel position is either ( 𝑥 𝑘+1 , 𝑦 𝑘 +1 )
 From the line equation we get the original y value at
𝑥 𝑘+1as under:
y  m  xk  1  b
 As shown in figure, the distance between and y
is d1 and distance between yk  1 and y is d 2
Bresenham Line Drawing method

d1  y  yk
 m  xk  1  b  yk
d 2   yk  1  y
 yk  1  m  xk  1  b
 The difference between these two separations is
d1  d 2  m  xk  1  b  yk   yk  1  m  xk  1  b 

d1  d 2  2m  xk  1  2 y k  2b  1

 A decision parameter pk for the kth step in the line


algorithm can be obtained by rearranging Eq above so that
it involves only integer calculations.
 We accomplish this by substituting, m  yx where y and
x are the vertical and horizontal separations of the endpoint
positions, and defining:
from,

d1  d 2   2y  xk  1 / x   2 yk  2b  1

x  d1  d 2   2y  xk  1  2xyk  2xb  x

x  d1  d 2   2yxk  2y  2xyk  2xb  x

x  d1  d 2   2yxk  2xy k  2y  2xb  x

 In eq above, last three terms are constant so we can put


some constant b in place of them.
 Also we put x  d1  d 2   pk as decision parameter.
pk  2yxk  2xyk  b
 As like eq. above we can find the value pk 1 as under:
pk 1  2yxk 1  2xyk 1  b

By subtracting eqns. we get:


pk 1  pk  2yxk 1  2xyk 1  b   yxk  2xyk  b 

pk 1  pk  2y  xk 1  xk   2x  yk 1  yk 

But xk 1  xk  1

pk 1  pk  2y  2x  yk 1  yk 
 Here from eq. above we can take the decision whether
we have to increase y value or not.
 If decision parameter p is < 0 then d  d that
1 2

means y is more closer to yk so we have to


plot  x , y  and if decision parameter p is > 0
k k

then d1  d2 that means y is more closer to  y  1 so


k

we have to plot  x , y  1
k k

 Here we are calculating the decision parameter value


from previous value but for initial point we can
determine decision parameter by:

p0  2y  x
 Here we have only considered the case of positive
slope with less than or equal to 1. Similarly we can
derive the equation for the slope greater than 1 but
for that purpose the action of x and y is interchanged.
Examples
Calculate the point between the following starting
coordinate and ending coordinate.
1. (1,1) and (8,5)
2. (9,8) and (14,22)
3. (20, 10) and (30, 18)
4. (35,40) and (43,45)

You might also like