CS3500 Computer Graphics Module: Scan Conversion: P. J. Narayanan
CS3500 Computer Graphics Module: Scan Conversion: P. J. Narayanan
Computer Graphics
Module: Scan Conversion
P. J. Narayanan
Spring 2004
CS3500
1
Current colour is defined/known. Frame buffer array is
initialized to the background colour.
Perform: frameBuf[i, j]
currentColour
The function WritePixel(x, y, colour) does the above.
If PointSize , assign the colour to a number of points
in the neighbourhood!
CS3500 January 27, 2005
4
Mid-Point Algorithm
Incremental Algorithm
Function DrawLine( , colour)
, slope
While ( )
, slope
EndWhile
WritePixel ( , colour)
EndFunction
Points to Consider
If abs(slope) , step through y values, adding inverse
NE
M
E
CS3500 January 27, 2005
9
Let slope
for below the line, for above.
NE if ; E if ; else any!
.
Therefore, .
Initial value:
January 27, 2005
Later, NE (17, 15), NE (18, 16), E (19, 16), NE (20, 17).
to
Example:
CS3500
11
For arbitrary centre, add to each point.
8-way symmetry: Only an eighth of the circle need to be
scan converted!
circle!
for inside circle, for outside.
SE if ; E if ; else any!
Therefore, .
Initial value:
Pseudocode
Function DrawCircle ( , colour)
CirclePoints ( , colour)
While ( )
if ( ) // East
else // South-East
CirclePoints ( , colour)
EndWhile
EndFunction
CS3500 January 27, 2005
15
Eliminate Multiplication?
Current selection is E: What are the new ’s?
if ( ) // East
+= += ++
else // South-East
+= += ++
CS3500 January 27, 2005
16
End of Class 14
Patterned Line
Represent the pattern as an array of booleans/bits, say,
16 pixels long.
Fill first half with 1 and rest with 0 for dashed lines.
if (pattern[i]) WritePixel(x, y)
where i is an index variable starting with 0 giving the
ordinal number (modulo 16) of the pixel from starting
point.
CS3500 January 31, 2005
18
Shared Points/Edges
It is common to have points common between two lines
and edges between two polygons.
Clipping
Often, many points map to outside the range in the
normalized 2D space.
Let’s get greedy: draw only the portion that is visible. That
is, clip the primitives to a clip-rectangle.
Clipping Points
Clip rectangle: to .
For :
Clipping Lines
segments!
.
should be within .
Cohen-Sutherland Algorithm
Identify line segments that can be accepted trivially.
For the rest, identify the segment that falls within the clip-
rectangle.
Region Outcodes
yM
ym
Overall Algorithm
Accept: code1 code0 == 0
ComputeCode( )
ComputeCode( )
Whole Algorithm
0 code0 ComputeCode(x0, y0), code1
1 if (! (code1 code0)) Accept and Return
2 if (code1 & code0) Reject and Return
3 code code1 ? code1 : code0
5 elsif (code & RIGHT) Intersect with line.
6 elsif (code & BOTTOM) Intersect with line.
7 elsif (code & LEFT) Intersect with line.
8 if (code == code1) Replace EndPoint1.
9 else Replace EndPoint0.
10 Goto step 1.
CS3500 February 12, 2004
29
Discussion
Simple logical operations to check intersections etc.
Clipping Polygons
Restrict drawing/filling of a polygon to the inside of the clip
rectangle.
An Example
An Example
Sutherland-Hodgman Algorithm
Input: A list of vertices . Implied edges from
to and from to .
Algorithm Detail
Process edges one by one and clip it to a line.
Compare the current edge with the current clip
line. Clip it to lie within the clip rectangle.
processed.
p
s s
p
s
p
p s
In Out In Out
In Out In Out
Function SuthHodg()
s last(inVertexList), p removeNext(inVertexList)
while (notEmpty(inVertexList))
if (inside(p, clipBoundary))
if (inside(s, clipBoundary))
addToList(p, outVertexList) // Case 1
else i intersect(s, p, clipBoundary) // Case 4
addToList(i, outVertexList)
s p, p removeNext(inVertexList)
Complete Algorithm
Invoke SuthHodg() 4 times for each clip edge as
clipBoundary.
The outVertexList after one run becomes the inVertexList
for the next.
Uses list data structures to implement polygons.
Function inside() determines if a point is in the inside of
the polygon. We can define it as “being on the left when
looking from first vertex to the second”.
Can be extended to clip to any convex polygonal region!
End of Class 15
Filled Rectangles
Write to all pixels within the rectangle.
for do
for do
WritePixel ( . colour)
EndFunction
Filled Polygons
For each scan line, identify spans of the polygon interior.
Strictly interior points only.
Parity Checking
D
C
E
A B
Special Concerns
Fill only strictly interior pixels: Fractions rounded up when
even parity, rounded down when odd.
Edge Coherence
If scan line intersects with an edge , it is likely that
also does. (Unless is the max vertex.)
When moving from to , the -coordinate goes from
to .
Store the integer part of , the numerator ( ) and the
denominator ( ) of the fraction separately.
numerator.
CS3500 February 03, 2004
46
increasing order of coordinate of the lower end point.
5 10 15 20 25 30 35
Insert edges from bucket of ET AET.
(They have and are sorted on .)
Compute next point on edge using coherence. (Increment
by 1 and numerator by , etc. Or vice versa)
y = 27 AET 30, 5, 0, 10 30, 14, −15, 5 32, 24, 15, 7 32, 34, 5, 22
Pattern Filling
A rectangular bit-map with the desired pattern can be
used to fill the interior with a pattern.
are row, col indices. Lower left corner at 0 and 0.
End of Class 16
End of Class 17