You are on page 1of 30

Output Primitives (Rasterization)

Basic raster graphics algorithms for drawing 2D primitives (from a graphic package implementors point of view)
(Foley-van Dam: Computer Graphics, Chapter 3)

Primitives
Describe the component parts of the scene (trees, walls, atoms, cars etc.) to be displayed. (Graphics output)Primitives: Functions in a graphics package that we use to describe the picture components Geometric primitives: Output primitives describing the geometry of objects. The simplest one: points, straight-line segments Output primitives are projected to a 2D plane, corresponding to the display area of an output device, and scan-converted into integer pixel positions.
2

Clipping
Clipping before scan conversion
Advantage: the scan converter must deal with only the clipped version of the primitive, not with the original (possibly much larger) one. Scissoring: scan convert the entire primitive but to write only the visible pixels in the clip-rectangle region of the canvas (checking each pixel s coordinates against the (x, y) bounds of the rectangle). E.g. Lines, rectangles, and polygons

Clipping after scan converting the entire collection of primitives into a temporary canvas
Wasteful, but easy to implement E.g. Text
window
3

Scan Converting Lines


Compute the coordinates of the pixels that lie on or near an ideal, infinitely thin straight line imposed on a 2D raster grid.

A line from (x1,y1) to (x2,y2) a series of pixels

[Criteria] uniform, user-selected density straight lines should appear straight line end-points should be constrained line drawing algorithms should be fast
4

Why Study Scan Conversion Algorithms?


Every high-end graphics card support this. You will never have to write these routines yourself, unless you become a graphics hardware designer. So why do we learn this stuff? Maybe you will become a graphics hardware designer But seriously, the same basic tricks underlie lots of algorithms: 3-D shaded polygons, texture mapping The algorithms introduced are discussed at a general, machineindependent level, so they apply to both software and hardware implementations.

Scan Converting Lines


Digital Differential Analyzer (DDA)
Basic incremental algorithm Each point is generated from the previous point in a simple fashion Slope of a line m = y / x y x
The DDA is a mechanical device that solves differential equations by numerical methods.
6

Digital Differential Analyzer (DDA)


Idea
1. 2.

3.

Go to starting point Increment x and y values by constants proportional to x and y such that one of them is 1. Round to the closest raster position

Drawbacks
Rounding to an integer takes time Floating-point operations (the slope is a fraction)
7

Digital Differential Analyzer (DDA)

yi+1 = mxi+1 + B = m (xi + x) + B = yi + m x If x = 1 then yi+1 = yi + m A unit change in x changes the value of y by m.
8

Digital Differential Analyzer (DDA)

PutPixel
If |m| > 1, a step in x creates a step in y that is greater than 1. Thus, we must reverse the roles of x and y by assigning a unit step to y and incrementing x by x = y / m = 1 /m. The checking for special cases (horizontal, vertical or diagonal lines) is omitted.
9

