0% found this document useful (0 votes)
28 views53 pages

CG CH3 Scan Conversion2

The document discusses scan conversion techniques for converting geometrically defined graphics objects to pixels. It covers methods for scan converting points, lines, circles, and curves. Specific algorithms discussed include digital differential analyzer, Bresenham's line algorithm, and circle generation techniques.

Uploaded by

Nardos Tesema
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)
28 views53 pages

CG CH3 Scan Conversion2

The document discusses scan conversion techniques for converting geometrically defined graphics objects to pixels. It covers methods for scan converting points, lines, circles, and curves. Specific algorithms discussed include digital differential analyzer, Bresenham's line algorithm, and circle generation techniques.

Uploaded by

Nardos Tesema
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/ 53

Computer Graphics

Chapter Four
Scan Conversion
Overview of Graphics System
 Scan Conversion/Rasterization:
 2D Drawing & 3D Objects made of graphics primitives
 Points, lines, circles, & filled polygons
 Lines, circles, etc. exist in the continuous space – mathematically
 Converting from continuous space to discrete space
 Graphics program converts geometrically defined objects to a
set of pixels in the discrete (image) space

 Mathematics and algorithms of conversion will be discussed


 Methods to smooth out the differences
 Scan Converting a Point:
 Converting a point (x, y) in the real space to a pixel location (x’, y’)
Scan Conversion - Point

 Approach 1 (Simple Flooring):  Approach 2 (Added Flooring):


 Take the integer part of x and y  Align the integer values in the
assign them to x’ and y’ coordinate system for (x, y) with
 x' = Floor(x) and y' = Floor(y) the pixel coordinates
 The origin of the coordinate  Each point is converted as:
system for (x, y) shifts to the x' = Floor(x + 0.5) and y’=Floor(y + 0.5)
lower left corner of the pixel grid  Origin of the coordinate system
(Drops to the ground) for (x,y) at the center of pixel (0,0)
 Points that satisfy x’ < x < x’ + 1 and
y’ < y < y’ + 1 are mapped to pixel x’ - 0.5 < x < x' + 0.5
(x’, y’). y’ - 0.5 < y < y' + 0.5
Scan Conversion - Line
 Line Segment: portion of an ever extending line is drawn on
screen
 Defined by two end points 𝑃1 (x1,y1) and 𝑃2 (x2,y2) and a line
equation y = mx + b
 Horizontal, vertical, and diagonal (slope = 1) should be
handled as special cases – should not be scan converted
Scan Conversion - Line
 Direct Use of the Line Equation:
 1st scan points P1 and P2 to pixel coordinates P1’ and P2’
Where P1‘ is (x1’, y1’) and P2’ is (x2’, y2’)
 Calculate m as m = (y2’ — y1’)/ (x2’ — x1’)
 Calculate b as b = y1’ — mx1’
 Floating point computation involved, making it slower

 Case 1: (|m| <= 1) Case 2: (|m| > 1)


For every integer x between x1’ & x2’ For every integer y, y1’ < y < y2’
Find y = mx + b Find x = (y - b)/m
Then scan convert (x, y) Then scan convert (x, y)
Scan Conversion - Line
 DDA (Digital Differential Analyzer) Algorithm: (Steps)
 Step 1:
 Calculate dx (Δx), dy (Δy), and m (slope)
Δx = xn – x0, Δx =xn – x0, m = Δy / Δx

 Step 2:
 Find the number of steps or points in from the starting point to the
ending coordinates.
 if (abs (Δx) > abs(Δy)):
Steps = abs(Δx)
else:
Steps = abs(Δy)
Scan Conversion - Line
 DDA (Digital Differential Analyzer) Algorithm: (Steps)
 Step 3:
 Calculate the next point (xi+1 , yi+1) using (xi , yi)

Case 1: (m < 1) Case 2: (m = 1) Case 3: (m > 1)


xi+1 = round_off (1 + xi) xi+1 = round_off (1 + xi) xi+1 = round_off (1/m + xi)

yi+1 = round_off (m + yi) yi+1 = round_off (1 + yi) yi+1 = round_off (1 + yi)

 Step 4:
if (end point is not reached ):
Go to step 3 and repeat
Else:
i.e. number of points generated equals the number of
steps(Terminate)
Scan Conversion - Line
 DDA (Digital Differential Analyzer) Algorithm: Examples

Calculate the points between the starting point (5, 6) and ending point (8, 12)

 Step 1: Δx = 8 – 5 = 3 ; Δy = 12 – 6 = 6 ; m = Δy / Δx = 6 / 3 = 2

 Step 2: Calculate the number of steps from start to end


| Δx | < | Δy | => Number of steps becomes Δy = 6

 Step 3: Since m > 1; Use


