You are on page 1of 43

Graphics System

Processor Input Devices Memory Frame Buffer

Output Display

Array of pixels Resolution: # of pixels Depth: # of bits/pixel

Input Devices
Physical Devices: Keyboard, Mouse, Tablet, etc. Logical Devices: High level interface with the user program Locator Valuator Pick Choice String Tablet Dial Light Pen Function Keys Keyboard

3D Input Devices: Spaceball, Data glove

Input Devices

Reading Assignment:

Foley van Dam Chapter 4 Hearn & Baker Chapter 2

Raster Graphics
Drawing Algorithms
Rasterization Pixelization Scan Conversion Continuous Discrete

Raster Graphics
Line Drawing Algorithms
Line Equation:
(x2,y2) y (x1,y1) x DDA (Digital Differential Analyzer)
y = mx + B
m= dy y 2 - y 1 y = = dx x 2 - x 1 x

Raster Graphics
Line Drawing Algorithms
DDA (Digital Differential Analyzer)
(x2,y2) y (x1,y1) x
y i = mx i + B y i +1 = mx i +1 + B y i +1 = m ( x i + x ) + B

Raster Graphics
Line Drawing Algorithms
DDA (Digital Differential Analyzer)
(x2,y2) y (x1,y1) x
if x = 1 x i +1 = x i + 1 y i +1 = y i + m

Raster Graphics
Line Drawing Algorithms
DDA (Digital Differential Analyzer)
y = y1 for (x = x1 ; x x2 ; x + +) { Writepi xel(x, round (y)); y+ = m; }

(xi, round(yi) )

Raster Graphics
Line Drawing Algorithms
DDA (Digital Differential Analyzer)
m <= 1 m>1

Exchange the role of x and y

Raster Graphics
Line Drawing Algorithms
Midpoint Line Algorithm
NE Line

Find on what side of the line the mid point is: If below then NE is closer to line If above then E is closer to line

above

M E
below

Raster Graphics
Midpoint Line Algorithm
M M
F(x, y) = 0 : (x, y) on line F(x, y) > 0 : (x, y) below line F(x, y) < 0 : (x, y) above line

NE M P(xp,yp) E

Line

Raster Graphics
Midpoint Line Algorithm
M M Line
Let 0 <= m < 1 Consider line dy y = x +B dx F ( x , y ) = ax + by + c = 0 xdy - ydx + Bdx = 0 (a = dy, b = - dx, c = Bdx)

NE M P(xp,yp) E

Raster Graphics
Midpoint Line Algorithm
M M
1 F (M ) = F ( x p + 1, y p + ) = d 2 1 d = a( x p + 1) + b( y p + ) + c 2 if d > 0 M is below the line, choose NE if d < 0 M is above the line, choose E

NE M P(xp,yp) E

Line

