©zachary wartell, larry f. hodges scan conversion i revision 1.6 8/9/2007 copyright 2006, dr....
Post on 22-Dec-2015
220 views
TRANSCRIPT
©Zachary Wartell, Larry F. Hodges
Scan Conversion I
Revision 1.68/9/2007
Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved
©Zachary Wartell, Larry F. Hodges
Scan Conversion
• recall slide 23 and slide 24 from ITCS 4120-Introduction.ppt• scan conversion – algorithm with parameters:
CoordinateGeometryDescription D [IN], // !2D!
Raster R [IN/OUT] // typically the framebuffer
alter R by setting the appropriate pixel’s data values (RGB, etc) to represent pixels of D
• scan conversion of– 1D geometry
• line segments• curves (circle, ellipse, etc.)
– 2D geometry• axis-aligned rectangle• polygons
b
a
D=Line(a,b)
a
b
Pixels: P0,0, P1,1, P2,2, P3,2, …. P7,7
Scan-Conversion
©Zachary Wartell, Larry F. Hodges
(0,0) 1 2 3 4
1
2
3
4
0.5 1.5 2.5 3.5
0.5
1.5
2.5
3.5
0 1 2 3
1
2
3
0.5 1.5 2.5 3.5
0.5
1.5
2.5
3.5
0
VS
Center Lower-Left Corner
(2.5,3.5)
(2,3)
LLC: Area of pixel (x,y) is squarewith lower-left corner at (x,y)
(OpenGL)
Scan Convert a Point
(2,3)(2.5,3.5)
- location of pixel address
- location of “diagonal” point
Address-Coordinate Mapping Conventions
©Zachary Wartell, Larry F. Hodges
4
4(0,0)
(Lower-Left Corner)
4
40
(Center)
0
Scan Convert a Vertical/Horizontal Line
4
4
(Lower Left Corner)
Pixel Length = Coordinate Length 4 = 4
(0,0)
Line 1: ((0,0),(4,0))Line 2: ((4,0),(4,4))
Overlap No Overlap
Pixel Length ≠ Coordinate Length 5 ≠ 4
Gap
(OpenGL)
©Zachary Wartell, Larry F. Hodges
3
30
(Center)
0
Scan Convert an Axis-Aligned Rectangle
Pixel Width & Area ≠ Coord. Width & Area 4 ≠ 3, 16 ≠ 9
Rect 1: ((0,0),(3,3))Rect 2: ((3,0),(6,3))
(Lower Left Corner)
(OpenGL)
(Lower Left Corner)
3
3(0,0) 3(0,0)
3
Overlap
No Overlap
Pixel Width = Coord. Width Pixel Area = Coord. Area (3 = 3, 9 = 9)
©Zachary Wartell, Larry F. Hodges
Scan Convert Example (LLC/OpenGL)Rect 1: ((0,0),(4,4))
4
4(0,0)
Line 1: ((4,0),(4,4))
(0,0)
4
4
1) 2)
Line 2: ((4,4),(0,4))
(0,0)
4
4
3) Line 3: ((0,4),(0,0))
(0,0)
4
4
4) Line 4: ((0,0),(4,0))
(0,0)
4
4
5)
Overlap
Gap
©Zachary Wartell, Larry F. Hodges
So do we go with Center or Lower-Left Corner?
• It depends …– actual API’s specification (such as OpenGL) are
generally equivalent to the lower-left corner convention
• easy to mathematically specify• adjacent, filled primitives’ pixels don’t overlap
– but when first learning scan conversion algorithms its easier to use the center convention
©Zachary Wartell, Larry F. Hodges
Explicit (Slope-Intercept) Equation
Slope-Intercept Formula For A Line
Given a third point on the line: P = (X,Y)
Slope: (Y - Y1)/(X - X1)
= (Y2 - Y1)/(X2 - X1)
Solving For Y
Y = [(Y2-Y1)/(X2-X1)]X
+ [-(Y2-Y1)/(X2-X1)]X1 + Y1
orY = mx + b
Cartesian Coordinate System
2
4
1 2 3 4 5 6
3
5
6
1 P1 = (X1,Y1)
P2 = (X2,Y2)
P = (X,Y)
SLOPE =
RISE
RUN=
Y2-Y1X2-X1
L
©Zachary Wartell, Larry F. Hodges
Implicit Equation
Cartesian Coordinate System
2
4
1 2 3 4 5 6
3
5
6
1 P1 = (X1,Y1)
P2 = (X2,Y2)
F(X,Y) = 0
F(x,y): mx + b – y = 0
For arbitrary (x,y):
F(x,y) < 0 ↔ (x,y) above LF(x,y) = 0 ↔ (x,y) on LF(x,y) > 0 ↔ (x,y) below L
F(X,Y) < 0
F(X,Y) > 0
L
©Zachary Wartell, Larry F. Hodges
Parametric Form of a 2D Line Equation
Given points P1 = (X1, Y1) and P2 = (X2, Y2)
X = X1 + t(X2-X1)
Y = Y1 + t(Y2-Y1)
t is called the parameter. When
t = 0 we get (X1,Y1)
t = 1 we get (X2,Y2)
As 0 < t < 1 we get all the other points on the line segment between (X1,Y1) and (X2,Y2).
©Zachary Wartell, Larry F. Hodges
Other Helpful Formulas
Length of line segment between P1 and P2:
L = [ (X2-X1)2 + (Y2-Y1)2 ]1/2
Midpoint of a line segment between P1 and P3:
P2 = ( (X1+X3)/2 , (Y1+Y3)/2 )
Two lines are perpendicular iff
1) M1 = -1/M2
2) Cosine of the angle between them is 0.
©Zachary Wartell, Larry F. Hodges
Basic Line and Circle Algorithms
1. Must compute integer coordinates of pixels which lie on or near a line or circle.
2. Pixel level algorithms are invoked millions or billions of times when an image is created or modified.
3. Lines must create visually satisfactory images.• Lines should appear straight• Lines should terminate accurately• Lines should have constant density• Line density should be independent of line length and angle.• Lines sharing vertex should avoid/minimize overlap• Lines sharing vertex should avoid/minimize gaps!
4. Line algorithm should always be defined.
©Zachary Wartell, Larry F. Hodges
Simple DDA Line Algorithm
-Based on the parametric equation of a line: P = P1 + t(P2-P1)
Procedure DDA(X1,Y1,X2,Y2 :Integer);
Var Length, I :Integer;
X,Y,Xinc,Yinc :Real;
Begin
Length := ABS(X2 - X1);
If ABS(Y2 - Y1) > Length Then
Length := ABS(Y2-Y1);
Xinc := (X2 - X1)/Length;
Yinc := (Y2 - Y1)/Length;
X := X1;
Y := Y1;
For I := 0 To Length Do
Begin
Plot(Round(X), Round(Y));
X := X + Xinc;
Y := Y + Yinc
End {For}
End; {DDA}
avoidgaps
©Zachary Wartell, Larry F. Hodges
DDA Example
Compute which pixels should be turned on to represent the line from (6,9) to (11,12).
Length := Max of (ABS(11-6), ABS(12-9)) = 5
Xinc := 1
Yinc := 0.6
Values computed are:
(6,9),
(7,9.6),
(8,10.2),
(9,10.8),
(10,11.4),
(11,12) 6 7 8 9 10 11 12 13
9
10
11
12
13
DDA creates good lines but it is too time consuming due to the round function and long operations on floating point values.
©Zachary Wartell, Larry F. Hodges
The Midpoint Method
Assumptions: line with slope m between 0 and 1 (i.e. line lies in first octant).
The general formula for a line is y = mx + b where m is the slope of the line and b is the y-intercept.
The other octants algorithms are similar based on symmetry. Also we distinguish between x-major (|Δx|>|Δy|) and y-major (|Δy|>|Δx| lines.
+x-x
-y
+y
©Zachary Wartell, Larry F. Hodges
Bresenham Line (Midpoint formulation)
xk
yk
yk+1
xk+1 =xk+1
Pk+1
L: y=mx+b
Pk
?
Pk – current pixel at (xk,yk)Pk+1 – next pixel at (xk+1,yk+1)
©Zachary Wartell, Larry F. Hodges
From Pk:(xk,yk) do we step to Pα::(xk+1, yk) or Pβ:(xk+1, yk+1) ? -Is Pk+1(xk, yk+1) = Pα or Pβ
-Is Pά or Pβ closer to line L? dl > du ↔ Δd > 0 -Is mid-point Mk below or above line L? f(xk+1, yk + ½) > 0
Bresenham Line (Midpoint formulation)
xk
yk
yk+1
xk+1 =xk+1
Pβ
Pα
Mk
L: y=mx+b
Pk
du
dl
y=m(xk+1)+bΔd=dl-du
Distance=1
yk+1 = (?)
©Zachary Wartell, Larry F. Hodges
Goal: Is mid-point Mk below or above line L? f(xk+1, yk + ½) > 0
f(xk+1, yk + ½) = m (xk+1) + b – (yk + ½) = Δy / Δx (xk+1) + b – (yk + ½)
Insight #1: avoid fractions, so use: g(…) > 0, K > 0 → K * g(…) > 0
Let K = 2 Δ x and define “decision parameter” pk pk = 2 Δ x * f(…) = 2 Δy (xk+1) + 2 Δx b – Δx (2 yk + 1)
= 2 Δy xk - 2 Δx yk + [2 Δy + Δx (2 b -1)] (1)
Avoid Fractions
pk
yk
yk+1Pβ
Pα
MkPk
yk
yk+1Pβ
Pα
MkPk
©Zachary Wartell, Larry F. Hodges
Avoid Multiplications – Base Case
Insight #2: Formulate as only iterative additions instead of multiplications
#2.1 Base Case: p0 = 2 Δy x0 - 2 Δx y0 + [2 Δy + Δx (2 b -1)] = 2 Δy - Δx
p0= 2 Δy - Δx
xk=0
yk=0
yk+1
xk+1 =xk+1
Pβ
Pα
Mk=0
L: y=mx+b
P0
xk=0
yk=0
yk+1
xk+1 =xk+1
Pβ
Pα
Mk=0
L: y=mx+b
P0
©Zachary Wartell, Larry F. Hodges
Insight #2 : Formulate as iterative addition instead of multiplication
#2.2 Induction Case: Compute pk+1 from pk
pk = 2 Δy xk - 2 Δx yk + [2 Δy + Δx (2 b -1)] (2) from (1) [18]pk+1 = 2 Δy xk+1 - 2 Δx yk+1 + [2 Δy + Δx (2 b -1)] (3)
so pk+1 = pk + 2 Δy - 2 Δx (yk+1 – yk) - from (2) and (3)
yields 2 situations:
i) yk+1 – yk = 0 iff we step to Pα
ii) yk+1 – yk = 1 iff we step to Pβ
yk,yk+1
xk
Pk
L
Pα=Pk+1=(xk+1,yk+1)
Pβ
Mk
xk+1 =xk+1
Mk+1
pkpk+1
xk+2
yk+1
yk,yk+1
xk
Pk
L
Pα=Pk+1=(xk+1,yk+1)
Pβ
Mk
xk+1 =xk+1
Mk+1
pkpk+1
xk+2
yk+1
Avoid Multiplications – Induction Case
Figure 20: “yk+1 – yk =0” situation
©Zachary Wartell, Larry F. Hodges
yk
xk
Pk
L'
Pα
xk+1 =xk+1
pk
xk+2
yk+1=yk+1
Mk
Mk+1
pk+1
Pβ=Pk+1=(xk+1,yk+1)
yk
xk
Pk
L'
Pα
xk+1 =xk+1
pk
xk+2
yk+1=yk+1
Mk
Mk+1
pk+1
Pβ=Pk+1=(xk+1,yk+1)
Avoid Multiplications – Induction Case (Situation ii)
Figure 21: “yk+1 – yk =1” situation
Insight #2 : Formulate as iterative addition instead of multiplication
#2.2 Induction Case: Compute pk+1 from pk
pk = 2 Δy xk - 2 Δx yk + [2 Δy + Δx (2 b -1)] (2) from (1) [18]pk+1 = 2 Δy xk+1 - 2 Δx yk+1 + [2 Δy + Δx (2 b -1)] (3)
so pk+1 = pk + 2 Δy - 2 Δx (yk+1 – yk) - from (2) and (3)
yields 2 situations:
i) yk+1 – yk = 0 iff we step to Pα
ii) yk+1 – yk = 1 iff we step to Pβ
©Zachary Wartell, Larry F. Hodges
Bresenham’s Line Algorithm
Begin {Bresenham for lines with slope between 0 and 1}
Δx := ABS(xend - xstart);
Δy := ABS(yend - ystart);
p := 2*Δy - Δx;
If xstart > xend Then Begin
x := xend;
y := yend
End
Else Begin
x := xstart;
y := ystart
End;
For I := 0 to Δx Do Begin
Plot(x,y);
x := x + 1;
If p 0 Then Begin
y := y + 1;
p := p + 2*(Δy- Δx);
Else
p := p + 2*Δy End
End {For Loop}
End; {Bresenham}
©Zachary Wartell, Larry F. Hodges
In Class Exercise
3,5
9,9
Line: ((3,5),(9,9))
I p step x y
0
1
2
3
4
5
6
xstart = , ystart =xend = , yend =
Δx = , 2*(Δy- Δx) =Δy = , 2*Δy =
p0 = 2*Δy- Δx =
{major, major_minor}
©Zachary Wartell, Larry F. Hodges
In Class Exercise (Answer)
3,5
9,9
Line: ((3,5),(9,9))
xstart = 3 , ystart = 5xend = 9 , yend = 9
Δx = 6 , 2*(Δy- Δx) = -4Δy = 4 , 2*Δy = 8
p0 = 2*Δy- Δx = 2
I p step x y
0 2 major_minor 3 5
1 -2 major 4 6
2 6 major_minor 5 6
3 2 major_minor 6 7
4 -2 major 7 8
5 6 major_minor 8 8
6 2 major_minor 9 9
©Zachary Wartell, Larry F. Hodges
Generalizing to other Octant’s
Octant 1: (x-major)
Octant 2: (y-major)Octant 3: (y-major)
Octant 4: (x-major)
Octant 5: (x-major)
Octant 6: (y-major)
Octant 7: (y-major)
Octant 8: (x-major)
©Zachary Wartell, Larry F. Hodges
Generalizing to other Octant’s
octant major minor midpoint offset
major-minor step cond.
p0 Δpmajor Δpmajor_minor
1 x +=1 y +=1 (+1,+1/2) p > 0 2*Δy - Δx 2*Δy 2*(Δy- Δx)
2
… … … … … … … …
8
O1
O2O3
O4
O5
O6 O7
O8
O1
O2O3
O4
O5
O6 O7
O8
©Zachary Wartell, Larry F. Hodges
Simple Circle Algorithms
Since the equation for a circle on radius r centered at (0,0) is
x2 + y2 = r2,
an obvious choice is to plot
y = ±(r2 - x2)1/2
as -r ≤ x ≤ r.
This works, but is inefficient because of the
multiplications and square root
operations. It also creates large gaps in the
circle for values of x close to R.
A better approach, which is still inefficient but avoids the gaps is to plot
x = r cosø
y = r sinø
as ø takes on values between 0 and 360 degrees.
©Zachary Wartell, Larry F. Hodges
Circle Drawing Algorithm
We only need to calculate the values on the border of the circle in the first octant. The other values may be determined by symmetry. Assume a circle of radius r with center at (0,0).
Procedure Circle_Points(x,y :Integer);
Begin
Plot(x,y);
Plot(y,x);
Plot(y,-x);
Plot(x,-y);
Plot(-x,-y);
Plot(-y,-x);
Plot(-y,x);
Plot(-x,y)
End;
(a,b)
(b,a)
(a,-b)
(b,-a)
(-a,-b)
(-a,b)
(-b,-a)
(-b,a)
©Zachary Wartell, Larry F. Hodges
Fast Circles
Consider only the second octant of a circle of radius r centered on the origin. We begin by plotting point (0,r) and end when x > y.
The decision at each step is whether to choose the pixel directly right the current pixel or the pixel which is below and to the right (8-way stepping).
Assume Pk = (xk, yk) is the current pixel.Pα = (xk +1, yk) is the pixel directly rightPβ = (xk +1, yk-1) is the pixel right and below
y=x
x2 + y2 – r2 = 0
©Zachary Wartell, Larry F. Hodges
Fast Circles - The Decision VariableImplicit Equation: f(x,y) = x2 + y2 - r2
f(x,y) = 0 ↔ (x,y) on C f(x,y) < 0 ↔ (x,y) in C (“below”) f(x,y) > 0 ↔ (x,y) outside C (“above”)
Mk: f(xk + 1, yk - 1/2) = (xk + 1)2 + (yk - 1/2)2 - r2
pk = (xk + 1)2 + (yk - 1/2)2 - r2
What about pk+1? pk+1 = (xk+1 + 1)2 + (yk+1 – 1/2)2 - r2
= [(xk + 1) + 1]2 + (yk+1 – 1/2)2 - r2
= pk + 2(xk + 1) + (y2k+1 - y2
k) – (yk+1 - yk) + 1
yk+1 = yk iff we step to Pα
yk+1 = yk - 1 iff we step to Pβ
C: x2 + y2 - r2 =0
PαPk
Pβ
Mk
yk-1
yk
xk+1
(=xk+1)
xk
PαPk
Pβ
Mk
yk-1
yk
xk+1
(=xk+1)
xk
©Zachary Wartell, Larry F. Hodges
Fast Circles - Decision Variable (cont.)
The initial value of pk is
p0 = f(1,r - 1/2) = 1+ (r - 1/2)2 - r2
= 5/4 - r {1-r can be used if r is an integer}
When point Pα = (xk + 1, yk) is chosen then
pk+1 = pk + 2xk+1 + 1
When point Pβ = (xk+ 1, yk - 1) is chosen then
pk+1 = pk + 2xk+1 + 1 – 2yk+1
©Zachary Wartell, Larry F. Hodges
Fast Circle Algorithm
Begin {Circle}
x := 0;
y := r;
p := 1 - r;
Repeat
Circle_Points(x,y);
x := x + 1;
If p < 0 Then
p := p + 2*x + 1
Else Begin
y := y - 1;
p := p + 2*(x-y) + 1
End
Until x > y
End; {Circle}
y=x
x2 + y2 – r2 = 0
©Zachary Wartell, Larry F. Hodges
Revisions• 1.5
– typos– fixing math variable fonts– added answer slide to In Class Exercise– Dumped #34, #35, #36 Antialiasing – Solutions
• 1.6– Dumped slide 33 on ellipsoid rasterization, want to spend class time on other things