xi+1 = round_off (1/m + xi)
yi+1 = round_off (1 + yi)
Scan Conversion - Line
 DDA (Digital Differential Analyzer) Algorithm: Examples
 Step 4: Iteratively compute the other points

Xi Yi Xi+1 Yi+1 Round off (Xi+1, Yi+1)


5 6 5.5 7 (6, 7)
6 8 (6, 8)
6.5 9 (7, 9)
7 10 (7, 10)
7.5 11 (8, 11)
8 12 (8, 12)
Scan Conversion - Line
 DDA (Digital Differential Analyzer) Algorithm: Examples
 Example 2: Calculate the points between the starting point (5, 6) and
ending point (13, 10).

 Example 3: Calculate the points between the starting point (1, 7) and
ending point (11, 17).
 Example 3: between (4,8),(8,10)
 Example 3: between (4,6),(7,9)
Scan Conversion - Line
 DDA (Digital Differential Analyzer) Algorithm:

Advantages Disadvantages

- Relatively simple - Extra overhead for round_off( )


- Somewhat easy to implement - Resulted lines are not smooth
because of round off( ) function
- The points generated by this
algorithm are not accurate.
Scan Conversion - Line
 Bresenham's Line Algorithm:
 Incremental method of scan-converting lines
 Uses only integer addition, subtraction, and multiplication by 2
 Assume 0 < m < 1
 Start with pixel choice for P1’ and move to the right horizontally
Scan Conversion - Line
 Bresenham's Line Algorithm:
 Line is best approximated by pixels that are closest (least distance) to the
true path

 Next pixel choice is either:


Pixel to the right (lower bound)
or
Pixel to the right up (Upper bound)
Scan Conversion - Line
 Bresenham's Line Algorithm:

 Step 1: Calculate Δx = xn – x0 and Δy = yn – y0 from the given input

 Step 2: Calculate the decision parameter Pi = 2Δy – Δx


Scan Conversion - Line
 Bresenham's Line Algorithm: distance between points to pixels

5
(xk+1, yk+1)

4
(xk, yk)
3
(xk+1, yk)

2 3 4 5
Scan Conversion - Line
 Bresenham's Line Algorithm:

 Step 3: Calculate Pi+1 & (Xi+1, Yk+1) based on Pi & (Xi, Yi)

Here we need to consider the current value of the parameter for computation

Case 1 ( Pi < 0) Case 2 (Pi >= 0)

Pi+1 = Pi + 2ΔY Pi+1 = Pi + 2ΔY - 2ΔX


Xi+1 = Xi + 1 Xi+1 = Xi + 1
Yk+1 = Yi Yk+1 = Yi + 1

 Step 4: Repeat Step 3 until reaching end point number of iteration equals
ΔX-1
Scan Conversion - Line
 Bresenham's Line Algorithm: Examples

Calculate the points between the starting coordinates (9, 18) and ending
coordinates (14, 22).

 Step 1: Calculate ΔX and ΔY from the given input


ΔX = Xn – X0 = 14 – 9 = 5
ΔY =Yn – Y0 = 22 – 18 = 4

 Step 2: Calculate the decision parameter


Pi = 2ΔY – ΔX
=3
 Step 3: Since Pk is > 0 – the pixel which lies diagonally above is selected

Xi+1 = 9 + 1=10 Yi+1 = 18 + 1=19 Pi+1 = 3+ (2x4) – (2x5) = 1


Scan Conversion - Line
 Bresenham's Line Algorithm: Examples

 Step 4: Repeat step 3 until endpoint is reached

Number of iterations = ΔX – 1 = 5-1 = 4 (Here it represents number of


intermediary points between first and last point)

Pi Pi+1 Xi+1 Yi+1


9 18
3 1 10 19
1 -1 11 20
-1 3 12 20
3 3 13 21
3 14 22
• Step 1: Input the two end-points of line, storing the left end-point in (X0, Y0).
• Step 2: Plot the point (X0, Y0).
• Step 3: Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first
value for the decision parameter as: 0 = 2 y – x Step 4: At each Xk along the
line, starting at k = 0, perform the following test: If Pk < 0, the next point to
plot is (Xk+1, Yk) and PK+1 = k + 2dy otherwise, Pk+1 = k + 2 y − 2 x Step 5:
Repeat
• step 4 (dx – 1) times. For m > 1, find out whether you need to increment X
while incrementing Y each time. After solving, the equation for decision
parameter Pk will be very similar, just the X and Y in the equation gets
interchanged.
Scan Conversion - Line
 Bresenham's Line Algorithm:

Advantages Disadvantages

- Easy to implement - Resulted line is not smooth


