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

Module 2 CG

The document discusses output primitives in computer graphics, which are basic geometric shapes used to create complex graphics. It explains scan conversion, a process that represents continuous graphics as discrete pixels, and details algorithms for line drawing, including DDA and Bresenham's algorithms. Additionally, it covers the mid-point circle drawing algorithm for representing circles on raster displays.

Uploaded by

Krishnendhu C.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module 2 CG

The document discusses output primitives in computer graphics, which are basic geometric shapes used to create complex graphics. It explains scan conversion, a process that represents continuous graphics as discrete pixels, and details algorithms for line drawing, including DDA and Bresenham's algorithms. Additionally, it covers the mid-point circle drawing algorithm for representing circles on raster displays.

Uploaded by

Krishnendhu C.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Module 2

Output Primitives
 Output Primitives are basic geometric shapes or structures.
 For example, Point, Straight Line, Sector, Arc, Ellipse, Rectangle, etc.
 In computer graphics, output primitives are the basic elements or
building blocks used to create more complex graphical objects.
 Graphical programming packages/software tools provide functions to
combine and manipulate these Output Primitives to build/create
complex graphics scenes or images.
Scan Conversion
 Scan Conversion is the process of representing continuous graphics
objects (like lines, circles, or polygons) as a collection of discrete pixels
that can be displayed on output display devices.
 The video display devices work by converting the binary values (0 and 1)
into pixel information. When the pixel information is 0 then the pixel is
off and when the pixel information is 1 then the pixel is one.
 Using this ability, graphics computers can create images made up of tiny
dots called pixels.
 Any model of graphics can be generated by controlling the matrix of dots
or points. For generating graphical objects, many algorithms have been
developed.
 The algorithms implementation varies from one computer system to
another computer system. Some algorithms are implemented using the
software. Some are performed using hardware or firmware. Some are
performed using various combinations of hardware, firmware, and
software.
Advantage of developing algorithms for scan conversion
 Algorithms can generate graphics objects at a faster rate.
 Using algorithms memory can be used efficiently.
 Algorithms can develop a higher level of graphical objects.
Examples of objects, which can be scan converted are:
 Point
 Line
 Sector
 Arc
 Ellipse
 Rectangle
 Polygon
 Characters
 Filled Regions
Scan Conversion of Line

 A straight line may be defined by two endpoints & an equation.


 The two endpoints are described by (x1,y1) and (x2,y2).
 The equation of the line is used to determine the x, y coordinates of all
the points that lie between these two endpoints.
 Using the equation of a straight line,
y = mx + b
m is the slope
m = Δx/Δy
b is the y interrupt
 We can find values of y by incrementing x from x =x1, to x = x2.
 By scan-converting these calculated x, y values, we represent the line as
a sequence of pixels.
Properties of Good Line drawing Algorithm
1. Line should appear Straight:
 The primary goal of any line drawing algorithm is to represent the
line as straight as possible.
 To achieve this, the algorithm must select addressable points
(pixels) that are close to the ideal geometric line, minimizing
distortion. If the selected points are not well chosen, the line
might appear jagged or even crossed.
2. Lines Should plotted accurately:
 A good line drawing algorithm must ensure that lines start and
finish at the correct locations.
 If the line terminates incorrectly, the result may not be as
expected, leading to visual errors. The termination of a line should
be handled precisely so that it appears to be complete and
uninterrupted.
3. Lines should have constant density
 Line Density: This refers to the evenness of the distribution of
pixels along the line.
 The number of pixels plotted along a line should be proportional
to the line's length. For consistent visual quality, the pixels should
be equally spaced along the line. Uneven pixel density can make
the line appear irregular or inconsistent.
4. Line density should be independent of line length and angle
 A good algorithm should ensure that the density of the line is
consistent, regardless of the line's length or angle.
 This can be achieved by estimating the line length and using a
line-generation algorithm that ensures the spacing between
plotted pixels is maintained.
 No matter if the line is short or long, horizontal, vertical, or
diagonal, the pixel density should remain consistent.
5. Line should be drawn rapidly.
 The algorithm should be designed to generate lines quickly,
minimizing the computational cost.
 Special-purpose hardware or optimized software algorithms can
