Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Rastru grafikas algoritmi

Elipses līnijas veidošanas algoritms

Ellipse-Generating Algorithms
Ellipse-Generating Algorithms

d1
F1 P=(x,y)
d2
F2

 The sum of the two distances d1 and d2, between the


fixed positions F1 and F2 (called the foci of the ellipse)
to any point P on the ellipse, is the same value, i.e.
d1 + d2 = const
2
Ellipse Properties
Expressing distances d1 and d2 in terms of the focal
coordinates F1 = (x1, y1) and F2 = (x2, y2), we have:

( x  x1 )2  ( y  y1 )2  ( x  x2 )2  ( y  y2 )2  constant

ry
rx

2
 x  xc   y  yc 
2

Cartesian coordinates:       1
 rx   ry 
Polar coordinates: x  xc  rx cos 
y  yc  ry sin 
3
Ellipse Algorithms
• Symmetry between quadrants
• Not symmetric between the two octants of a quadrant
• Thus, we must calculate pixel positions along the elliptical arc
through one quadrant and then we obtain positions in the
remaining 3 quadrants by symmetry

(-x, y) (x, y)
ry
rx

(-x, -y) (x, -y)


4
Ellipse Algorithms
fellipse ( x, y)  r x  r y  r r
2 2
y x
2 2 2 2
x y

Decision parameter:
 0 if ( x, y ) is inside the ellipse

f ellipse ( x, y )   0 if ( x, y ) is on the ellipse
 0 if ( x, y ) is outside the ellipse
Slope = -1

1
ry 2 dy 2ry2 x
Slope   2
rx dx 2rx y

5
Ellipse Algorithms
Slope = -1
• Starting at (0, ry) we take unit steps in the x
direction until we reach the boundary between ry 1 2
region 1 and region 2. Then we take unit steps in rx
the y direction over the remainder of the curve
in the first quadrant.
• At the boundary
dy
 1  2ry2 x  2rx2 y
dx
• therefore, we move out of region 1 whenever

2ry2 x  2rx2 y

6
Midpoint Ellipse Algorithm
Midpoin
yi  t

yi-1

xi xi+1 xi+2

Assuming that we have just plotted the pixels at (xi , yi).


The next position is determined by:
p1i  f ellipse ( xi  1, yi  12 )
 ry2 ( xi  1)2  rx2 ( yi  12 )2  rx2 ry2
If p1i < 0 the midpoint is inside the ellipse  yi is closer
If p1i ≥ 0 the midpoint is outside the ellipse  yi – 1 is closer
7
Decision Parameter (Region 1)
At the next position [xi+1 + 1 = xi + 2]
p1i 1  f ellipse ( xi 1  1, yi 1  12 )
 ry2 ( xi  2)2  rx2 ( yi 1  12 ) 2  rx2 ry2

OR
p1i 1  p1i  2ry2 ( xi  1)2  ry2  rx2 ( yi 1  12 )2  ( yi  12 )2 

where yi+1 = yi or yi+1 = yi – 1

8
Decision Parameter (Region 1)
Decision parameters are incremented by:

 2
 y i 1
2 r x  r 2
if p1i  0
increment   2 y

 2 r
 y i 1x  ry
2
 2 r 2
x yi 1 if p1i  0
Use only addition and subtraction by obtaining
2ry2 x and 2rx2 y

At initial position (0, ry)


2ry2 x  0
2rx2 y  2rx2 ry

p10  f ellipse (1, ry  12 )  ry2  rx2 (ry  12 ) 2  rx2 ry2


9
 ry2  rx2 ry  14 rx2
Region 2
Over region 2, step in the negative y direction and midpoint is
taken between horizontal pixels at each step.

Midpoint
yi 
yi-1

xi xi+1 xi+2
Decision parameter:

p 2i  f ellipse ( xi  12 , yi  1)
 ry2 ( xi  12 )2  rx2 ( yi  1)2  rx2 ry2
