Sie sind auf Seite 1von 42

Scan Converting Ellipse

General equation of an ellipse:


d
1
+d
2
=constant
Or,

However, we will only consider standard ellipse:



(x, y)
F
1

F
2

d
1

d
2

( ) ( ) ( ) ( ) constant
2
2
2
2
2
1
2
1
= + + + y y x x y y x x
1
2 2
=
|
.
|

\
|

+
|
.
|

\
|

b
y y
a
x x
c c
a
b

Scan Converting Ellipse
Formally An ellipse is defined as the locus of points
satisfying following equation: r from a fixed point (h,k).
This distance is described In the Pythagoras theorem as :


where (h,k) is the centre, a and b are the length of major
and minor axis respectively
The standard equation for the ellipse at centre (0,0) is:


1
) (
2
2
2
2
=

b
k y
a
h) (x
1
2
2
2
2
= +
b
y
a
x
Scan Converting Ellipse
So, we can write a simple circle drawing algorithm by
solving the equation for y at unit x intervals using:


Which is not a good solution. (you know that)


) ) / ( 1 .(
2 2
a x b y =
Scan Converting Ellipse
Four-Way Symmetry
Like circle, ellipse centred at (0, 0) follows symmetry. But
now it is four symmetry.
Thus ellipse points are computed
in the first quadrant,
rest three of the ellipse points
are found by symmetry.
Centre can be shifted while
plotting

(x,y)
(-x,y)
(-x,-y)
(x,-y)
X-axis
Y-axis
o
Scan Converting Ellipse
So we define a routine that plots ellipse pixels with centre
(h,k) in all the four quadrants

put_ellipse_pixel(x,y,h,k)
{
Put_pixel(h+x, k+y)
Put_pixel(h-x, k+y)
Put_pixel(h+x, k-y)
Put_pixel(h-x, k-y)
}

Ellipse Drawing Algorithms

1. Polar Domain Algorithms
2. Midpoint Ellipse Algorithm
3. Bresenham Ellipse Algorithm


Polar Domain Algorithm
We could use polar coordinates r and ,
x =h + a cos y = k + b sin
where (h,k) is the centre, a and b are the length of major and minor axis
respectively

A fixed angular step size can be used to plot equally spaced
points along the circumference

A step size of 1/a or 1/b, whichever is smaller can be used to
set pixel positions to approximately 1 unit apart for a
continuous boundary.
Polar Domain Algorithm
We could also use incremental form. Let P(x
k
,y
k
) be currently
plotted point,
x
k
= a cos()
y
k
= b sin()
At next iteration k = k+1 ; = +A
x
k+1
= a cos(+ A)
= acos() cos(A) - a sin() sin(A)
= x
k
cos(A) - y
k
(a/b)sin(A)
y
k+1
= b sin( +A)
= b sin() cos(A) - b cos() sin(A)
= y
k
cos(A) + x
k
(b/a)sin(A)
Polar Domain Algorithm
STEPS
1. Input centre (h,k),and a,b
2. If (a>b) Au = 1/a else Au = 1/b ;
3. C = cos(Au);S = sin(Au); t = a/b;
4. Initialize x = 0;y = b; u = 0;
5. WHILE (u < t/2)
{
xtemp = x
x = x*C-t*y*S;
y = y*C+(1/t)*xtemp*S;
put_ellipse_pixel(x,y,h,k);
u = u+ Au;
}
This method is still computationally expensive
Ellipse Drawing Algorithms

1. Polar Domain Algorithms
2. Midpoint Ellipse Algorithm
3. Bresenham Ellipse Algorithm




Midpoint Ellipse Algorithm
Reconsider an ellipse centered at the origin:



The discriminator function?

and its properties:
f
e
(x,y) < 0 for a point inside the ellipse
f
e
(x,y) > 0 for a point outside the ellipse
f
e
(x,y) = 0 for a point on the ellipse
( )
2 2 2 2 2 2
, b a y a x b y x f
e
+ =
1
2 2
=
|
.
|

\
|
+
|
.
|

\
|
b
y
a
x
Midpoint Ellipse Algorithm
Midpoint Ellipse Algorithm
Midpoint Ellipse Algorithm
Ellipse is different from circle.
Similar approach with circle, different
is sampling direction.
Region 1:
Sampling is at x direction
Choose between (x
k
+1, y
k
), or (x
k
+1, y
k
-1)
Midpoint: (x
k
+1, y
k
-0.5)
Region 2:
Sampling is at y direction
Choose between (x
k
, y
k
-1), or (x
k
+1, y
k
-1)
Midpoint: (x
k
+0.5, y
k
-1)
Slope =
-1
Region 1
Region 2
Midpoint Ellipse Algorithm
Region 1:


p1
k
ve:
midpoint is inside
choose pixel (x
k+1
, y
k
)
p1
k
+ve:
midpoint is outside
choose pixel (x
k+1
, y
k
-1)
( )
2
1
, 1 1 + =
k k e k
y x f p
Region 2:


p2
k
ve:
midpoint is inside
choose pixel (x
k
+1, y
k
-1)

p2
k
+ve:
midpoint is outside
choose pixel (x
k
, y
k
-1)
( ) 1 , 2
2
1
+ =
k k e k
y x f p
Decision Parameters
Midpoint Ellipse Algorithm
Derivation for Region 1:
Let us assume that P(x
k
, y
k
)
is the currently plotted pixel.
Q(x
k+1
, y
k+1
) (x
k+1
, y ) is
the next point along the actual
circle path. We need to decide
next pixel to be plotted from
among candidate positions
Q1(x
k
+1, y
k
) or
Q2(x
k
+1, y
k
-1)
Midpoint Ellipse Algorithm
Our decision parameter is the circle function evaluated at the
midpoint between these two pixels
p1
k
= f
e
(x
k
+1, y
k
-1/2) = b
2
(x
k
+1)
2
+ a
2
(y
k
-1/2)
2
a
2
b
2

If p1
k
< 0 ,
this midpoint is inside the ellipse and
the pixel on the scan line y
k
is closer to the ellipse boundary.
Otherwise,
the mid position is outside or on the ellipse boundary,
and we select the pixel on the scan line y
k
-1

Midpoint Ellipse Algorithm
Successive decision parameters are obtained using incremental
calculations
p1
k
= b
2
(x
k
+1)
2
+ a
2
(y
k
)
2
a
2
b
2

Put k = k+1
p1
k+1
= b
2
[(x
k+1
)+1]
2
+ a
2
(y
k+1
)
2
a
2
b
2
= b
2
(x
k
+2)
2
+ a
2
(y
k+1
)
2
a
2
b
2

subtracting p
k
from p
k+1

p1
k+1
p1
k
= b
2
(x
k
+2)
2
+ a
2
(y
k+1
)
2
[b
2
(x
k
+1)
2
+ a
2
(y
k
)
2
]
or
p1
k+1
= p1
k
+2 b
2
(x
k
+1) + a
2
[(y
k+1
)
2
(y
k
)
2
]+b
2

Where y
k+1
is either y
k
or y
k-1
, depending on the sign of p1
k
Midpoint Ellipse Algorithm
If p1
k
< 0
Q1(x
k
+1, y
k
) was the next choice
y
k+1
= y
k
(y
k+1
)
2
(y
k
)
2
= 0
p1
k+1
= p1
k
+ b
2
(2.x
k
+3)
else
Q2(x
k
+1, y
k
-1) was the next choice
y
k+1
= y
k
1

(y
k+1
)
2
(y
k
)
2
= 2.y
k
+2
p1
k+1
= p1
k
+ b
2
(2.x
k
+3) + a
2
( 2.y
k
+ 2)



Midpoint Ellipse algorithm
Initial decision parameter is obtained by evaluating the circle
function at the start position (x0,y0) = (0,b)
p1
0
= f
e
(1, b 1/2)

= b
2
a
2
b + a
2
Midpoint Ellipse Algorithm
Derivation for Region 2:
Let us assume that P(x
k
, y
k
)
is the currently plotted pixel.
Q(x
k+1
, y
k+1
) (x
k+1
, y ) is
the next point along the actual
circle path. We need to decide
next pixel to be plotted from
among candidate positions
Q1(x
k
, y
k
-1) or
Q2(x
k
+1, y
k
-1)
Midpoint Ellipse Algorithm
Our decision parameter is the circle function evaluated at the
midpoint between these two pixels
p2
k
= f
e
(x
k
+1/2, y
k
-1) = b
2
(x
k
+1/2)
2
+ a
2
(y
k
1)
2
a
2
b
2

If p2
k
<0 ,
tthe mid position is inside or on the ellipse boundary,
and we select the pixel on the scan line x
k
+1
Otherwise,
his midpoint is outside the ellipse and
the pixel on the scan line x
k
is closer to the ellipse boundary.
Midpoint Ellipse Algorithm
Successive decision parameters are obtained using incremental
calculations
p2
k
= b
2
(x
k
+)
2
+ a
2
(y
k
1 )
2
a
2
b
2
Put k = k+1
p2
k+1
= b
2
[(x
k+1
)+]
2
+ a
2
(y
k+1
1 )
2
a
2
b
2
= b
2
[(x
k+1
)+]
2
+ a
2
(y
k
2 )
2
a
2
b
2

subtracting p
k
from p
k+1

p2
k+1
p2
k
= b
2
(x
k+1
+)
2
+ a
2
(y
k
2 )
2
[b
2
(x
k
+)
2
+ a
2
(y
k
1 )
2
]
or
p2
k+1
= p2
k
+b
2
[(x
k+1
+)
2
(x
k
+)
2
] a
2
(2.y
k


3)
Where x
k+1
is either x
k
or x
k+1
, depending on the sign of p2
k
Midpoint Ellipse Algorithm
If p2
k
< 0
Q2(x
k
+1, y
k
-1) was the next choice
x
k+1
= x
k
+1

(x
k+1
+)
2
(x
k
+)
2
= 2.x
k
+2
p2
k+1
= p2
k
+ b
2
(2.x
k
+2) a
2
(2.y
k
3)
else
Q1(x
k
, y
k
1) was the next choice
x
k+1
= x
k
(x
k+1
+)
2
(x
k
+)
2
= 0
p2
k+1
= p2
k
a
2
(2.y
k
3)
Midpoint Ellipse algorithm
Initial decision parameter is obtained by evaluating the circle
function at the start position (x
0
,y
0
) the last point plotted after
drawing region 1
p2
0
= f
e
(x
0
+, y
0
1)

= b
2
(x
0
+)
2
+ a
2
( y
0
1)
2
a
2
b
2
Midpoint Ellipse algorithm
Condition to move from one region to another
Starting at point (0,b) in the region 1 where slope is <1 we take unit step
increment along x-direction until we reach region 2 where slope is >1. At each
step we need to check slope.
The ellipse slope can be calculated as
At the boundary of region 1 and 2
dy/dx = -1
2b
2
x = 2a
2
y
We move out of region 1 if 2b
2
x 2a
2
y


y a
x b
dx
dy
2
2
2
2
=
Midpoint Ellipse Algorithm
1. Input a, b and ellipse center (x
c
, y
c
). First point on
the similar ellipse centered at the origin is (0, b).
2. Initial value for decision parameter at region 1:


3. At each x
k
in region 1, starting from k = 0, test p1
k
:
If p1
k
< 0, next point (x
k
+1, y
k
) and


else, next point (x
k+1
, y
k
-1) and


4. Determine symmetry points in the other 3 octants.
5. Get the actual point for ellipse centered at (x
c
, y
c
)
that is (x + x
c
, y + y
c
).
6. Repeat step 3 - 6 until 2b
2
x > 2a
2
y.
2
4
1
2 2
0
1 a b a b p + =
, 3 2 1 1
2 2
1
b x b p p
k k k
+ + =
+
. 2 3 2 2 1 1
2 2 2 2
1
a b y a x b p p
k k k k
+ + + =
+
Midpoint Ellipse Algorithm
7. Initial value for decision parameter in region 2:


8. At each y
k
in region 2, starting from k = 0, test p2
k
:
If p2
k
> 0, next point is (x
k
, y
k
-1) and


else, next point is (x
k
+1, y
k
-1) and


9. Determine symmetry points in the other 3 octants.
10.Get the actual point for ellipse centered at (x
c
, y
c
)
that is (x + x
c
, y + y
c
).
11.Repeat step 8 - 10 until y < 0.

2 2
1
3 2 2 2 a y a p p
k k k
+ =
+
( ) ( )
2 2
2
0
2
2
2
1
0
2
0
1 2 b a y a x b p + + =
. 2 3 2 2 2 2
2 2 2 2
1
b a y a x b p p
k k k k
+ + + =
+
Ellipse Drawing Algorithms

1. Polar Domain Algorithms
2. Midpoint Ellipse Algorithm
3. Bresenham Ellipse Algorithm




Bresenhams Ellipse Algorithm
Reconsider an ellipse centered at the origin:



or

We will define the distance from the actual value to two
candidate positions and proceed as in circle algorithm.

1
2 2
=
|
.
|

\
|
+
|
.
|

\
|
b
y
a
x
)
2
) / ( 1 .(
2 2
a x b y =
Bresenhams Ellipse Algorithm
Derivation for Region 1
Let us assume that P(x
k
, y
k
) is
the currently plotted pixel.
Q(x
k+1
, y
k+1
) (x
k+1
, y ) is the
next point along the actual
circle path. We need to decide
next pixel to be plotted from
two candidate positions
Q1(x
k
+1, y
k
) or Q2(x
k
+1, y
k
-1)
Q1
Q2
P
Bresenhams Ellipse Algorithm
Thus actual value of y at x = x
k+1
is given by
y
2

= b
2
(1 ((x
k
+1) /a)

2
)
We define the distance measure in squares to simplify calculations
Let d
1
= y
k
2

y
2


= y
k
2

[b
2
(1 ((x
k
+1) /a)

2
)]


d
2
= y
2

(y
k
-1)

2


= [b
2
(1 ((x
k
+1) /a)

2
)]

(y
k
-1)

2


The difference in d
1
and d
2
determine the next pixel to be plotted
if (d
1
-d
2
)<0 Q1(x
k
+1, y
k
) is plotted
else Q2(x
k
+1, y
k
-1) is plotted.

Bresenhams Ellipse Algorithm
We can define a decision parameter p
k
for the k
th
step as follows:
p1
k
= ( d
1
-d
2
)
= 2. b
2
(1 (x
k
+1)

2
/a
2
)+ y
k
2
+ (y
k
1)

2

To put p
k
in recurrence relation replace k = k+1


p1
k+1
= 2. b
2
(1 (x
k+1
+1)

2
/a
2
)+ y
k+1
2
(y
k+1
1)

2

= 2. b
2
(1 (x
k
+2)

2
/a
2
)+ y
k+1
2
(y
k+1
1)

2

From above two equations

p1
k+1
p1
k
= ?
p1
k+1
= p1
k
+ (b
2
/a
2
)(4x
k
+6) + [y
2
k+1
y
2
k
] [(y
k+1
-1)
2
(y
k
-1)
2
]

Bresenhams Ellipse Algorithm
If p1
k
< 0
Q1(x
k
+1, y
k
) was the next choice
y
k+1
= y
k
[y
k+1
2
y
k
2
] [(y
k+1
-1)

2
(y
k
-1)

2
] = 0
p1
k+1
= p1
k
+ (b
2
/a
2
)(4.x
k
+6)
else
Q2(x
k
+1, y
k


1) was the next choice
y
k+1
= y
k


1

[y
k+1
2
y
k
2
] [(y
k+1
-1)

2
(y
k
-1)

2
] = 4.y
k
+4
p1
k+1
= p1
k
+ (b
2
/a
2
)(4.x
k
+6) (4.y
k
4)
Bresenhams Ellipse Algorithm
The first parameter p
0
is directly computed from:
P
k
= 2. b
2
(1 (x
k
+1)

2
/a
2
)+ y
k
2
+ (y
k
1)

2


By putting k = 0, x
k
= 0 and y
k
= b
p
0
= 2. b
2
(1 (0

+1)

2
/a
2
)+ b
2
+ (b

1)

2

= 2.b
2
/a
2
2.b + 1
Let us derive it for region 2
Bresenhams Ellipse Algorithm
Derivation for Region 2
Let us assume that P(x
k
, y
k
) is
the currently plotted pixel.
Q(x
k+1
, y
k+1
) (x, y
k+1
) is the
next point along the actual
circle path. We need to decide
next pixel to be plotted from
two candidate positions
Q1(x
k
, y
k
-1 ) or Q2(x
k
+1, y
k
-1)
Bresenhams Ellipse Algorithm
Thus actual value of x at y = y
k+1
= y
k
1is given by
x
2

= a
2
(1 ((y
k
1) /b)

2
)
We define the distance measure in squares to simplify calculations
Let d
1
= x
2

x
k
2


= [a
2
(1 ((y
k
1) /b)

2
)]

x
k
2


d
2
= (x
k
+ 1)

2
x
2


= (x
k
+1)

2
[a
2
(1 ((y
k
1) /b)

2
)]


The difference in d
1
and d
2
determine the next pixel to be plotted
if (d
1
-d
2
)>0 Q1(x
k
, y
k
1 ) is plotted
else Q2(x
k
+1, y
k
1) is plotted.

Bresenhams Ellipse Algorithm
We can define a decision parameter p2
k
for the k
th
step as follows:
p2
k
= ( d
1
-d
2
)
= 2. a
2
(1 (y
k
-1)

2
/b
2
) x
k
2
(x
k
+ 1)

2

To put p2
k
in recurrence relation replace k = k+1


p2
k+1
= 2. a
2
(1 (y
k+1
1)

2
/b
2
) x
k+1
2
(x
k+1
+ 1)

2

= 2. a
2
(1 (y
k
2)

2
/b
2
) x
k+1
2
(x
k+1
+ 1)

2

From above two equations

p2
k+1
p2
k
= ?
p2
k+1
= p2
k
(a
2
/b
2
)(4y
k
6) [x
2
k+1
+ x
2
k
] [(x
k+1
+1)
2
+ (x
k
+1)
2
]

Bresenhams Ellipse Algorithm
If p2
k
> 0
Q1(x
k
, y
k
1) was the next choice
x
k+1
= x
k
[x
k+1
2
x
k
2
] [(x
k+1
+1)

2
(x
k
+1)

2
] = 0
p2
k+1
= p2
k
(a
2
/b
2
)(4.y
k
6)
else
Q2(x
k
+1, y
k


1) was the next choice
x
k+1
= x
k
+

1

[x
k+1
2
x
k
2
] [(x
k+1
+1)

2
(x
k
+1)

2
] = 4.x
k
+4
p2
k+1
= p2
k
(a
2
/b
2
)(4.y
k


6) + (4.x
k
+ 4)
Bresenhams Ellipse Algorithm
The first parameter p2
0
is directly computed from:
p2
k
=2. a
2
(1 (y
k
-1)

2
/b
2
) x
k
2
(x
k
+ 1)

2
Where (x
k
, y
k
) is the last point plotted in region .
Write the algorithm, implement in lab and report me how the
output of this algorithm is different from Midpoint Algorithm
Brsenhams Ellipse Algorithm
1. Input a, b and ellipse center (x
c
, y
c
). First point on
the similar ellipse centered at the origin is (0, b).
2. Initial value for decision parameter at region 1:


3. At each x
k
in region 1, starting from k = 0, test p1
k
:
If p1
k
< 0, next point (x
k
+1, y
k
) and


else, next point (x
k+1
, y
k
-1) and


4. Determine symmetry points in the other 3 octants.
5. Get the actual point for ellipse centered at (x
c
, y
c
)
that is (x + x
c
, y + y
c
).
6. Repeat step 3 - 6 until 2b
2
x > 2a
2
y.
. 2 1 . 2 1
2 2
0
b a b p + =
). 3 . 2 ( 2 1 1
2 2
1
+ + =
+ k k k
x a b p p
). 1 ( 4 ) 3 . 2 ( 2 1 1
2 2
1
+ + =
+
y x a b p p
k k k
Bresenhams Ellipse Algorithm
7. Initial value for decision parameter in region 2:


8. At each y
k
in region 2, starting from k = 0, test p2
k
:
If p2
k
> 0, next point is (x
k
, y
k
-1) and


else, next point is (x
k
+1, y
k
-1) and


9. Determine symmetry points in the other 3 octants.
10.Get the actual point for ellipse centered at (x
c
, y
c
)
that is (x + x
c
, y + y
c
).
11.Repeat step 8 - 10 until y < 0.

). 6 . 4 )( ( 2 2
2 2
1
=
+ k k k
y b a p p
( )
2 2 2 2 2
0
) 1 ( ) 1 ( 1 . 2 2 + = x x b y a p
). 4 . 4 ( ) 6 . 4 ( 2 2
2 2
1
+ + =
+ k k k k
x y a b p p

Das könnte Ihnen auch gefallen