be used to accelerate the line drawing process.
Algorithm for line Drawing
1. Direct use of line equation
2. DDA (Digital Differential Analyzer)
3. Bresenham's Algorithm
Digital Differential Analyzer (DDA) Algorithm
DDA (Digital Differential Analyzer) is a line drawing algorithm used in computer
graphics to draw a line between two specified endpoints on a screen. It is a
simple and efficient algorithm that works by using the incremental difference
between the x-coordinates and y-coordinates of the two endpoints to plot the
line.
Algorithm:
Step 1: Start the algorithm.
Step 2: Get the input of two endpoints (x1, y1) and (x2, y2).
Step 3: Calculate the differences
Δx = x2 - x1 and Δy = y2 - y1
Step 4: Determine the number of steps
Check if ∣Δx∣ > ∣Δy∣
If yes, steps = ∣Δx∣
If no, steps = ∣Δy∣
Step 5: Compute the increments
x_inc = Δx/steps and y_inc = Δy/steps
Step 6: Initialize the starting point
x = x1 and y = y1
Step 7: Set the first pixel and plot the pixel at (x1, y1).
Step 8: Iterate for the calculated number of steps:

 Update the x and y coordinates


x = x + x_inc
y = y + y_inc
 Plot the pixel at (Round(x), Round(y))
Step 9: Repeat Step 8 until the required number of steps is completed.
Step 10: End the algorithm.
Example:
Drawing a Line from (2, 2) to (6, 6) Using DDA Algorithm
Given the two endpoints
 Start point: (x1, y1) = (2,2)
 End point: (x2, y2) = (6,6)
Calculate differences
 Δx = x2 - x1 = 6 – 2 = 4
 Δy = y2 - y1 = 6 – 2 = 4
Determine the number of steps
 Steps = max(∣Δx∣, ∣Δy∣) = max(4, 4) = 4
Compute the increments
 x_inc = Δx/steps = 4/4 =1
 y_inc = Δy/steps = 4/4 = 1
Initialize the starting point
 x = x1 = 2
 y = y2 = 2
