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

CS3500 Computer Graphics Module: Scan Conversion: P. J. Narayanan

This document discusses scan conversion and rasterization in computer graphics. Scan conversion is the process of taking vector graphics primitives defined by points and converting them into a raster (pixel-based) format for display. Key steps include rounding points to integer coordinates, using algorithms like incremental or midpoint line drawing to determine the pixels along a line, and writing color values to those pixels in the frame buffer. The midpoint line algorithm is presented as an example for drawing lines using only integer arithmetic.

Uploaded by

api-3799599
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

CS3500 Computer Graphics Module: Scan Conversion: P. J. Narayanan

This document discusses scan conversion and rasterization in computer graphics. Scan conversion is the process of taking vector graphics primitives defined by points and converting them into a raster (pixel-based) format for display. Key steps include rounding points to integer coordinates, using algorithms like incremental or midpoint line drawing to determine the pixels along a line, and writing color values to those pixels in the frame buffer. The midpoint line algorithm is presented as an example for drawing lines using only integer arithmetic.

Uploaded by

api-3799599
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

CS3500

Computer Graphics
Module: Scan Conversion
P. J. Narayanan
Spring 2004

CS3500
1

Graphics in Practice: Summary


Basic primitives: Points, Lines, Triangles/Polygons.

Each constructed fundamentally from points.

Points can be specified in different coordinate systems.


The pipeline of operations on a point is:

View View Clipping


Modelling
Orientation Mapping Perspective
Object World Camera Normalized Screen
Coords Coords Coords Coords Coords

CS3500 January 27, 2005


2

Scan Conversion or Rasterization


Primitives are defined using points, which have been
mapped to the screen coordinates.

In vector graphics, connect the points using a pen directly.

In Raster Graphics, we create a discretized image of the


whole screen onto the frame buffer first. The image is
scanned automatically onto the display periodically.

This step is called Scan Conversion or Rasterization.

CS3500 January 27, 2005


3

Scan Converting a Point


The 3D point has been transformed to its screen
coordinates .



Round the coordinates to frame buffer array indices .

 



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

Scan Converting a Line


Identify the grid-points that lie on the line and colour them.

Problem: Given two end-points on the grid, find the pixels


on the line connecting them.

Incremental algorithm or Digital Differential Analyzer


(DDA) algorithm.

Mid-Point Algorithm

CS3500 January 27, 2005


5

Line on an Integer Grid

CS3500 January 27, 2005


6

Incremental Algorithm
Function DrawLine( , colour)











, slope
 


 












While ( )


WritePixel ( round , colour)







, slope

EndWhile
WritePixel ( , colour)





EndFunction

CS3500 January 27, 2005


7

Points to Consider
If abs(slope) , step through y values, adding inverse

slopes to x at each step.


Simple algorithm, easy to implement.
Need floating point calculations (add, round), which are
expensive.
Can we do with integer arithmetic only?
Yes: Bresenham’s Algorithm
We will study a simplified version of it called the
Mid-Point Line Algorithm.
CS3500 January 27, 2005
8

Two Options at Each Step!

NE
M
E
CS3500 January 27, 2005
9

Mid-Point Line Algorithm


Line equation: .






Let slope



















for below the line, for above.










NE if ; E if ; else any!











.











Therefore, .













Initial value:
 











Similar analysis for other slopes. Eight cases in total.


CS3500 January 27, 2005
10


January 27, 2005


Later, NE (17, 15), NE (18, 16), E (19, 16), NE (20, 17).







to



 
 

 

    
 
  

 
 
  
  


  


Example:





    
 
   
  
 


 



 

  
  
    






 







     







      

CS3500
 
11

Scan Converting Circles


Need to consider only with centre at origin: .





For arbitrary centre, add to each point.





8-way symmetry: Only an eighth of the circle need to be
scan converted!

If on circle, , are also on the
















circle!

Easy way: , but floating point calculations!








CS3500 January 27, 2005


12

Back to Two Points??


E
M
SE

Choice between E and SE neighbours between the


vertical and the 45 degree lines.
CS3500 January 27, 2005
13

Mid-Point Circle Algorithm


Circle equation:










for inside circle, for outside.


















SE if ; E if ; else any!
























Therefore,  .









Initial value: 
 










CS3500 January 27, 2005


14

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?
























Current selection is SE: What are the new ’s?












 











if ( ) // East





+= += ++





else // South-East
+= += ++

 








CS3500 January 27, 2005
16

End of Class 14

CS3500 January 27, 2005