Midpoint Line Algorithm (Bresenham's Line Algorithm)


Assume a line from (x0,y0) to (x1,y1) that 0 < slope < 1 and x0 < x1. Suppose that we have just finished drawing a pixel P = (xp, yp) and we are interested in figuring out which pixel to draw next. This algorithm uses only integer arithmetic.

10

Midpoint Line Algorithm (Bresenham's Line Algorithm)


Assume that we have just selected the pixel P and now must choose between the pixel one increment to the right, or the pixel one increment to the right and one increment up.

Bresenham:
If distance (NE, Q) > distance (E, Q) then select E = (xp+1, yp) else select NE = (xp+1, yp+1) P (xp, yp)

NE Q M E

Midpoint Line: We observe on which side of the line the midpoint M lies.
11

Midpoint Line Algorithm (Bresenham's Line Algorithm)


How to calculate on which side of the line the midpoint lies? A line equation in the implicit form F(x, y) = ax + by + c = 0, where ab 0 Using y = (y / x) x + B (x y = y x + x B), where a = y, b = -x, c = Bx. F(x,y) = yx - xy + Bx = 0 Let's use an equivalent representation F(x,y) = 2ax + 2by + 2c = 0.

12

Midpoint Line Algorithm (Bresenham's Line Algorithm)


Making slope assumptions, observe that b < 0, and this implies:
F(x, y) < 0 for points above the line F(x, y) > 0 for points below the line

To apply the midpoint criterion, we need only to compute F(M) = F(xp+1, yp+) and to test its sign.

13

Midpoint Line Algorithm (Bresenham's Line Algorithm)


To determine which one to pick up, we define a decision variable D = F(xp+1, yp+) D = 2a(xp+1) + 2b(yp+) + 2c = 2axp + 2byp + (2a + b + 2c)
NE

If D > 0 then M is below the line, so select NE, otherwise select E.


P

Q M E
14

Midpoint Line Algorithm (Bresenham's Line Algorithm)


How to compute D incrementally? Suppose we know the current D value (2a(xp+1) + 2b(yp+) + 2c), and we want to determine the next D. If we decide on going to E next, Dnew = F(xp + 2, yp + ) = 2a(xp + 2) + 2b(yp + ) + 2c = D + 2a = D + 2y If we decide on going to NE next, Dnew = F(xp + 2, yp + 1 + ) = 2a(xp + 2) + 2b(yp + 1 + ) + 2c = D + 2(a + b) = D + 2(y - x).
P

NE Q M E

15

Midpoint Line Algorithm (Bresenham's Line Algorithm)


Since we start at (x0,y0), the initial value of D can be calculated by
Dinit = F(x0 + 1, y0 + ) = (2ax0 + 2by0 + 2c) + (2a + b) = F(x0, y0) + 2a + b = 2y - x

Advantages
need to add integers and multiply by 2 (which can be done by shift operations) incremental algorithm

16

Midpoint Line Algorithm (Bresenham's Line Algorithm)


At each step, the algorithm chooses between 2 pixels based on the sign of the decision variable calculated in the previous iteration; then it updates the decision variable by adding either E or NE to the old value depending on the choice of pixel.
{a = y1 - y2; b = x1 - x2; deltaE = 2*a; deltaNE=2*(a+b); d=2*a+b; while(x < x1) { if (d>=0) {d=d+deltaE; x+=1; } else {d=d+deltaNE; x+=1; y+=1; } putpixel(x, y, color); } }

17

Midpoint Line Algorithm- Example


Line endpoints: (x0, y0) = (5,8); (x1, y1) = (9,11) x = 4; y = 3 Dinit = 2y x = 2 > 0 select NE Dnew = D + 2(y - x) = 0 Select E Dnew = D + 2y = 0 + 6 = 6 Select NE

13 12 11 10 9 8 7 6 4 5 6 7 8 9 10 11

18

Scan Converting Lines (issues)


Endpoint order
S01 is a set of pixels that lie on the line from P0 to P1 S10 is a set of pixels that lie on the line from P1 to P0 S01 should be the same as S10 PE QW W Switching the endpoints does not work NE
M

Outline primitives composed of lines

SW

Care must be taken to draw shared vertices of polylines only once

19

Scan Converting Lines (issues)


Varying intensity of a line as a function of slope
The same number of pixels can be used to draw the two lines although their length are different. If the intensity of each pixel is I, then the intensity per unit length of line A is I, whereas for line B is only I/2. On bilevel display : no cure On an n-bits-per-pixel system: compensate by setting intensity to be a function of the lines slope. needs antialiasing treating the line as a thin rectangle
20

Scan Converting Lines (issues)


Starting at the edge of a clip rectangle
Starting point is not the intersection point of the line with clipping edge Clipped line may have a different slope

21

Scan Converting Circles


Otherwise translate it to the origin and then scan convert with pixels written with the appropriate offset.

Centered at the origin: x2 + y2 = R2

To draw a quarter circle using the explicit form: increment x from 0 to R in unit steps solving for +y at each step. Inefficient! square-root operations Large gaps for values of x close to R.

22

Scan Converting Circles


Eight-way symmetry
If the point (x, y) is on the circle, then we can trivially compute 7 other points on the circle.

We only consider 45 of a circle


23

Midpoint Circle Algorithm


The second octant is considered. Suppose that we have just finished drawing a pixel (xp, yp) and we are interested in figuring out which pixel to draw next.

24

Midpoint Circle Algorithm


F(x,y) = x2 + y2 - R2
= 0 on the circle > 0 outside the circle < 0 inside the circle

If F(midpoint between E and SE) > 0 then


select SE = (xp+1,yp-1)

else
select E = (xp+1, yp);

25

Midpoint Circle Algorithm


Decision variable dold = F(xp+1, yp-)
= (xp+1)2 + (yp-)2 - R2 If dold < 0, select E. dnew = F(xp+2, yp-) = dold + (2xp + 3) If dold 0, select SE. dnew = F(xp+2, yp--1) = dold + (2xp - 2yp + 5)

We have to calculate new d based on the point of evaluation P=(xp, yp), but this is not expensive computationally.

26

Midpoint Circle Algorithm


Since we start at (0,R), the initial value of d can be calculated by
dinit = F(1, R - ) = 5/4 R

By substituting d - 1/4 by h, we can get the integer midpoint circle scan-conversion algorithm.

27

Midpoint Circle Algorithm


void circlen_(int u,int v, int r) { int xp,yp,d; xp= 0; yp= r; d= 1-r; CirclePointsn(u,v,xp,yp); while(yp > xp) { if(d < 0) { d= d+xp*2+3; xp++; } else { d= d+(xp-yp)*2+5; xp++; yp--; } CirclePointsn(u,v,xp,yp); } } //starting values

// E

//SE

28

Midpoint Circle Algorithm


void CirclePointsn(int u,int v,int xp,int yp) { putpixel(u+xp,v+yp,color); putpixel(u+xp,v-yp,color; putpixel(u-xp,v-yp,color; putpixel(u-xp,v+yp,color); putpixel(u+yp,v+xp,color); putpixel(u+yp,v-xp,color); putpixel(u-yp,v-xp,color); putpixel(u-yp,v+xp,color); }

29

Scan Converting Ellipses


F(x,y) = b2x2 + a2y2 -a2b2 Divide the quadrant into two regions; the boundary of two regions is the point at which the curve has a slope of -1. And then apply any midpoint algorithm.

30

You might also like