enough.
- Fast and incremental
- Generated points are more
accurate than DDA Algorithm
Scan Conversion - Curves
 Curve Functions:

 One approach to approximate curves is select a couple of points


and connect them with a line
 As the number of points increases the approximations becomes
accurate

 3 line, 6 line and 12 line


Segments polyline drawing
Scan Conversion - Circle
 Defining a Circle:
 A circle is a set of points that are all at a given distance r from a center
position ( xc , yc ).

 For any circle point ( x , y ), this distance relationship is expressed by the


Pythagorean theorem in Cartesian coordinates as:

 For the sake of calculating y from x the above equation can be rewritten:
Scan Conversion - Circle
 Properties of a Circle:
 Reduce computations by considering the symmetry of circles.

 The shape of the circle is similar in each quadrant.

 Determine the curve positions in the first quadrant, generate the circle
section in the second quadrant of the xy plane by noting that the two
circle sections are symmetric with respect to the y axis.

 And circle sections in the third and fourth quadrants can be obtained
from sections in the first and second quadrants by considering symmetry
about the x axis.
 We can take this one step further and note that there is also symmetry
between octants.
 Circle sections in adjacent octants within one quadrant are symmetric
with respect to the 45 line dividing the two octants.
Scan Conversion - Circle
 Properties of a Circle:
Scan Conversion - Circle
 Midpoint Circle Algorithm:

 Along a circle section from x = 0 to x = y in the 1st quadrant, the slope (m)
of the curve varies from 0 to -1.0

 i.e. we can take unit steps in the +ve x direction over the octant & use
decision parameter to determine which 2 possible positions is vertically
closer to the circle path

 Positions in the other 7 octants are obtained by symmetry


Scan Conversion - Circle
 Further on the Properties of Circles:
 Determining pixel positions along a circle circumference using
symmetry and either

or

 The polar coordinate computation still requires a good deal of


computation

 The Cartesian equation involves multiplications and square root


calculations, while the parametric equations contain multiplications
and trigonometric calculations
Scan Conversion - circle
 Further on the Properties of Circles:

 More efficient circle algorithms are based on incremental calculation of


decision parameters, as in the Bresenham line algorithm, which
involves only simple integer operations.

 Bresenham’s line algorithm for raster displays is adapted to circle


generation by setting up decision parameters for finding the closest
pixel to the circumference at each sampling step.

 The circle equation is nonlinear, so that


square root evaluations would be required to compute pixel distances
from a circular path.

 Bresenham’s circle algorithm avoids these square-root calculations


by comparing the squares of the pixel separation distances.
Scan Conversion - Circle
 Further on the Properties of Circles:

 The idea in this approach is to test the halfway position between two
pixels to determine if this midpoint is inside or outside the circle
boundary.

 This method is more easily applied to other conics; and for an integer
circle radius, the midpoint approach generates the same pixel
positions as the Bresenham’s circle algorithm.
Scan Conversion - Circle
 Midpoint Circle Algorithm: an incremental algorithm

 As in raster algorithm, we sample at unit intervals & determine the


closest pixel position to the specified circle path at each step

 For a given radius, r and screen center position (xc,yc) , we can set up
our algorithm to calculate pixel positions around a circle path centered at
the coordinate origin (0,0)

 Each calculated position (x, y) is moved to its proper screen position by


adding xc to x and yc to y
Scan Conversion - Circle
 Midpoint Circle Algorithm: an incremental algorithm

 Use eight-way symmetry and calculate the points for the top right
eighth of a circle, and then use symmetry to get the rest of the points

 Assume that we have just plotted point (xk, yk). The next point is a
choice between (xk+1, yk) and (xk+1, yk-1)

 Choose the point that is nearest to the actual circle So how do we make
this choice?
Scan Conversion - Circle
(xk, yk) (xk+1, yk)

(xk+1, yk-1)
Scan Conversion - Circle
 Midpoint Circle Algorithm: Given the center and radius of a circle
 Step 1: Assign the starting point coordinates (X0, Y0) as-

 X0 = 0
 Y0 = R
 Step 2:Calculate the value of initial decision parameter P0 as-

 P0 = 1 – R
 Step 3: Find the next point using the parameter Pk

 Case 1 (Pk < 0) Case 2: (Pk >= 0)


Xi+1 = Xi + 1 Xk+1 = Xk + 1
Yi+1 = Yi Yk+1 = Yk - 1
Pi+1 = Pi + 2 x Xi+1 + 1 Pi+1 = Pi + 2 x Xi+1+ 1 - 2Yi+1
Scan Conversion - Circle
 Midpoint Circle Algorithm: Given the center and radius of a circle

 Step 4: If the given center point (Xc, Yc) is not (0, 0), then do the following