17

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.

Perform WritePixel(x, y) only if pattern bit is a 1.

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.

They will be scan converted twice. Not efficient.


Sometimes harmful.

Solution: Treat the intervals closed on the left and open


on the right. [ )&[ )








Thus, edges of polygons on the top and right boundaries


are not drawn.
CS3500 January 31, 2005
19

CS3500 January 31, 2005


20

Clipping
Often, many points map to outside the range in the
normalized 2D space.

Think of the FB as an infinite canvas, of which a small


rectangular portion is sent to the screen.

Let’s get greedy: draw only the portion that is visible. That
is, clip the primitives to a clip-rectangle.

Scissoring: Doing scan-conversion and clipping together.

CS3500 February 12, 2004


21

Clipping Points
Clip rectangle: to .











For :










Can use this to clip any primitives: Scan convert normally.


Check above condition before writing the pixel.

Simple, but perhaps we do more work than necessary.

Analytically clip to the rectangle, then scan convert.

CS3500 February 12, 2004


22

Clipping Lines

CS3500 February 12, 2004


23

Intersecting Line Segments


Infinite line equation: . Not good for line






segments!

.
 











Represent sides of clip-rectangles and lines for clipping


this way, with two parameters t and s. Solve for . Both



should be within .





CS3500 February 12, 2004


24

Cohen-Sutherland Algorithm
Identify line segments that can be accepted trivially.

Identify line segments that can be rejected trivially.

For the rest, identify the segment that falls within the clip-
rectangle.

For ease of this, assign outcodes to each of the 9


regions.

CS3500 February 12, 2004


25

Region Outcodes

Bits from left to right: 1001 1000 1010


yM







0001 0000 0010




ym

0101 0100 0110


xm xM

CS3500 February 12, 2004


26

Overall Algorithm
Accept: code1 code0 == 0

Reject: code1 & code0 != 0

Else, identify one of the boundaries crossed by the line


segment and clip it to the inside.

Do it in some order, say, TOP, RIGHT, BOTTOM , LEFT .

We also have: TOP = 1000, BOTTOM = 0100, LEFT =


0001, RIGHT = 0010
CS3500 February 12, 2004
27

Intersecting with Right/Top


if (code & RIGHT) // Intersects right boundary
// Adjust right boundary to the intersection with













ComputeCode( )



if (code & TOP) // Intersects top boundary


// Adjust top boundary to the intersection with

















ComputeCode( )



CS3500 February 12, 2004


28

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

4 if (code & TOP) Intersect with line.



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

4 to accept and 3 to reject.

CS3500 February 12, 2004


30

Discussion
Simple logical operations to check intersections etc.

Not efficient, as external intersections are not eliminated.

In the worst case, 3 intersections may be computed and


then the line segment could be rejectd.

4 intersections may be computed before accepting a line


segment.

CS3500 February 12, 2004


31

Clipping Polygons
Restrict drawing/filling of a polygon to the inside of the clip
rectangle.

A convex polygon remains convex after clipping.

A concave polygon can be clipped to multiple polygons.

Can perform by intersecting to the four clip edges in turn.

CS3500 February 03, 2004


32

An Example

CS3500 February 03, 2004


33

An Example

CS3500 February 03, 2004


34

Sutherland-Hodgman Algorithm
Input: A list of vertices . Implied edges from







to and from to .








Ouput: Another list of vertices giving the clipped polygon.


Method: Clip the entire polygon to the infinite line for each
clip edge in turn.
Four passes, the output of each is a partially clipped
polygon used as input to the next.

Post-processing to eliminate degenerate edges.

CS3500 February 03, 2004


35

Algorithm Detail
Process edges one by one and clip it to a line.

Start with the edge .








Compare the current edge with the current clip









line. Clip it to lie within the clip rectangle.

Repeat for the next edge . Till all edges are










processed.

When processing , treat as the in vertex


















and as the out vertex.




CS3500 February 03, 2004


36

At Each Step ...


in vertex: (already handled). out vertex: . Four cases:


p
s s
p
s
p

p s
In Out In Out

In Out In Out

CS3500 February 03, 2004


37

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), addToList(p, outVertexList)


elsif (inside(s, clipBoundary))
i intersect(s, p, clipBoundary) // Case 2

addToList(i, outVertexList)
s p, p removeNext(inVertexList)

CS3500 February 03, 2004


38

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!

CS3500 February 03, 2004


39

End of Class 15

CS3500 February 03, 2004


40

Filled Rectangles
Write to all pixels within the rectangle.

