computergraphics

19
Computer Graphics Spring 2007, #1 2D Graphical Primitives

Upload: rajyalakshmi-jammalamadaka

Post on 01-Feb-2016

215 views

Category:

Documents


0 download

DESCRIPTION

material for CG

TRANSCRIPT

Page 1: ComputerGraphics

Computer Graphics

Spring 2007, #12D Graphical Primitives

Page 2: ComputerGraphics

Contents

• Pixel• Straight line• Staright line drawing algorithms

– Digital Differential Analyzer DDA

– Bresenham

• Second order curves– midpoint algorithm

Page 3: ComputerGraphics

Pixel

• A pixel is the basic entity in computer graphics• Represents a 0-dimensional geometrical point at

position (x,y) with zero length, zero area, zero volume, ...

• A pixel has several properties– position (xint,yint) = (round(x),round(y))

where round(x) = (int)(x+0.5)– colours (R,G,B)– intensity– non-zero extension

Page 4: ComputerGraphics

Pixels and coordinate systems

0

0 1 2 3 4 5 6 7 8

1234567

X

Y

Pixels (1,5), (1.8,7.1), etc.

Page 5: ComputerGraphics

Straight Line

• A line represents a 1-dimensional geometrical object with non-zero length, zero area, zero volume, ...

• A straight line has two end points p1, p2 and a straight line connecting the end points

• Geometrically defined from the parametric representation

s = p1 + t∗(p2-p1) , t ∈ [0,1]or an equation with constants a,b,c and endpoints:

a∗x + b∗y + c = 0

Page 6: ComputerGraphics

From Straight Lines to Pixels

• How should we draw a straight line using pixels?

• Requirements:– should be straight!– fixed width (constant intensity)– no jags (steps)– well defined starting and end point– same intensity independent of angle w.r.t. coordinate

axes– drawn quickly: integer arithmetic, additions and

subtractions, bit shifts

Page 7: ComputerGraphics

From Staright Lines to Pixels

0

0 1 2 3 4 5 6 7 8

1234567

X

Y

3∗Y - 2∗X - 3 = 0, from (0,1) to (6,5)

Page 8: ComputerGraphics

DDA: Digital Differential Analyzer

• Basic idea: Calculate the (x,y)-coordinates of the pixels for the line using the parametric representation for the line and scan over t.( x(t) , y(t) ) = (p1x,p1y) + t(p2x-p1x,p2y-p1y)

x(t) = x1 + t∗∆Xy(t) = y1 + t∗∆Y = y1 + m∗(t∗∆X)where m = ∆Y/∆X

Page 9: ComputerGraphics

DDA: Digital Differential Analyzer

• Use suitable incremental values for t, calculate (x(t),y(t)) and round off to ints

• Simplified version:– if |m| ≤ 1 then increment x → x + 1, that is, scan over tn such that

tn∗∆X = 0,1,2, integers; and y is incremented with m– if |m| > 1 then increment y → y + 1, that is, scan over tn such

that m∗tn∗∆X = 0,1,2, integers, and x is incremented by 1/m.

• Problems:– floating point division at startup – floating point additions when incrementing– rounding at every increment

Page 10: ComputerGraphics

Bresenham algorithm

• For |m| < 1, x-increments are integer valued• Try to eliminate rounding off floating point values

by detecting when the rounding off gives a value that is different from the previous round off value

• Example: Will the next point be (2,2) or (2,3)?

0

0 1 2 3 4 5

12345

X

Y

Page 11: ComputerGraphics

Bresenham algorithm

• x increases in integer steps: x = round(x)• y has rounding ”errors” d = round(y)-y

d ∈ (-0.5, 0.5]• Denote previously drawn pixel by

(x, y’) = (round(x),round(y)) = (x,y+d)• Next point will be (x+1,y+m) which rounded off

yieldsround(x+1) = x+1round(y+m) = y’ if d+m < 0.5

y’+1 if d+m ≥ 0.5

Page 12: ComputerGraphics

Bresenham algorithm

• Bresenham: Enough to monitor the sign of d + m - 0.5 ≡ s

• At the starting point d = 0 ⇒ s = m - 0.5• Algorithm:

s = m – 0.5;loop {

s = s + m;if ( s > 0.0 )

{ s = s - 1.0; y’= y’+1;}

}

Page 13: ComputerGraphics

Bresenham algorithm

• Computational trick: switch to integer arithmetic by scaling everything with 2∗∆X

• Scaled start up value s∗2∗∆X = p = 2∆Y –∆Xloop {

p = p + 2∗∆Y;if ( p > 0 )

{p = p – 2∗∆X;y’ = y’ + 1;}

}

Page 14: ComputerGraphics

Bresenham algorithm

• Advantages:– no floating point division

– no floating point numbers– only integer multiplication by 2, i.e. shift left!

• Note:– if ∆X < 0 switch p1 ⇔ p2 or reprogram with

diminishing x and y (subtract m or 2∗∆Y)

– if |m| > 1 switch the roles of x ⇔ y

Page 15: ComputerGraphics

2nd Order Curves

• Curves which are algebraically of second order (circle, ellipse, parabola, some splines) have special algorithms

• Example: for circles use octant symmetry-x y

-y x

-y -x

-x -y x -y

y -x

y x

x y

10

0 1 2 3 4 5

1112131415

X

Y

Page 16: ComputerGraphics

Midpoint algorithm for circles

• Assume that the circle in centered at the origin (0,0)

• Let (xi,yi) be the last drawn pixel.• Now study (xi+1,yi+1) = (xi+1,yi+1) and

decide whether (xi+1,yi) or (xi+1,yi -1) will be chosen.

• Circle equation: F(x,y) = x2 + y2 - R2 = 0• For any(!) point inside the circle F(x,y) < 0,

for any point outside the circle F(x,y) > 0

Page 17: ComputerGraphics

Midpoint algorithm for circles

• Introduce a decision variable pi to be evaluated at (xi,yi) to decide which is the next pixel.

• pi evaluates F(x,y) at the midpoint between yiand yi-1 for xi+1=xi+1:

pi = F(xi+1,yi-0.5) = (xi+1)2 + (yi-0.5)2 –R2

• if pi < 0 then the midpoint is inside the circle, and we choose the pixel above the midpoint: yi+1 = yi

• if pi > 0 then the midpoint is outside the circle, and we choose the pixel below the midpoint: yi+1 = yi-1

Page 18: ComputerGraphics

Midpoint algorithm for circles

• Really neat trick: Calculate pi+1 using values already calculated at pi:pi+1 = F(xi+1+1,yi+1-0.5)

= (xi+1+1)2 + (yi+1-0.5)2 – R2

pi+1 – pi = 2(xi+1)+1+(yi+1)2 – (yi)2 – (yi+1-yi)

Page 19: ComputerGraphics

Midpoint algorithm for circles

• if pi < 0 then yi+1 = yi, and pi+1 = pi + 2∗xi+1 +1

• if pi > 0 then yi+1 = yi -1, andpi+1 = pi + 2∗xi+1 +1 - 2∗yi+1

• We need only p0 from the very first pixel drawn at (0,R):

p0 = F(1,R-0.5) = 1.25 –R• Note 1: pi is incremented/decremented by

integer values, hence round off p0 = round(p0); if R integer then start with p0 = 1-R

• Note 2: pi is incremented with 2∗xi and and 2∗yi, hence 2∗xi+1 = 2∗xi + 2 , 2∗yi+1 = 2∗yi – 2 or 2∗yi