Iteratively plot the pixels
Steps:
1. x = 2 and y = 2
plot (Round(x), Round(y))
plot (2, 2)
2. x = x + x_inc = 2 + 1 = 3 // x = 2 and x_inc = 1
y = y + y_inc = 2 + 1 = 3 // y = 2 and y_inc = 1
plot (Round(x), Round(y)
plot (3, 3)
3. x = x + x_inc = 3 + 1 = 4 // x = 3 and x_inc = 1
y = y + y_inc = 3 + 1 = 4 // y = 3 and y_inc = 1
plot (Round(x), Round(y)
plot (4, 4)
4. x = x + x_inc = 4 + 1 = 5 // x = 4 and x_inc = 1
y = y + y_inc = 4 + 1 = 5 // y = 4 and y_inc = 1
plot (Round(x), Round(y)
plot (5, 5)
5. x = x + x_inc = 5 + 1 = 6 // x = 5 and x_inc = 1
y = y + y_inc = 5 + 1 = 6 // y = 5 and y_inc = 1
plot (Round(x), Round(y)
plot (6, 6)
Now the iteration is completed
The plotted points for the line from (2, 2) to (6, 6) using the DDA algorithm are
(2, 2), (3, 3), (4, 4), (5, 5), (6, 6)
Bresenham's Line Drawing Algorithm
The Bresenham Line Drawing Algorithm is a computer graphics algorithm used
to draw a straight line between two points (x1, y1) and (x2, y2) on a pixel grid. It
uses integer arithmetic instead of floating-point calculations for efficiency.
In this algorithm, once a pixel is chosen at any step then the next pixel is:

 Either the one to its right (lower-bound for the line)


 One top its right and up (upper-bound for the line)
Algorithm:
Step 1: Start Algorithm
Step 2: Input
The coordinates of the starting point (x1, y1) and ending point (x2, y2)
Step 3: Initialization
Calculate:
dx = x2 – x1
dy = y2 – y1
Determine the initial decision parameter d:
d=2 x dy – dx
Set increments:
ΔE = 2 x dy //Horizontal move
ΔNE = 2(dy−dx) //Diagonal move
Step 4: Set the starting point (x, y) = (x1, y1)
Step 5: Plot the initial point (x, y)
Step 6: Iterate:
While x < x2
If d < 0:
The next pixel is horizontally to the right: x = x + 1, d = d + ΔE
If d ≥ 0:
The next pixel is diagonally up and to the right: x = x + 1, y = y + 1, d = d + ΔNE
Plot the pixel at (x, y)
Step 7: Stop when x = x2
Step 8: End Algorithm
Working:
The algorithm calculates whether the next pixel should be on the same
horizontal row or on the next row above, based on the decision variable d:
1. If d < 0, the error is smaller if we stay on the same row (lower bound).
2. If d ≥ 0, the error is smaller if we move up one row (upper bound).
Example:
Input:
 Starting point: (x1, y1) = (2, 2)
 Ending point: (x2, y2) = (6, 4)
Calculation:
Calculate the difference,
dx = x2 – x1 = 6 – 2 = 4
dy = y2 – y1 = 4 – 2 = 2
Calculate the decision variable,
d = 2 x dy – dx = 2 x 2 – 4 = 4 – 4 = 0
Calculate the Increments for the decision variable,
ΔE = 2 x dy = 2 x 2 = 4
ΔNE = 2(dy−dx) = 2(2 – 4) = -4
Iteration:
Iteration 1
Current point: (x, y) = (2, 2)
Decision variable: d = 0
Since d >= 0
x=x+1=2+1=3
y=y+1=2+1=3
move diagonally (3, 3)
Update d: d = d + ΔNE = 0 + -4 = -4
Iteration 2
Current point: (x, y) = (3, 3)
Decision variable: d = -4
Since d < 0
x=x+1=3+1=4
y=3
move horizontally (4, 3)
Update d: d = d + ΔE = -4 + 4 = 0
Iteration 3
Current point: (x, y) = (4, 3)
Decision variable: d = 0
Since d >= 0
x=x+1=4+1=5
y=y+1=3+1=4
move diagonally (5, 4)
Update d: d = d + ΔNE = 0 + -4 = -4
Iteration 4
Current point: (x, y) = (5, 4)
Decision variable: d = -4
Since d < 0
x=x+1=5+1=6
y=6
move horizontally (6, 4)
Update d: d = d + ΔE = -4 + 4 = 0
Iteration
Current point: (x, y) = (6, 4)
The line ends because x = x2 = 6
Output:
The algorithm plots the points (2, 2), (3, 3), (4, 3), (5, 4), (6, 4) to form the line.
Difference between DDA Algorithm and Bresenham's Line Algorithm
DDA Algorithm Bresenham's Line Algorithm
DDA Algorithm uses floating point, i.e. Real Arithmetic. Bresenham's Line Algorithm uses fixedpoint, i.e.
Integer Arithmetic

DDA Algorithms use multiplication & division Bresenham's Line Algorithm uses only
operations subtraction and addition operations

DDA Algorithm is slowly than Bresenham's Line Bresenham's Algorithm is faster than DDA
Algorithmin line drawing because it uses real arithmetic Algorithm in line because it involves only
(Floating Point operation) addition & subtraction in its calculation and
uses only integer arithmetic.

DDA Algorithm is not accurate and efficient as Bresenham's Line Algorithm is more accurate
compared to Bresenham's Line Algorithm. and efficient than DDA Algorithm.

DDA Algorithm can draw circles and curves. But they Bresenham's Line Algorithm can draw circles
cannot draw it accurately as compared to Bresenham's and curves more accurately than DDA
Line Algorithm. Algorithm.

Scan Conversion of Circle


The scan conversion of a circle involves determining which pixels should be
turned on to represent a circle on a raster display. A circle is a set of points that
are equidistant from a central point. Mathematically, the relation is derived
from the Pythagorean theorem:
A circle is a set of points that are equidistant from a central point.
Mathematically, the relation is derived from the Pythagorean theorem:
Equation of a Circle with Center (0, 0):
x2 + y2 = r2
Equation of a Circle with Center (xc, yc):
(x – xc)2 + (y – yc)2 = r2
Algorithm for Circle Drawing
1. Mid-point Circle Drawing Algorithm
2. Bresenham's Circle Drawing Algorithm
Mid-point Circle Drawing Algorithm

 The mid-point circle drawing algorithm is used to calculate all the


perimeter points of a circle.
 In this algorithm, the mid-point between the two pixels is calculated
which helps in calculating the decision parameter.
 The value of the decision parameter will decide which pixel should be
chosen for drawing the circle.
 This algorithm only calculates the points for one octant and the points
for other octants are generated using the eight-way symmetry for the
circle.
Algorithm:
Step 1: Start Algorithm
Step 2: Plot the initial point such that x = 0 and y = r.
Step 3: Find the initial decision parameter: P = 5/4 – r.
Step 4: iteration
if (P < 0):
Set P = P + 2x + 3
x=x+1
y=y
if (P >= 0):
Set P = P+ 2(x-y) + 5
x=x+1
y=y–1
Step 5: Plot the complete circle by using 8-way symmetry.
(x, y), (y, x), (−x, y), (−y, x), (−x, −y), (−y, −x), (x, −y), (y, −x)
Step 6: Stop when x ≥ y
Step 7: End Algorithm
Example:
Let the circle radius r = 5 and its center is at the origin (xc, yc) = (0, 0)
Start with: x = 0, y = r = 5
Calculate the initial decision parameter:
P = 5/4 – r = 1 – 5 = -4
(5/4 = 1.25, it is often approximated to 1, to avoid fractions and to simplify the
calculation.)
Iteration 1
x = 0 and y = 5
P = -4 (P less than 0)
Update P:
P = P + 2x +3 = -4 + 0 + 3 = -1
x=x+1=0+1=1
y=5
Plot the symmetric points:
(1, 5), (5, 1), (−1, 5), (−5, 1), (−1, −5), (−5, −1), (1, −5), (5, −1)
Iteration 2
x = 1 and y = 5
P = -1 (P less than 0)
Update P:
P = P + 2x +3 = -1 + 2 + 3 = 4
x=x+1=1+1=2
y=5
Plot the symmetric points:
(2, 5), (5,2), (−2, 5), (−5, 2), (−2, −5), (−5, −2), (2, −5), (5, −2)
Iteration 3
x = 2 and y = 5
P = 4 (P greater than or equal to 0)
Update P:
P = P + 2(x – y) +5 = 4 + 2(2 – 5) +5 = 3
x=x+1=2+1=3
y=y–1=5–1=4
Plot the symmetric points:
(3, 4), (4, 3), (−3, 4), (−4, 3), (−3, −4), (−4, −3), (3, −4), (4, −3)
Iteration 4
x = 3 and y = 4
P = 3 (P greater than or equal to 0)
Update P:
P = P + 2(x – y) +5 = 3 + 2(3 – 4) +5 = 6
x=x+1=3+1=4
y=y–1=4–1=3
Plot the symmetric points:
(4, 3), (3, 4), (−4, 3), (−3, 4), (−4, −3), (−3, −4), (4, −3), (3, −4)
Now stop the algorithm since x >= y. In this case, x = 4 and y = 3.
Bresenham's Circle Drawing Algorithm

 Bresenham’s circle drawing algorithm is a circle drawing algorithm, which


calculates all the nearest points nearest to the circle boundary.
 It is an incremental method.
 It only uses integer arithmetic which makes it’s working faster as well as
less complex.
 The strategy that is followed in this algorithm is to select the pixel which
has the least distance with the true circle boundary and with then keep
calculating the successive points on the circle.
 As we know that the circle follows 8 symmetry property, i.e. if we know
the boundary coordinates of the first octant, the rest 7 octants’ value can
be easily calculated by changing their magnitudes or by interchanging
the coordinate values according to the respective octants.
 This algorithm calculates the location of the pixels similarly.
 Here, we calculate the values only for the first octant and the values for
the rest octants can be calculated by extending these values to the other
7 octants, using the eight-way symmetry property of the circle.
Algorithm:
Step 1: Start Algorithm
Step 2: Initialize x = 0, y = r.
Step 3: Calculate the decision parameter as follows: D = 3 – 2r.
Step 4:
if D < 0:
D = D + 4x + 6
x=x+1
y=y
if D ≥ 0:
D = D + 4(x – y) + 10
x=x+1
y=y–1
Step 5: Plot the complete circle by using 8-way symmetry.
Step 6: Stop when x ≥ y
Step 7: End Algorithm
Example:
Let the circle radius r = 5 and its center is at the origin (xc, yc) = (0, 0)
Start with: x = 0, y = r = 5
Calculate the decision parameter:
D = 3 – 2r
D=3–2x5
D = 3 – 10 = -7
Iteration 1
x = 0, y = 5 and D = -7
D<0
Update:
D = D + 4x + 6 = -7 + 4 x 0 + 6 = -1
x=x+1=0+1=1
y=y=5
Plot the symmetric points:
(1, 5), (5, 1), (−1, 5), (−5, 1), (−1, −5), (−5, −1), (1, −5), (5, −1)
Iteration 2
x = 1, y = 5 and D = -1
D<0
Update:
D = D + 4x + 6 = -1 + 4 x 1 + 6 = 9
x=x+1=1+1=2
y=y=5
Plot the symmetric points:
(2, 5), (5, 2), (−2, 5), (−5, 2), (−2, −5), (−5, −2), (2, −5), (5, −2)
Iteration 3
x = 2, y = 5 and D = 9
D >= 0
Update:
D = D + 4(x – y) + 10 = 9 + 4(2 - 5) + 10 = 7
x=x+1=2+1=3
y=y–1=5–1=4
Plot the symmetric points:
(3, 4), (4, 3), (−3, 4), (−4, 3), (−3, −4), (−4, −3), (3, −4), (4, −3)
Iteration 4
x = 3, y = 4 and D = 7
D >= 0
Update:
D = D + 4(x – y) + 10 = 7 + 4(3 - 4) + 10 = 13
x=x+1=3+1=4
y=y–1=4–1=3
Now stop the algorithm since x >= y. In this case, x = 4 and y = 3.
Polygon Filling
Polygon is an ordered list of vertices as shown in the following figure. For filling
polygons with particular colors, you need to determine the pixels falling on the
border of the polygon and those which fall inside the polygon.

Filled Area Primitives


Filled Area Primitives are used to filling solid colors to an area or image or
polygon. Filling the polygon means highlighting its pixel with different solid
colors. Following are two filled area primitives:
 Seed Fill Algorithm
 Scan Fill Algorithm
Seed Fill Algorithm
In this seed fill algorithm, we select a starting point called seed inside the
boundary of the polygon. The seed algorithm can be further classified into two
algorithms:
 Boundary Fill Algorithm
 Flood Fill Algorithm
1. Boundary Fill Algorithm
 The boundary fill algorithm uses polygon boundaries. It is also called
an edge fill algorithm. In this method, a seed point is taken in the
polygon boundary and checks whether the adjacent or neighbouring
pixel is colored or not. If the adjacent pixel is not colored then, fill it.
 One requirement for boundary fill algorithm is that the boundary has
to have a single color.
 The boundary algorithm works by checking the default color of the
pixel before filling. If the color is not boundary color, then you fill it
with the fill color and move to the next pixel. This approach proceeds
outwards pixel by pixel until it hits the boundary.
 There are two methods in which the boundary fill algorithm can be
implemented: four-connected pixel or eight-connected pixel.
Four-connected Pixel
In the four-connected pixel method, check the four-pixel adjacent to
the current pixel, namely towards left, right, top, and bottom.
Eight-connected Pixel
In the eight-connected pixel method, check the four-pixel adjacent to
the current pixel and also the pixel diagonal to it, namely towards left,
right, top, bottom, and the four diagonals.

Working of Boundary Fill Algorithm


Step 1: First, initialize the four values namely x, y, default color, fill
color.
Step 2: Define the value of boundary pixel color.
Step 3: Now, check if the current pixel is of the default color. If YES,
repeat Step 4 and Step 5 till the boundary pixels are reached.
Step 4: Change the default color of the current pixel.
Step 5: Repeat Step 3 and 4 for the neighbouring pixels.
Step 6: Exit.
Advantages:
 Very simple and easy algorithm.
 It can be used to generate creative paintings.
Disadvantages:
 In the four-connected approach, it does not color the corners.
2. Flood Fill Algorithm
 In this method, a point or seed that is inside the region is selected. This
point is called a seed point. Then four connected approaches or eight
connected approaches is used to fill with specified color.
 The flood fill algorithm has many characters similar to boundary fill.
But this method is more suitable when the boundary is of many colors
and the interior is to be filled with one color.
Advantages:
 Easy to understand and implement.
 Fast processing.
Disadvantages:
 Very slow algorithm
 Maybe fail for large polygons
 The Initial pixel required more knowledge about the surrounding
pixels.
Scan Fill Algorithm
1. Scanline Polygon Filling Algorithm

 The scanline fill algorithm is a computer graphics technique that fills


the interior of a polygon in an image. It works by scanning the image
from top to bottom, line by line, and filling in the spaces between the
polygon's edges using scanlines or horizontal lines.
 The purpose of this algorithm is to fill the interior pixels of a polygon,
given only the vertices and edges.
 The algorithm works by filling the points that are formed after the
intersection of scanlines with the polygon edges.
Algorithm:
Step 1: Find the intersection of the scanline with all the edges of the
polygon.
Step 2: Sort the intersection by increasing the x-coordinate i.e. from left
to right.
Step 3: Make pairs of intersections and fill in color within all the pixels
inside the pair.
Example:
y
8 12 16 20

X
List of intersection points:
8, 12, 16, 20
The Sorted order will be:
8, 12, 16, 20
Make pairs of the intersection:
(8, 12) and (16, 20)
Fill in all the pixels with the color inside the pixel.

You might also like