Function FilledRectangle ( , colour)








for do


for do



WritePixel ( . colour)



EndFunction

How about non-upright rectangles? General polygons?

CS3500 February 03, 2004


41

Filled Polygons
For each scan line, identify spans of the polygon interior.
Strictly interior points only.

For each scan line, the parity determines if we are inside


or ouside the polygon. Odd is inside, Even is outside.

Trick: End-points count towards parity enumeration only


if it is a min point.

Span extrema points and other information can be


computed during scan conversion. This information is
stored in a suitable data structure for the polygon.
CS3500 February 03, 2004
42

Parity Checking
D

C
E

A B

CS3500 February 03, 2004


43

Scan Converting Filled Polygons


Find intersections of each scan line with polygon edges.

Sort them in increasing -coordinates.

Use parity to find interior spans and fill them.

Most information can be computed during scan conversion.


A list of intersecting polygons stored for each scan line.

Use edge coherence for the computation otherwise.

CS3500 February 03, 2004


44

Special Concerns
Fill only strictly interior pixels: Fractions rounded up when
even parity, rounded down when odd.

Intersections at integer pixels: Treat interval closed on


left, open on right.

Intersections at vertices: Count only vertex for parity.

Horizontal edges: Do not count as 


!


CS3500 February 03, 2004


45

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.



For next scan line, add to numerator. If sum




goes , increment integer portion, subtract from





numerator.
CS3500 February 03, 2004
46

Filled Polygon Scan Conversion


Perform all of it together. Each scan line should not
intersected with each polygon edge!

Edges are known when polygon vertices are mapped to


screen coordinates.

Build up an edge table while that is done.

Scan conversion is performed in the order of scan lines.


Edge coherence can be used; an active edge table can
keep track of which edges matter for the current scan line.
CS3500 February 03, 2004
47

Edge Table for a Polygon


Construct a bucket-sorted table of edges, sorted into
buckets of for the edge.


Each bucket contains a list of edges with , in the





increasing order of coordinate of the lower end point.

Each edge is represented by its for the edge, of the




lower point, and the slope as a rational number.

This is the basis for constructing the Active Edge Table to


compute the spans.
CS3500 February 03, 2004
48

Polygon and Edge Table


35
30 25 30, 20, −15, 5 32, 20, 15, 7
25 ...
20 20 30, 5, 0, 10
15 ...
10 10 32, 30, 5, 22
...
5
5 20, 15, −10, 15 10, 15, 15, 5

5 10 15 20 25 30 35

CS3500 February 03, 2004


49

Active Edge Tables


Start with the lowest value and an empty AET.


Insert edges from bucket of ET AET.


(They have and are sorted on .)



Between pairs of AET entries lie spans. Fill them.


Remove edges from AET where is the point.





Compute next point on edge using coherence. (Increment
by 1 and numerator by , etc. Or vice versa)



Continue above 4 steps till ET and AET are empty.


CS3500 February 03, 2004
50

Polygon and Edge Table

y = 15 AET 20, 9, −10, 15 32, 31, 5, 22

y = 22 AET 30, 5, 0, 10 32, 32, 5, 22

y = 27 AET 30, 5, 0, 10 30, 14, −15, 5 32, 24, 15, 7 32, 34, 5, 22

CS3500 February 03, 2004


51

Pattern Filling
A rectangular bit-map with the desired pattern can be
used to fill the interior with a pattern.

If pattern , draw pixel, else ignore.


 








are row, col indices. Lower left corner at 0 and 0.




are the pattern height and width.





CS3500 February 03, 2004


52

Scan Conversion: Summary


Filling the frame buffer given 2D primitives.

Convert an analytical description of the basic primitives


into pixels on an integer grid in the frame buffer.

Lines, Polygons, Circles, etc. Filled and unfilled


primitives.

Efficient algorithms required since scan conversion is


done repeatedly.

2D Scan Conversion is all, even for 3D graphics.


CS3500 February 03, 2004
53

Scan Conversion: Summary


High level primitives (point, line, polygon) map to window
coordinates using transformations.
Creating the display image on the Frame Buffer is
important. Needs to be done efficiently.
Clipping before filling FB to eliminate futile effort.
After clipping, line remains line, polygons can become
polygons of greater number of sides, etc.
General polygon algorithm for clipping and scan
conversion are necessary.
CS3500 February 03, 2004
54

End of Class 16

CS3500 February 03, 2004


55

End of Class 17

CS3500 February 03, 2004

You might also like