Module 2
Output Primitives
Output Primitives: Points and lines − Line Drawing Algorithm: DDA and
Bresenham’s Algorithm − Midpoint Circle Generating Algorithm − Line
Attributes − Color and Grayscale Levels.
Line Drawing Algorithms
Cartesian equation:
y = mx + c y2
where y1
m – slope x1 x2
c – y-intercept
2
Slope
+ve -ve
if |m| = 1 45°
45°
θ = 45°
if |m| < 1
-45° < θ < 45° θ°
θ°
if |m| > 1
45° < θ < 90° or θ°
-90° < θ < -45° θ°
3
|m| = 1
y=x y
m=1
8
c=0
7
x y 6
0 0 5
1 1 4
2 2 3
3 3 2
4 4 1
5 5 0
6 6 0 1 2 3 4 5 6 7 8 x
7 7
4
8 8
|m| < 1
y=½x+1 y
m=½ 8
c=1 7
6
x y round(y)
5
0 1 1
4
1 1.5 2
2 2 2 3
3 2.5 3 2
4 3 3 1
5 3.5 4 0
6 4 4 0 1 2 3 4 5 6 7 8 x
7 4.5 5
8 5 5 5
|m| > 1
y = 3x - 2 y
m=3 8
c = -2 7
x y round(y) 6
0 -2 -2 5
1 1 1 4
2 4 4 3
3 7 7 2
4 10 10
1
5 13 13
0
6 16 16
0 1 2 3 4 5 6 7 8 x
7 19 19
8 22 22 6
outside
The Digital Differential Analyzer
(DDA) Algorithm
means that for a unit (1) change in x there is
m-change in y.
x y Do not use
i.e. y = 3x + 1 m=3 0 1 y = 3x + 1
to calculate y.
1 4 Use m
2 7
3 10
4 13
5 16
means that for a unit (1) change in y there is
1/m change in x. 7
The DDA Method
• Uses differential equation of the line : m
• If slope |m| ≤ 1 then increment x in steps of 1 pixel
and find corresponding y-values.
• If slope |m| > 1 then increment y in steps of 1 pixel
and find corresponding x-values.
8
step through in x step through in y
The DDA Method
Desired
line
(xi+1,round(yi+m))
(xi,yi) (xi+1,yi+m)
(xi,round(yi))
9
if slope m ≥ 0
if |m| ≤ 1 if |m| > 1
xi+1 = xi + 1 yi+1 = yi + 1
yi+1 = yi + m xi+1 = xi + 1/m
Right
Right
Left
10
Left
Proceeding from right-endpoint to left-endpoint
if slope m ≥ 0
if |m| ≤ 1 if |m| > 1
xi+1 = xi - 1 yi+1 = yi - 1
yi+1 = yi - m xi+1 = xi - 1/m
Right
Right
Left
11
Left
if slope m < 0
if |m| ≤ 1 if |m| > 1
xi+1 = xi + 1 yi+1 = yi - 1
yi+1 = yi + m xi+1 = xi - 1/m
Left
Left
Right
12
Right
Proceeding from right-endpoint to left-endpoint
if slope m < 0
if |m| ≤ 1 if |m| > 1
xi+1 = xi - 1 yi+1 = yi + 1
yi+1 = yi - m xi+1 = xi + 1/m
Left
Left
Right
13
Right
Example (DDA)
x y round(y)
y
0 1 1 8
1 4/3 1 7
2 5/3 2 6
5
3 2 2
4
4 7/3 2
3
5 8/3 3
2
6 3 3 1
7 10/3 3 0
8 11/3 4 0 1 2 3 4 5 6 7 8 x 14
Example (DDA)
y x round(x)
y
8 0 0 8
7 1/3 0 7
6 2/3 1 6
5
5 1 1
4
4 4/3 1
3
3 5/3 2
2
2 2 2 1
1 7/3 2 0
0 8/3 3 0 1 2 3 4 5 6 7 8 x 15
void LineDDA(int x0, int y0, int x1, int y1)
{
int dx = x1 – x0, dy = y1 – y0, steps;
if (abs(dx)>abs(dy)) steps = abs(dx);
else steps = abs(dy);
// one of these will be 1 or -1
double xIncrement = (double)dx / (double )steps;
double yIncrement = (double)dy / (double )steps;
double x = x0;
double y = y0;
setPixel(round(x), round(y));
for (int i=0; i<steps; i++) {
{
x += xIncrement;
y += yIncrement;
setPixel(round(x), round(y));
}
}
Note: The DDA algorithm is faster than the direct use of y = mx + c.
It eliminates multiplication; only one addition. 16
Example
• Draw a line from point (2,1) to (12,6)
• Draw a line from point (1,6) to (11,0)
7
0 1 2 3 4 5 6 7 8 9 10 11 12
17
Bresenham's Line Algorithm
❖ This algorithm is used for scan converting a line. It was
developed by Bresenham.
❖ It is an efficient method because it involves only integer
addition, subtractions, and multiplication operations.
These operations can be performed very rapidly so lines
can be generated quickly.
❖ In this method, next pixel selected is that one who has the
least distance from true line.
❖
18
Bresenham's Line Algorithm
Once a pixel in choose at any step
The next pixel is
1. Either the one to its right (lower-bound for the line)
2. One top its right and up (upper-bound for the line)
19
Bresenham's Line Algorithm
❖ To chooses the next one between the bottom pixel S and top pixel T.
❖ If S is chosen
➢ xi+1=xi+1 and yi+1=yi
❖ If T is chosen,
➢ xi+1=xi+1 and yi+1=yi+1
❖ The actual y coordinates
➢ x = xi+1 is y=mxi+1+b
20
Bresenham's Line Algorithm
❖ The distance from S to the actual line in y directions = y-yi
❖ The distance from T to the actual line in y direction
❖ t = (yi+1)-y
❖ Now consider the difference between these 2 distance values
➢ s-t
❖ When (s-t) <0 ⟹ s < t
➢ The closest pixel is S
❖ When (s-t) ≥0 ⟹ s ≥ t
➢ The closest pixel is T
❖ This difference is s-t = (y-yi)-[(yi+1)-y] = 2y - 2yi -1
21
Bresenham's Line Algorithm
Substituting m by and introducing decision variable
di=△x (s-t)
di=△x (2 (xi+1)+2b-2yi-1)
di=2△y.xi-2△x.yi+c
Where, c= 2△y+△x (2b-1)
We can write the decision variable di+1 for the next slip on
di+1=2△y.xi+1-2△x.yi+1+c
di+1-di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Since x(i+1)=xi+1,we have
di+1+di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
22
Bresenham's Line Algorithm
If chosen pixel is at the top pixel T (i.e., di≥0)⟹ yi+1=yi+1
di+1=di+2△y-2△x
If chosen pixel is at the bottom pixel T (i.e., di<0)⟹ yi+1=yi
di+1=di+2△y
Finally, we calculate d1
d1=△x[2m(x1+1)+2b-2y1-1]
d1=△x[2(mx1+b-y1)+2m-1]
Since mx1+b-yi=0 and m = , we have
d1=2△y-△x
23
Bresenham's Line Algorithm
Advantage:
1. It involves only integer arithmetic, so it is simple.
2. It avoids the generation of duplicate points.
3. It can be implemented using hardware because it does not use multiplication
and division.
4. It is faster as compared to DDA (Digital Differential Analyzer) because it does
not involve floating point calculations like DDA Algorithm.
Disadvantage:
1. This algorithm is meant for basic line drawing only Initializing is not a part of
Bresenham's line algorithm. So to draw smooth lines, you should want to look into
a different algorithm.
24
Bresenham's Line Algorithm
❖ Given- Starting coordinates = (X0, Y0) and Ending coordinates =
(Xn, Yn)
❖ The points generation using Bresenham Line Drawing Algorithm
(for ΔY/ΔX >= 0) involves the following steps-
❖ Step-01:
❖ Calculate ΔX and ΔY from the given input.
❖ These parameters are calculated as-
➢ ΔX = Xn – X0
➢ ΔY =Yn – Y0
❖ Step-02:
❖ Calculate the decision parameter Pk.
❖ It is calculated as- 25
❖ Pk = 2ΔY – ΔX
Bresenham's Line Algorithm
❖ Step-03:
❖ Suppose the current point is (Xk, Yk) and the next point is (Xk+1, Yk+1).
❖ Find the next point depending on the value of decision parameter Pk.
❖ Follow the below two cases-
26
Bresenham's Line Algorithm
Step-04:
Keep repeating Step-03 until the end point is reached or number of iterations
equals to (ΔX-1) times.
27
Bresenham's Line Algorithm
Draw a line using from (2,3) to (7,6)
28
Bresenham's Line Algorithm
❖ Draw a line using from (2,3) to (5,8)
29
Bresenham's Line Algorithm
❖ Draw a line using from (2,3) to (5,8)
➢ Here, ΔY =5 > ΔX=3, so, Pk = 2ΔX - Δy
➢ Here, Yi+1 = Yi + 1, Xi+1 = Xi or Xi+1
depending on the sign of Pk.
30
Bresenham's Line Algorithm
❖ Draw a line using from (2,3) to (5,8)
31
Bresenham's Line Algorithm
❖ Draw a line using from (2,5) to (7,4)
➢ if ΔY/ΔX < 0, and |ΔY| < |ΔX|, Pk = 2|ΔY| -
|ΔX|
➢ if Pk <0,
■ Pk+1=Pk + 2|ΔY|,
■ xk+1 = xk + 1,
■ yk+1 = yk
➢ if Pk >0,
■ Pk+1=Pk + 2|ΔY| - 2|ΔX|,
■ xk+1 = xk + 1,
■ yk+1 = yk-1 32
Bresenham's Line Algorithm
❖ Draw a line using from (4,1) to (2,7).
➢ if ΔY/ΔX < 0, and |ΔY| > |ΔX|, Pk = 2|ΔX| -
|ΔY|
➢ if Pk <0,
■ Pk+1= Pk + 2|ΔX|,
■ xk+ 1 = xk,
■ yk+1 = yk + 1
➢ if Pk >0,
■ Pk+1= Pk + 2|ΔX| - 2|ΔY|,
■ xk+1 = xk-1,
■ yk+1 = yk + 1. 33
Midpoint Circle Drawing
Algorithm
34
Function of Circle – Steps
❖ Input: Radius ( r ) and Centre (xi, yi)
❖ Step 1:
➢ First point is (0,r) Plotted point: (xi,yi+r)
➢ P0 = 1-r
❖ Step 2:
➢ If Pk < 0,
■ xk+1 = xk + 1
■ yk+1 = yk
■ Pk+1 = Pk + 2*xk+1 + 1
➢ Else
■ xk+1 = xk + 1
■ yk+1 = yk-1
■ Pk+1 = Pk - 2*yk+1+ 2*xk+1 + 1 35
➢ Plotted point: (xi + xk+1,yi + yk+1)
Function of Circle – Example 1
Iteration 1
P0 = 1 – r
= 1 – 10
= -9
36
Function of Circle – Example 1
Iteration 1
P0 = -9
P0 < 0
37
Function of Circle – Example 1
Iteration 2
P0 < 0
Pk+1 = Pk + 2*xk+1 + 1
38
Function of Circle – Example 1
Iteration 2
P1 < 0
39
Function of Circle – Example 1
Iteration 2
P2 < 0
40
Function of Circle – Example 1
Iteration 2
P2 < 0
41
Function of Circle – Example 1
Iteration 2
P3 > 0
42
Function of Circle – Example 1
Iteration 2
P3 > 0
Pk+1 = Pk - 2*yk+1+ 2*xk+1 + 1
43
Function of Circle – Example 1
Iteration 2
P4 < 0
44
Function of Circle – Example 1
Iteration 2
P5 > 0
45
Function of Circle – Example 1
Iteration 2
P6 > 0
46
Function of Circle – Example 1
Iteration 2
P7 > 0
47
Function of Circle – Example 1
Iteration 2
P7 > 0
48
Function of Circle – Example 1
Iteration 2
P7 > 0
49
Function of Circle – Example 1
50
Function of Circle – Example 1
51
Function of Circle – Example 1
52
Function of Circle – Example 1
53
Function of Circle – Example 2
54
Line Attributes
❖ Basic attributes of a straight line segment
are its type, its width, and its color.
❖ In some graphics packages, lines can
also be displayed using selected pen or
brush options.
55
Line Attributes
❖ Line Type:
➢ Possible selections for the line-type
attribute include solid lines, dashed
lines, and dotted lines.
➢ We modify a line drawing algorithm to
generate such lines by setting the
length and spacing of displayed solid
sections along the line path.
56
Line Attributes
❖ Line Type:
Three different types of lines
57
Line Attributes
❖ Line Width:
➢ Implementation of line- width options
depends on the capabilities of the
output device.
➢ A heavy line on a video monitor could
bc displayed as adjacent parallel lines,
while a pen plotter might require pen
changes.
58
Line Attributes
❖ Line Width:
➢ For raster implementation, a
standard-width line is generated with
single pixels at each sample position,
as in the Bresenham algorithm.
➢ Other-width link line Attributes are
displayed as positive integer multiples
of the standard line by plotting
additional pixels along adjacent parallel
line paths.
59
Line Attributes
❖ Line Width:
➢ An example for m< 1
60
Line Attributes
❖ Line Width:
61
Line Attributes
❖ Line Width:
➢ A miter join is accomplished by extending
the outer boundaries of each of the two
lines until they meet.
➢ A round join is produced by capping the
connection between the two segments with
a circular boundary whose diameter is
equal to the line width.
➢ A bevel join is generated by displaying the
line segments with butt caps and filling in
the triangular gap where the segments 62
meet.
Line Attributes
❖ Pen and Brush Options
63
Line Attributes
❖ Pen and Brush Options
➢ With some packages, lines can be
displayed with pen or brush selections.
Options in this category include shape,
size, and pattern.
➢ These shapes can be stored in a pixel
mask that identifies the array of pixel
positions that are to be set along the line
path. For example, a rectangular pen can
be implemented with the mask shown in
next slide. 64
Line Attributes
❖ Pen and Brush Options
65
Line Attributes
❖ Pen and Brush Options
➢ Lines can be displayed with selected patterns by
superimposing the pattern values onto the pen or
brush mask.
66
COLOR AND GRAYSCALE
LEVELS
❖ Various color and intensity-level options can
be made available to a user, depending on
the capabilities and design objectives of a
particular system.
❖ In a color raster system, the number of color
choices available depends on the amount of
storage provided per pixel in the frame buffer.
❖ We can store color codes directly in the frame
buffer, or we can put the color codes in a
separate table and use pixel values as an
index into this table. 67
COLOR AND GRAYSCALE
LEVELS
❖ A minimum number of colors can be provided
in this scheme with 3 bits of storage per pixel,
68
COLOR AND GRAYSCALE
LEVELS
69
COLOR AND GRAYSCALE
LEVELS
❖ Conversion between RGB to CMYK:
➢ RGB to CMYK conversion formula
➢ The R,G,B values are divided by 255 to change the range
from 0..255 to 0..1:
■ R' = R/255
■ G' = G/255
■ B' = B/255
➢ The black key (K) color is calculated from the red (R'), green
(G') and blue (B') colors:
■ K = 1-max(R', G', B')
➢ The cyan color (C) is calculated from the red (R') and black
(K) colors:
■ C = (1-R'-K) / (1-K)
❖ 70
COLOR AND GRAYSCALE
LEVELS
❖ Conversion between RGB to CMYK:
➢ The magenta color (M) is calculated from the green (G') and
black (K) colors:
■ M = (1-G'-K) / (1-K)
➢ The yellow color (Y) is calculated from the blue (B') and
black (K) colors:
■ Y = (1-B'-K) / (1-K)
❖
71
COLOR AND GRAYSCALE
LEVELS
❖ CMYK to RGB conversion formula
➢ The R,G,B values are given in the range of 0..255.
➢ The red (R) color is calculated from the cyan (C) and
black (K) colors:
■ R = 255 × (1-C) × (1-K)
➢ The green color (G) is calculated from the magenta
(M) and black (K) colors:
■ G = 255 × (1-M) × (1-K)
➢ The blue color (B) is calculated from the yellow (Y)
and black (K) colors:
■ B = 255 × (1-Y) × (1-K)
72
COLOR AND GRAYSCALE
LEVELS
❖ Grayscale
➢ With monitors that have no color capability, color
functions can be used in an application program to
set the shades of gray, or grayscale, for displayed
primitives.
➢ Numeric values over the range from 0 to 1 can be
used to specify grayscale levels, which are then
converted to appropriate binary codes for storage in
the raster.
➢ With the display intensity corresponding to a given
color index ci calculated as
■ intensity = 0.5[min(r, g, b) + max(r, g, b)] 73
COLOR AND GRAYSCALE
LEVELS
❖ Grayscale
74
Thank You
75