Post on 24-Feb-2016
description
transcript
+
CPCS 391 Computer Graphics 1
Instructor: Dr. Sahar ShabanahLecture 3
+Scan conversion Algorithms
Primitives and Attributes Why Scan Conversion? Algorithms for Scan Conversion: Lines Circles Ellipses Filling Polygons
2
+Scan Conversion Problem
To represent a perfect image as a bitmapped image.
3
+Line Drawing Algorithms Lines are used a lot - want to get them right. Lines should appear straight, not jagged. Horizontal, vertical and diagonal easy, others difficult Lines should terminate accurately. Lines should have constant density. Line density should be independent of line length or
angle. Lines should be drawn rapidly. Efficient algorithms.
4
+DDA: Digital Differential Analyzer
y i1 mx i1 Bm(x i x) Bmx i mx B(mx i B) mxy i mx
y i1 y i m x 1
(xi,yi)
(xi,Round(yi))
(xi,Round(yi+m))
(xi,yi +m)
Desired Line
Line: Left to Right:1- Slope m>0: sample at unit x intervals ( Δx= 1), calculate each succeeding y value as
5
+DDA
x i1 x i 1m
y 1
2- Slope m<0: sample at unit y
intervals ( Δy= 1), calculate each
succeeding x value as
Line: from Right to Left3- Slope m> 0:
y i1 y i m x 1
4- Slope m< 0:
x i1 x i 1m
y 1
6
+DDA
Faster than brute force.
Based on Calculating either ∆x or ∆y.
Mathematically well defined
Floating point Round off error. Time consuming
arithmetic
Advantages Disadvantages
7
+Bresenhams Line Algorithm Accurate Efficient Integer Calculations Uses Symmetry for other lines Adapted to display circles, ellipses and curves It has been proven that the algorithm gives an
optimal fit for lines
8
+Bresenhams Line Algorithm
d2
d1
Xk+1
yk+1
yyk
9
+Bresenhams Line Algorithm
y m(x i 1) bd1 y ym(xk1) b yk
d2 (yk1) yyk1 m(xk1) bd1 d2 2m(xk1) 2y 2b 1 (3 11)
decision parameter, use m yx
pk x(d1 d2)2yxk 2xy c (3 12)
10
+Bresenhams Line Algorithm
The sign of pk is the same as the sign of d1 – d2,
since Δx> 0 for our example. Parameter c is independent and will be eliminated in the recursive calculations for pk.
If the pixel at yk is closer to the line path than the pixel at yk+l (that is, d1 < d2), then decision parameter pk is negative. In that case, we plot the lower pixel; otherwise, we plot the upper pixel.
pk1 2yxk1 2xyk c
11
+ Bresenhams Line Algorithm
This recursive calculation of decision parameters is performed at each integer x position, starting at the left coordinate endpoint of the line. The first parameter, po is evaluated from Eq. 3-12 at the starting pixel position (xo, yo) and with m evaluated as Δy/Δx:
pk1 pk 2y(xk1 xk ) 2x(yk1 yk )but xk1 xk 1pk1 pk 2y 2x(yk1 yk )
p0 2y 2x
yk1 yk 0or1
12
+Bresenhams Line Drawing Algorithm1. Input the two line endpoints, store the left endpoint
(x0,y0).
2. Plot the first point (x0,y0).
3. Calculate constants ∆x, ∆y, and 2∆y - 2∆x and 2∆y, get starting values for decision parameter pk, p0=2∆y-∆x
4. At each xk along the line, starting at k = 0, do the following test: if pk < 0, the next point to plot is(xk+1, yk)
pk+1 = pk + 2∆yelse, the next point to plot is(xk+1, yk+1) pk+1=pk +2∆y-2∆x
5. Repeat step 4. ∆x times.
13
+Bresenhams Line Algorithm14
+Midpoint Line Algorithm
If (BlueLine < Midpoint) Plot_East_Pixel();
Else Plot_Northeast_Pixel();
15
+
Find an equation, given a line and a point, that will tell us if the point is above or below that line?
Midpoint Line Algorithm
y yx
x B
xy yx Bxyx xy BxF(x,y) ax by c 0F(x,y) yx xy Bxnow,d F(M) M midpoint
F(x p 1,y p 12)
y(x p 1) x(y p 12) Bx
y d y d
If F(x,y) ==0 (x,y) on the line <0 for points below the
line >0 for points above the
line d=F(M)
16
+Midpoint Line Algorithm P=(xp, yp) is pixel chosen by the algorithm in previous step
To calculate d incrementally we require dnew
If d > 0 then choose NE
d y(x p 1) x(y p 12) Bx
dnew F(x p 2,y p 32)
y(x p 2) x(y p 32) Bx
dnew d y xNE
1 2 4 3 4 dnew d NE
NE y x
P=(xp, yp)
Yp+2
M
E
NE
xp+1xp xp+2
Prev
ious
Curre
nt
Next
),1( 21 yxp
),2( 23 yxp
yp
Yp+1
MNE
17
+Midpoint Line Algorithm If d < 0 then choose E
d y(x p 1) x(y p 12) Bx
dnew F(x p 2, y p 12)
y(x p 2) x(y p 12) Bx
dnew d yE{
dnew d EE y
P=(xp, yp)
M
E
NE
xp+1xp xp+2
Prev
ious
Curre
nt
Next
),1( 21 pp yx
),2( 21 pp yx
yp
Yp+1
Yp+2
ME
18
+Midpoint Line Algorithm To find Initial value of d
d0 F(x0 1,y0 12)
y(x p 1) x(y p 12) Bx
yx p xy p Bx y 12x
F(x0, y0) y 12x
d0 y 12x
[as (x0, y0) is on the line]
P=(x0, y0)
M
E
NE
x0+1x0
Star
t
Initi
al d
o
),1( 21
00 yx
Only fractional value
d0 2y xNE 2(y x)E 2y
Multiply by 2 to avoid fractions. Redefine d0, E, NE
19
+Midpoint Line Algorithm
Midpoint: Looks at which side of the line the mid point falls on.
Bresenham: Looks at sign of scaled difference in errors.
It has been proven that Midpoint is equivalent to Bresenhams for lines.
20
+void MidpointLine(int x0, int y0, int x1, int y1, int color){
int dx = x1 – x0, dy = y1 – y0;
int d = 2*dy – dx;
int dE = 2*dy, dNE = 2*(dy – dx);
int x = x0, y = y0;
WritePixels(x, y, color);
while (x < x1) {
if (d <= 0) { // Current d
d += dE; // Next d at E
x++;
} else {
d += dNE; // Next d at NE
x++;
y++} Write8Pixels(x, y, color);}}
21
+Midpoint Circle Algorithm Implicit of equation of circle is: x2 + y2 - R2
= 0, at origin
Eight way symmetry require to calculate one octant
For each pixel (x,y), there are 8 symmetric pixels
In each iteration only calculate one pixel, but plot 8 pixels
22
23+Midpoint Circle Algorithm Define decision variable d as:
SE Choose Circle outside s
E Choose Circle inside s
iM
d
iM
d
Ryx
yxFMFd
pp
pp
0
0
1
),1()(22
212
21
P=(xp, yp)
M
E
xp+1xp xp+2
Prev
ious
Curre
nt
Next
),1( 21 pp yx ),2( 2
1 pp yx
ME
yp
yp – 1
yp – 2
SE MSE
E choose weeither Choose
0d
€
(x p + 2,y p − 32)
24+Midpoint Circle Algorithm If d <= 0 then midpoint m is inside circle
we choose E Increment x y remains unchanged
Edd
xdd
Ryx
yxFd
Ryxd
new
E
pnew
pp
ppnew
pp
32
2
),2(
1
22212
21
22212 P=(xp, yp)
M
E
xp+1xp xp+2
Prev
ious
Curre
nt
Next
),1( 21 pp yx ),2( 2
1 pp yx
ME
yp
yp – 1
yp – 2
d < 0
25+Midpoint Circle Algorithm If d > 0 then midpoint m is outside circle
we choose E Increment x Decrement y
€
d = x p +1( )2
+ y p − 12( )
2− R2
dnew = F(x p + 2, y p − 32)
= x p + 2( )2
+ y p − 32( )
2− R2
dnew − d = 2x p − 2y p + 5ΔSE
1 2 4 4 3 4 4
dnew = d + ΔSE
Prev
ious
P=(xp, yp)
M
SE
xp+1
xp xp+2
Curre
nt
Next
),1( 21 pp yx
€
(x p + 2,y p − 32)
MSE
yp
yp – 1
yp – 2
d > 0
26+Midpoint Circle AlgorithmInitial condition Starting pixel (0, R) Next Midpoint lies at (1, R – ½) d0 = F(1, R – ½) = 1 + (R2 – R + ¼) – R2 = 5/4 – R To remove the fractional value 5/4 :
Consider a new decision variable h as, h = d – ¼ Substituting d for h + ¼,
d0=5/4 – R h = 1 – R d < 0 h < – ¼ h < 0 Since h starts out with an integer value and is incremented by
integer value (E or SE), e can change the comparison to just h < 0
27+Midpoint Circle Algorithmvoid MidpointCircle(int radius, int value) {
int x = 0;int y = radius ;int d = 1 – radius ;CirclePoints(x, y, value);while (y > x) {
if (d < 0) { /* Select E */d += 2 * x + 3;
} else { /* Select SE */
d += 2 * ( x – y ) + 5;y – –;
}x++;CirclePoints(x, y, value);
}}
28+Midpoint Circle Algorithm
Void CirclePoints(int x, int y, float value){
SetPixel(x,y);SetPixel(x,-y); SetPixel(-x,y); SetPixel(-x,-y); SetPixel(y,x);
SetPixel(y,-x); SetPixel(-y,x);
SetPixel(-y,-x);}
29+ Midpoint Circle Algorithm Second-order differences can be used to enhance
performance.
52232
),(
pp
ppp yxSE
xEyx
22
52)1(23)1(2
),1(
SESEEE
yxSExE
yxnew
new
ppnew
pnewpp
42
5)1(2)1(23)1(2
)1,1(
SESEEE
yxSExE
yxnew
new
ppnew
pnewpp
E is chosen
SE is chosen 523
),0(
RSE
ER
:value Initial
MSE
MSE
E
30+Midpoint Circle Algorithmvoid MidpointCircle(int radius, int value) {
int x = 0;int y = radius ;int d = 1 – radius ;int dE = 3;int dSE = -2*radius +5;CirclePoints(x, y, value);while (y > x) {
if (d < 0) { /* Select E */d += dE;dE += 2;dSE += 2;
} else { /* Select SE */d += dSE;dE += 2;dSE += 4;y – –;}
x++;CirclePoints(x, y, value);}
}
31+Midpoint Ellipse Algorithm Implicit equation is: F(x,y) = b2x2 + a2y2 – a2b2 = 0 We have only 4-way symmetry There exists two regions
In Region 1 dx > dy Increase x at each step y may decrease
In Region 2 dx < dy Decrease y at each step x may increase
(x1,y1)(-x1,y1)
(x1,-y1)(-x1,-y1)
(-x2,y2)
(-x2,-y2)
(x2,y2)
(x2,-y2)
32+Midpoint Ellipse Algorithm
Region 1
Region 2S SE
E
SE
Gradient Vector
TangentSlope = -1
yaxb
dxdy
dxdyaybx
2
2
22 022
yaxb
dxdy
22
1 1 Region In
33+Midpoint Ellipse Algorithm In region 1
SE
ppnew
pp
ppnew
E
pnew
pp
ppnew
pp
pp
yaxbdd
bayaxb
yxFd
SEto move then 0 d if
xbdd
bayaxb
yxFd
E to move then 0 d if
bayaxb
yxFd
)22()32(
)()2(
),2(
)32(
)()2(
),2(
)()1(
),1(
22
222322
23
2
222122
21
222122
21
P=(xp, yp)
M
E
xp+1
xp
xp+2
Prev
iou
s Curre
ntNe
xt
),1( 21 pp yx ),2( 2
1 pp yx
ME
yp
yp – 1
yp – 2
SE M
SE
),2( 23 pp yx
34+Midpoint Ellipse Algorithm In region 2
SE
ppnew
pp
ppnew
S
pnew
pp
ppnew
pp
pp
yaxbdd
bayaxb
yxFd
SEto move then 0 d if
yadd
bayaxb
yxFd
Sto move then 0 d if
bayaxb
yxFd
)32()22(
)2()(
)2,(
)32(
)2()(
)2,(
)1()(
)1,(
22
222232
23
2
222212
21
222212
21
P=(xp, yp)
MS
xp+1
xp
xp+2
Prev
iou
s Curre
nt Nex
t
)1,( 21 pp yx
)2,( 21 pp yx
MS
yp
yp – 1
yp – 2
SE
MSE
)2,( 23 pp yx
35+Midpoint Ellipse AlgorithmDPx=2*ry*ry; Dpy =2*rx*rx; x=0; y=ry; Px=0; Py =2*rx*rx*ry; f =ry*ry +rx*rx(0.25-ry ); ry2=ry *ry; Set4Pixel(x,y); while (px<py ) //Region I{ x=x+1; Px=Px+DPx; if (f>0) // Bottom case{y=y -1; Py =Py -Dpy ; f=f+ry2+Px-Py;} else // Top casef=f+ry2+Px; Set4Pixel(x,y);