Raster Graphics
Midpoint Line Algorithm
NE M P(xp,yp) E M Line M
When E : 1 d new = F (M ' ) = F ( x p + 2, y p + ) 2 1 d new = a( x p + 2) + b( y p + ) + c 2 1 dold = a( x p + 1) + b( y p + ) + c 2 E = d new - dold = a = dy

Raster Graphics
Midpoint Line Algorithm
NE M P(xp,yp) E M Line M
When NE : 3 d new = F (M ' ' ) = F ( x p + 2, y p + ) 2 3 d new = a( x p + 2) + b( y p + ) + c 2 1 dold = a( x p + 1) + b( y p + ) + c 2 NE = d new - dold = a + b = dy - dx

Raster Graphics
Midpoint Line Algorithm
At start : 1 1 d start = F ( x 0 + 1, y 0 + ) = a( x 0 + 1) + b( y 0 + ) + c 2 2 b d start = ax 0 + by 0 + c + a + 2 b dx d start = a + = dy (division) 2 2 F ( x , y ) = 2(ax + by + c )

Raster Graphics
Midpoint Line Algorithm
dx = x2 - x1; dy = y2 - y1; d = 2dy - dx; E = 2dy; NE = 2(dy - dx); x = x1; y = y1; Writepixel(x, y); While (x < x2) if d 0 d+ = E; x+ = 1; else d+ = NE; x+ = 1; y+ = 1; end Writepixel(x, y); end While

Raster Graphics
Midpoint Line Algorithm
NE
above

Line

M E

below

F(x,y) = ax + by +c

Raster Graphics
Midpoint Line Algorithm
NE
above

Line

NE M

M
Line

M E

below

P(xp,yp) E

F(x,y) = ax + by +c

Raster Graphics
Midpoint Circle Algorithm
(x,y) x 2 + y 2 = R2

Raster Graphics
Midpoint Circle Algorithm
(x,y) x 2 + y 2 = R2

Raster Graphics
Midpoint Circle Algorithm
(x,y)

Raster Graphics
Midpoint Circle Algorithm
(-x,y) (-y,x) (-y,-x) (-x,-y) (x,y) (y,x) (y,-x) (x,-y)

8-way symmetry: drawing in one octant is enough

Raster Graphics
Midpoint Circle Algorithm
Consider II octant
P(xp, yp) E M SE M M

F (x , y ) = x 2 + y 2 R2 For a given point (x, y) F(x, y) = 0 : (x, y) on circle F(x, y) > 0 : (x, y) outside circle F(x, y) < 0 : (x, y) inside circle

Raster Graphics
Midpoint Circle Algorithm
Consider II octant
P(xp, yp) E M SE M M

Evaluate F(M) If < 0 (M inside circle) Choose E If > 0 (M outside circle) Choose SE

Raster Graphics
Midpoint Circle Algorithm
Consider II octant
P(xp, yp) E M SE M M

1 dold = F (M ) = F ( x p + 1, y p ) 2 1 = ( x p + 1)2 + ( y p )2 R 2 2 When E (dold < 0) 1 d new = F (M ' ) = F ( x p + 2, y p ) 2 1 = ( x p + 2)2 + ( y p )2 R 2 2 E = d new d old = 2 x p + 3

Raster Graphics
Midpoint Circle Algorithm
Consider II octant
P(xp, yp) E M SE M M

When SE (dold 0) 3 d new = F (M ' ' ) = F ( x p + 2, y p ) 2 3 = ( x p + 2 )2 + ( y p )2 R 2 2 SE = d new d old = 2 x p 2 y p + 5

Raster Graphics
Midpoint Circle Algorithm
Consider II octant
P(xp, yp) E M SE M M

Initial Condition (0, R) start point, 1 next mid point = (1, R - ) 2 1 5 F (1, R ) = R 2 4

Raster Graphics
Midpoint Circle Algorithm
Consider II octant
5 x = 0; y = R;d = - R; 4 Writepixel(x, y); While (y > x) do if d < 0 d+ = 2x + 3; x+ = 1; else d+ = 2x - 2y + 5; x+ = 1; y- = 1; end Writepixel(x, y); end While

Raster Graphics
Ellipse Drawing Algorithm

E SE

x2
2

+ 2 =1 a b

y2

SE

Clipping
Extraction of data/primitives inside a region of interest window => Discard (parts of ) primitives outside window. Point Clipping: Remove points outside window. A point is either entirely inside the window or not
(xR, yT)
Q P

(xL, yB)

xL <= x <= xR yB <= y <= yT Q is inside P is outside

Clipping
Line Clipping: Remove portion of line segment outside window Can we use point clipping for the end points?

1 2

Point clipping works

Clipping
Line Clipping: Remove portion of line segment outside window How about these lines?
3 5

Point clipping does not work

Clipping
Cohen and Sutherland

Clipping
Cohen and Sutherland
4 bit code to indicate the zone of end points of line with respect to window
1xxx

xxx1

0000

xx1x

x1xx

Clipping
Cohen and Sutherland
4 bit code to indicate the zone of end points of line with respect to window
1001 1000 1010

0001

0000

0010

0101

0100

0110

Clipping
Cohen and Sutherland
Trivially accept case line is totally visible if both ends of the line have outcode as 0000

Trivially accept

Clipping
Cohen and Sutherland
Trivially reject case line is totally invisible logical AND of the two end points outcodes

Trivially reject

Clipping
Cohen and Sutherland
If not trivially reject and accept case line is potentially visible

Potentially visible

Clipping
Cohen and Sutherland
If potentially visible
subdivide into segments and apply trivial acceptance and rejection test segments by intersection with window edges edges in any order but consistent (e.g., top-bottom, right-left)
D B A B A

Result

Clipping
Cohen and Sutherland
simple, still popular limited to rectangular region extension to 3D clipping using 3D orthographic view volume is straightforward

You might also like