If p2i > 0 the midpoint is outside the ellipse  xi is closer
If p2i ≤ 0 the midpoint is inside the ellipse  xi + 1 is closer
10
Decision Parameter (Region 2)
At the next position [yi+1 – 1 = yi – 2]
p 2i 1  f ellipse ( xi 1  12 , yi 1  1)
 ry2 ( xi 1  12 )2  rx2 ( yi  2)2  rx2 ry2

OR
p2i 1  p2i  2rx2 ( yi  1)  rx2  ry2 ( xi 1  12 )2  ( xi  12 )2 

where xi+1 = xi or xi+1 = xi + 1

11
Decision Parameter (Region 2)
Decision parameters are incremented by:

  2
 x i 1 x
2 r y  r 2
if p 2i  0
increment   2
 2 r x
 y i 1  2 r 2
y
x i 1  rx
2
if p 2i  0

At initial position (x0, y0) is taken at the last


position selected in region 1

p 20  f ellipse ( x0  12 , y0  1)
 ry2 ( x0  12 )2  rx2 ( y0  1)2  rx2 ry2

12
Midpoint Ellipse Algorithm
1. Input rx, ry, and ellipse center (xc, yc), and obtain the first
point on an ellipse centered on the origin as
(x0, y0) = (0, ry)
2. Calculate the initial parameter in region 1 as
p10  ry2  rx2 ry  14 rx2

3. At each xi position, starting at i = 0, if p1i < 0, the next


point along the ellipse centered on (0, 0) is (xi + 1, yi) and
p1i 1  p1i  2ry2 xi 1  ry2

otherwise, the next point is (xi + 1, yi – 1) and


p1i 1  p1i  2ry2 xi 1  2rx2 yi 1  ry2

and continue until 2ry


2
x  2r 2
x y 13
Midpoint Ellipse Algorithm
4. (x0, y0) is the last position calculated in region 1.
Calculate the initial parameter in region 2 as
p20  ry2 ( x0  12 )2  rx2 ( y0  1)2  rx2 ry2
5. At each yi position, starting at i = 0, if p2i > 0, the next
point along the ellipse centered on (0, 0) is (xi, yi – 1) and
p2i 1  p2i  2rx2 yi 1  rx2
otherwise, the next point is (xi + 1, yi – 1) and
p2i 1  p2i  2ry2 xi 1  2rx2 yi 1  rx2
Use the same incremental calculations as in region 1.
Continue until y = 0.
6. For both regions determine symmetry points in the other
three quadrants.
7. Move each calculated pixel position (x, y) onto the
elliptical path centered on (xc, yc) and plot the
coordinate values
x = x + xc , y = y + yc
14
rx = 8 , ry = 6
Example 2ry2x = 0 (with increment 2ry2 = 72)
2rx2y = 2rx2ry (with increment -2rx2 = -128)
i pi xi+1, yi+1 2ry2xi+1 2rx2yi+1 Region 1
0 -332 (1, 6) 72 768 (x0, y0) = (0, 6)
1 -224 (2, 6) 144 768
p10  ry2  rx2 ry  14 rx2  332
2 -44 (3, 6) 216 768
3 208 (4, 5) 288 640 Move out of region 1 since
4 -108 (5, 5) 360 640 2ry2x > 2rx2y
5 288 (6, 4) 432 512
6 244 (7, 3) 504 384 6    
5  
4 
3 
2
1
0
0 1 2 3 4 5 6 7 8
15
Example
Region 2
(x0, y0) = (7, 3) (Last position in region 1) p20  fellipse (7  12 , 2)  151

i pi xi+1, yi+1 2ry2xi+1 2rx2yi+1


0 -151 (8, 2) 576 256
1 233 (8, 1) 576 128
2 745 (8, 0) - - Stop at y = 0

6    
5  
4 
3 
2 
1 
0 
0 1 2 3 4 5 6 7 8 16

You might also like