and plot the point
Xplot = Xi + Xc
Yplot = Yi + Yc
 Step 5: Keep repeating Step-03 and Step-04 until Xplot >= Yplot

 Step 6: Step-05 generates all the points for one octant.

 To find the points for other seven octants, follow the eight symmetry
property of circle.
Scan Conversion - Circle
 Midpoint Circle Algorithm: Example

Given the center point coordinates (0, 0) and radius as 10, generate all the points
to form a circle.
 Step-01:
Assign the starting point coordinates (X0, Y0) as-
X0 = 0
Y0 = R = 10
 Step-02: Calculate the value of initial decision parameter P0 as-

P0 = 1 – R
P0 = 1 – 10
P0 = -9
Scan Conversion - Curves
 Midpoint Circle Algorithm: Example
 Step-03:
As Pinitial < 0, so case-01 is satisfied.
Thus,

Xk+1 = Xk + 1 = 0 + 1 = 1
Yk+1 = Yk = 10
Pk+1 = Pk + 2 x Xk+1 + 1 = -9 + (2 x 1) + 1 = -6
 Step-04:
This step is not applicable here as the given centre point coordinates is (0, 0).

 Step-05:
Step-03 is executed similarly until Xk+1 >= Yk+1 as follows-
Scan Conversion - Curves
 Midpoint Circle Algorithm: Example

Pk Pk+1 (Xk+1, Yk+1)


(0, 10)
-9 -6 (1, 10)
-6 -1 (2, 10)
-1 6 (3, 10)
6 -3 (4, 9)
-3 8 (5, 9)
8 5 (6, 8)

Algorithm calculates all the points of octant-1 and terminates.


Scan Conversion - Curves
 Midpoint Circle Algorithm: Example 2
Given the center point coordinates (4, -4) and radius as 10, generate all the
points to form a circle.
Class Activity
 Circle Algorithm: Given the center point coordinates (0, 0)
and radius as 10, generate all the points to form a circle.
No of segment decide by yourself

Write it as an algorithm to draw in openGL using given


formula below
X=xc+r*cos(angle)
Y= yc+r*sin(angle)
Scan Conversion - Curves
 Ellipse: Symmetry
 An ellipse only has a 2-way symmetry.

(-x,y) (x,y)
ry

rx
(-x,-y) (x,-y)
Scan Conversion - Rectangle
 Scan Converting a Rectangle:
 A rectangle whose sides are parallel to the coordinate axes may be
constructed if the locations of two vertices are known

 The remaining corner points are then derived

 lines would be drawn as follows: line (x1,y1) to (xl,y2), line (xl,y2) to {x1,y2}
line (x2,y2) to (x2,y1), and line (x2,y1) to (x1,y1).

X1,y1
X2,y1

X2,y2
X1,y2
Example
• Draw rectangle with (x1,y1)=(2,6) and (x2,y2)=(8,2)

• Find (X1,y2)=?
• Find (x2,y1)=?
Filling Polygons
 Fill area primitives:

 Fill area/Filled area: is an area that is filled with some solid color or
pattern.

 Application

 Describe surfaces of solid objects,


 Fill regions are usually planar surfaces, mainly polygons.

 For the present, assume that all fill areas are to be displayed with a
specified solid color.
Filling Polygons
Character Generation
 Bitmapped Fonts:

 Characters represented as a rectangular grid or bits (binary image)

 Simple and quick to draw because character is mapped onto pixel


positions in the frame buffer.

 However, it needs a lot of space.


 Each variation in size and style needs to be stored separately in a font
cache.

 Can still generate different size and styles from a single bitmap but the
results are poor quality because bitmap descriptions do not scale well.
Contd…
Two ways of representing characters:
 Bitmap (Raster)

00111000
01101100
11000110
11000110
11111110
11000110
11000110
00000000
Character Generation
 Bitmapped Fonts:
Character Generation
 Outline Fonts

 Characters described by straight lines and curves.

 Can generate different sizes and styles by manipulating curve


definition.

 Slow because they must be scan converted into the frame buffer
Contd…
 Stroke(outline)
 Defined using line/curve primitives
 Each character represented (stored) as a series of
line segments
 Takes longer time draw than bitmap fonts
 we can change the font, colour, and also line
width and line style
 width of these lines can be changed using
glLineWidth
 style using
glLineStipple
Character Generation

The letter B represented with an 8x8 bilevel bitmap


pattern and with an outline shape defined with
straight line and curve segments
Character Generation
 So, between these two representations:

 Bitmapped are faster to draw than outline fonts (simple to define and
display), needs more space (font cache)

 Outline produce higher quality characters (more costly, less space,


geometric transformations)

You might also like