Sei sulla pagina 1di 73

Computer Graphics

Chapter 3
Graphics Output Primitives
Andreas Savva
2
Basic Raster Algorithms
for 2D Primitives
Description of pictures
Specified by a set of intensities for the pixel positions.
Describe it as a set of complex objects, such as trees,
furniture and walls, positioned at specific coordinate
locations within the scene.
Graphics programming packages provide functions to
describe a scene in terms of basic geometric structures
referred to as output primitives.
Points
Straight lines
Circles
Splines curves and surfaces
Polygon color areas
Character strings
Etc.
3
Implementing Application programs
Description of objects in terms of primitives and
attributes and converts them to the pixels on the
screen.
Primitives what is to be generated
Attributes how primitives are to be generated
4
Points
The electron beam is turned on to illuminate the phosphor
at the selected location (x, y) where
0 x maxx
0 y maxy

setpixel(x, y, intensity) loads an intensity value into the
frame-buffer at (x, y).
getpixel(x, y) retrieves the current frame-buffer
intensity setting at position (x, y).

(0,0)
(maxx,maxy)
CRT
5
Lines
Analog devises, such as a random-scan display or a
vector plotter, display a straight line smoothly from one
endpoint to another. Linearly varying horizontal and
vertical deflection voltages are generated that are
proportional to the required changes in the x and y
directions to produce the smooth line.
6
Digital devices display a straight line by plotting discrete
coordinate points along the line path which are calculated
from the equation of the line.
Screen locations are referenced with integer values, so plotted
positions may only approximate actual line positions between two
specific endpoints.
A computed line position of (10.48, 20.51) will be converted to
pixel position (10, 21). This rounding of coordinate values to
integers causes lines to be displayed with a stairstep appearance
(the jaggies).
Particularly noticeable on systems with low resolution.
To smooth raster lines, pixel intensities along the line paths must
be adjusted.
7
Line Drawing Algorithms
Cartesian equation:
y = mx + c
where
m slope
c y-intercept
x
y
x x
y y
m
A
A
=

=
1 2
1 2
x
1
y
1
x
2
y
2
8
Slope
if |m| = 1
u = 45
45
45
+ve -ve
u
u
u
u
if |m| < 1
-45 < u < 45
if |m| > 1
45 < u < 90 or
-90 < u < -45
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
y = x
m = 1
c = 0
y
x
x y
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
|m| = 1
10
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
y = x + 1
m =
c = 1
y
x
x y round(y)
0 1 1
1 1.5 2
2 2 2
3 2.5 3
4 3 3
5 3.5 4
6 4 4
7 4.5 5
8 5 5
|m| < 1
11
|m| > 1
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
y = 3x - 2
m = 3
c = -2
y
x
x y round(y)
0 -2 -2
1 1 1
2 4 4
3 7 7
4 10 10
5 13 13
6 16 16
7 19 19
8 22 22
outside
12
The Digital Differential Analyzer
(DDA) Algorithm
m
x
y
=
A
A
x y
0 1
1 4
2 7
3 10
4 13
5 16
means that for a unit (1) change in x there is
m-change in y.
i.e. y = 3x + 1 m = 3
m y
x 1
=
A
A
means that for a unit (1) change in y there is
1/m change in x.
Do not use
y = 3x + 1
to calculate y.
Use m
13
The DDA Method
Uses differential equation of the line : m
If slope |m| s 1 then increment x in steps of 1 pixel
and find corresponding y-values.
If slope |m| > 1 then increment y in steps of 1 pixel
and find corresponding x-values.













step through in x step through in y
14
The DDA Method
Desired line
(x
i
,round(y
i
))
(x
i
,y
i
)
(x
i
+1,round(y
i
+m))
(x
i
+1,y
i
+m)
15
if slope m > 0
if |m| s 1
x
i+1
= x
i
+ 1
y
i+1
= y
i
+ m
if |m| > 1
y
i+1
= y
i
+ 1
x
i+1
= x
i
+ 1/m
Left
Right
Left
Right
16
Proceeding from right-endpoint to left-endpoint
if slope m > 0
if |m| s 1
x
i+1
= x
i
- 1
y
i+1
= y
i
- m
if |m| > 1
y
i+1
= y
i
- 1
x
i+1
= x
i
- 1/m
Left
Right
Left
Right
17
if slope m < 0
if |m| s 1
x
i+1
= x
i
+ 1
y
i+1
= y
i
+ m
if |m| > 1
y
i+1
= y
i
- 1
x
i+1
= x
i
- 1/m
Left
Right
Left
Right
18
Proceeding from right-endpoint to left-endpoint
if slope m < 0
if |m| s 1
x
i+1
= x
i
- 1
y
i+1
= y
i
- m
if |m| > 1
y
i+1
= y
i
+ 1
x
i+1
= x
i
+ 1/m
Left
Right
Left
Right
19
Example (DDA)

+ =
+ =
s s
+ =
+
+
3
1
1
1
3
1
1
1 0
1
i i
i i
y y
x x
m
x y
x y round(y)
0 1 1
1 4/3 1
2 5/3 2
3 2 2
4 7/3 2
5 8/3 3
6 3 3
7 10/3 3
8 11/3 4
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
y
x
20
Example (DDA)

=
=
<
+ =
+
+
) (
1
1
8 3
3
1
1
1
i i
i i
x x
y y
m
x y
y x round(x)
8 0 0
7 1/3 0
6 2/3 1
5 1 1
4 4/3 1
3 5/3 2
2 2 2
1 7/3 2
0 8/3 3
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
y
x
21
void LineDDA(int x0, int y0, int x1, int y1)
{
int dx = x1 x0, dy = y1 y0, steps;

if (abs(dx)>abs(dy)) steps = abs(dx);
else steps = abs(dy);

// one of these will be 1 or -1
double xIncrement = (double)dx / (double )steps;
double yIncrement = (double)dy / (double )steps;

double x = x0;
double y = y0;
setPixel(round(x), round(y));

for (int i=0; i<steps; i++) {
{
x += xIncrement;
y += yIncrement;
setPixel(round(x), round(y));
}
}
Note: The DDA algorithm is faster than the direct use of y = mx + c.
It eliminates multiplication; only one addition.
22
Example
Draw a line from point (2,1) to (12,6)
Draw a line from point (1,6) to (11,0)

7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12
23
Bresenham Line Algorithm
A more efficient approach
Basis of the algorithm:






From start position decide A or B next
A
B
Start position
24
Bresenham Line Algorithm
For a given value of x
one pixel lies at distance t
i
above the line, and
one pixel lies at distance s
i
below the line
True line
s
i

t
i
25
Bresenham Line Algorithm
Decision parameter

d
i
= (s
i
- t
i
)

If d
i
< 0, then closest pixel is below true line (s
i
smaller)
If d
i
> 0, then closest pixel is above true line (t
i
smaller)


We must calculate the new values for d
i
as we move
along the line.
26
3dy
2dy
dy
Example:
) 2 or 5 . 0 (i.e. 0.5 (slope) gradient Let dx dy
dx
dy
< < <
Start pixel at (x
0
,y
1
)
4dy
At x
1
:
s
1
= dy t
1
= dx - dy
d
1
= (s
i
- t
i
) = dy - (dx - dy) = 2dy - dx
but 2dy < dx d
i
< 0 y stays the same
hence next pixel is at (x
1
,y
1
)
At x
2
:
s
2
= 2dy t
2
= dx - 2dy
d
2
= (s
2
t
2
) = 2dy - (dx - 2dy) = 4dy - dx
Suppose d2 > 0 y is incremented
hence next pixel is at (x
2
,y
2
)

At x
3
:
s
3
= 3dy - dx t
2
= 2dx - 3dy
d
3
= (s
2
t
3
) = 6dy - 3dx < 0
so y stays the same
hence next pixel is at (x
3
,y
2
)
x
1
x
2
x
3
x
4
x
5
x
0
y
0
y
1
y
2
y
3
y
5
27
In General
For a line with gradient 1
d
0
= 2dy dx
if d
i
< 0 then y
i+1
= y
i
d
i+1
= d
i
+ 2dy
if d
i
0 then y
i+1
= y
i
+ 1

d
i+1
= d
i
+ 2(dy dx)
x
i+1
= x
i
+ 1
For a line with gradient > 1
d
0
= 2dx dy
if d
i
< 0 then x
i+1
= x
i
d
i+1
= d
i
+ 2dx
if d
i
0 then x
i+1
= x
i
+ 1

d
i+1
= d
i
+ 2(dx dy)
y
i+1
= y
i
+ 1
Note: For |m| 1 the constants 2dy and 2(dy-dx) can be calculated once,
so the arithmetic will involve only integer addition and subtraction.
28
Example Draw a line from (20,10) to (30,18)
19
18
17
16
15
14
13
12
11
10
20 21 22 23 24 25 26 27 28 29 30 31 32
(20,10)
(30,18)
dx = 10
dy = 8

initial decision d
0
= 2dy dx = 6
Also 2dy = 16, 2(dy dx) = -4
i d
i
(x
i+1
,y
i+1
)
0 6 (21,11)
1 2 (22,12)
2 -2 (23,12)
3 14 (24,13)
4 10 (25,14)
5 6 (26,15)
6 2 (27,16)
7 -2 (28,16)
8 14 (29,17)
9 10 (30,18)
29
void LineBres(int x0, int y0, int x1, int y1) // line for |m| < 1
{
int dx = abs(x1 x0), dy = abs(y1 y0);
int d = 2 * dy dx, twoDy = 2 * dy, twoDyMinusDx = 2 * (dy dx);
int x, y;

if (x0 > x1) { // determines which point to use as start, which as end
x = x1;
y = y1;
x1 = x0;
}
else {
x = x0;
y = y0;
}
setPixel(x,y);

while (x < x1) {
x++;
if (d < 0) d += twoDy;
else {
y++;
d += twoDyMinusDx;
}
setPixel(x, y);
}
}
30
Special cases
Special cases can be handled separately
Horizontal lines (Ay = 0)
Vertical lines (Ax = 0)
Diagonal lines (|Ax| = |Ay|)
directly into the frame-buffer without
processing them through the line-plotting
algorithms.
31
Parallel Line Algorithms
Take advantage of multiple processors.
Given n
p
processors, subdivide the line path into n
p
Bresenham
segments.
For a line with slope 0 < m < 1 and leftpoint (x
0
,y
0
) the distance to
the right endpoint (left endpoint for next segment) is



where
Ax = width of the line
Ax
p
is computed using integer division

Numbering the segments, and the processors, as 0, 1, 2, , n
p
-1,
starting x-coordinate for the k
th
partition is
x
k
= x
0
+ kAx
p

p
p
p
n
n x
x
1 + A
= A
32
Parallel Line Algorithms (continue)
i.e. Ax = 15 , n
p
= 4 processors



Starting x-values at x
0
, x
0
+ 4, x
0
+ 8, x
0
+ 12

Ay
p
= mAx
p

At the k
th
segment, the starting y-coordinates is
y
k
= y
0
+ round(kAy
p
)
Also, the initial decision parameter for Bresenhams
algorithm at the start of the k
th
subinterval is:
p
k
= (kAx
p
)(2Ay) round(kAy
p
)(2Ax) + 2Ay Ax
4
4
18
4
1 4 15
= =
+
= A
p
x
33
Anti-aliasing for straight lines
Lines generated can have jagged or stair-step appearance,
one aspect of phenomenon called aliasing, caused by fact
that pixels are integer coordinate points.




Use anti-aliasing routines to smooth out display of a line
by adjusting pixels intensities along the line path
34
Another raster effect
Line B
Line A
Both lines plotted with the same number of pixels, but
the diagonal line is longer than the horizontal line
Visual effect is diagonal line appears less thick.

If the intensity of each pixel is I, then the intensity per unit
length of line A is I, whereas for line B it is only I/\2; this
discrepancy is easily detected by the viewer.
35
Circle Generating Algorithms
Circles and ellipses are common components in
many pictures.
Circle generation routines are often included in
packages.
36
Circle Equations
Polar form
x = rCosu
y = rSinu (r = radius of circle)
u
P=(rCosu, rSinu)
rSinu)
rCosu)
x
y
r
37
Drawing a circle
Disadvantages
To find a complete circle u varies from 0 to
360
The calculation of trigonometric functions
is very slow.

u = 0
while (u < 360)
x = rCosu
y = rSinu
setPixel(x,y)
u = u + 1
end while
38
Cartesian form
Use Pythagoras theorem
x
2
+ y
2
= r
2
x
r
y
y
x
x
y
r
( )
2 2
, P x r x =
39
Circle algorithms
Step through x-axis to determine y-values
Disadvantages:
Not all pixel filled in
Square root function is very slow
40
Circle Algorithms
Use 8-fold symmetry and only compute pixel
positions for the 45 sector.
45
(x, y)
(y, x)
(-x, y)
(y, -x)
(x, -y) (-x, -y)
(-y, x)
(-y, -x)
41
Bresenhams Circle Algorithm
General Principle
The circle function:


and

Consider only
45 u 90
2 2 2
( , )
circle
f x y x y r = +
if (x,y) is inside the circle boundary
if (x,y) is on the circle boundary
if (x,y) is outside the circle boundary
0
( , ) 0
0
circle
f x y
<

= =

>

42
Bresenhams Circle Algorithm
p
1
p
3
p
2
D(s
i
)

D(t
i
)

After point p
1
, do we choose p
2
or p
3
?
y
i
y
i
- 1
x
i

x
i
+ 1
r
43
Bresenhams Circle Algorithm
Define: D(s
i
) = distance of p
3
from circle
D(t
i
) = distance of p
2
from circle

i.e. D(s
i
) = (x
i
+ 1)
2
+ y
i
2
r
2
[always +ve]
D(t
i
) = (x
i
+ 1)
2
+ (y
i
1)
2
r
2
[always -ve]

Decision Parameter p
i
= D(s
i
) + D(t
i
)
so if p
i
< 0 then the circle is closer to p
3
(point above)
if p
i
0 then the circle is closer to p
2
(point below)

44
The Algorithm
x
0
= 0
y
0
= r
p
0
= [1
2
+ r
2
r
2
] + [1
2
+ (r-1)
2
r
2
] = 3 2r

if p
i
< 0 then
y
i+1
= y
i
p
i+1
= p
i
+ 4x
i
+ 6

else if p
i
0 then
y
i+1
= y
i
1
p
i+1
= p
i
+ 4(x
i
y
i
) + 10

Stop when x
i
y
i
and determine symmetry
points in the other octants
x
i+1
= x
i
+ 1
45
Example
10

9

8

7

6

5

4

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10
i p
i
x
i
, y
i
0 -17 (0, 10)
1 -11 (1, 10)
2 -1 (2, 10)
3 13 (3, 10)
4 -5 (4, 9)
5 15 (5, 9)
6 9 (6, 8)
7 (7,7)
r = 10
p
0
= 3 2r = -17
Initial point (x
0
, y
0
) = (0, 10)
46
Exercises
Draw the circle with r = 12 using the
Bresenham algorithm.

Draw the circle with r = 14 and center at
(15, 10).
47
Decision Parameters
Prove that if p
i
< 0 and y
i+1
= y
i
then
p
i+1
= p
i
+ 4x
i
+ 6

Prove that if p
i
0 and y
i+1
= y
i
1 then
p
i+1
= p
i
+ 4(x
i
y
i
) + 10

48
Advantages of Bresenham circle
Only involves integer addition, subtraction
and multiplication
There is no need for squares, square roots
and trigonometric functions
49
Midpoint Circle Algorithm
y
i
y
i
-1
x
i
x
i
+1 x
i
+2
Midpoint
x
2
+ y
2
r
2
= 0
Assuming that we have just plotted the pixels at (x
i
, y
i
).
Which is next? (x
i
+1, y
i
) OR (x
i
+1, y
i
1).
- The one that is closer to the circle.
50
Midpoint Circle Algorithm
The decision parameter is the circle at the midpoint
between the pixels y
i
and y
i
1.





If p
i
< 0, the midpoint is inside the circle and the pixel
y
i
is closer to the circle boundary.
If p
i
0, the midpoint is outside the circle and the pixel
y
i
- 1 is closer to the circle boundary.

1
2
2 2 2
1
2
( 1, )
( 1) ( )
i circle i i
i i
p f x y
x y r
= +
= + +
51
Decision Parameters
Decision Parameters are obtained using
incremental calculations



OR



where y
i+1
is either y
i
or y
i
-1 depending on the sign of p
i

1
1 1 1 2
2 2 2
1
1 2
( 1, )
( 2) ( )
i circle i i
i i
p f x y
x y r
+ + +
+
= +
= + +
2 2 2
1 1 1
2( 1) ( ) ( ) 1
i i i i i i i
p p x y y y y
+ + +
= + + + +
Note:
x
i+1
= x
i
+1
52
The Algorithm
1. Initial values:- point(0,r)
x
0
= 0
y
0
= r
2. Initial decision parameter

3. At each x
i
position, starting at i = 0, perform the
following test: if p
i
< 0, the next point is (x
i
+ 1, y
i
) and
p
i+1
= p
i
+ 2x
i+1
+ 1
If p
i
0, the next point is (x
i
+1, y
i
-1) and
p
i+1
= p
i
+ 2x
i+1
+ 1 2y
i+1
where 2x
i+1
= 2x
i
+ 2 and 2y
i+1
= 2y
i
2
4. Determine symmetry points in the other octants
5. Move pixel positions (x,y) onto the circular path centered
on (x
c
, y
c
) and plot the coordinates: x = x + x
c
, y = y + y
c
6. Repeat 3 5 until x y
move circle origin at (0,0) by
x = x x
c
and y = y y
c
2 2
5 1 1
0 2 2 4
(1, ) 1 ( )
circle
p f r r r r = = + =
53
Example
10

9

8

7

6

5

4

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10
i p
i
x
i+1
, y
i+1
2x
i+
1
2y
i+
1
0 -9 (1, 10) 2 20
1 -6 (2, 10) 4 20
2 -1 (3, 10) 6 20
3 6 (4, 9) 8 18
4 -3 (5, 9) 10 18
5 8 (6, 8) 12 16
6 5 (7, 7)
r = 10
p
0
= 1 r = -9 (if r is integer round p
0
= 5/4 r to integer)
Initial point (x
0
, y
0
) = (0, 10)
54
Exercises
Draw the circle with r = 12 using the
Midpoint-circle algorithm

Draw the circle with r = 14 and center at
(15, 10).
55
Exercises
Prove that if p
i
< 0 and y
i+1
= y
i
then
p
i+1
= p
i
+ 2x
i+1
+ 1

Prove that if p
i
0 and y
i+1
= y
i
-1 then
p
i+1
= p
i
+ 2x
i+1
+ 1 2y
i+1

56
Midpoint function
void plotpoints(int x, int y)
{
setpixel(xcenter+x, ycenter+y);
setpixel(xcenter-x, ycenter+y);
setpixel(xcenter+x, ycenter-y);
setpixel(xcenter-x, ycenter-y);
setpixel(xcenter+y, ycenter+x);
setpixel(xcenter-y, ycenter+x);
setpixel(xcenter+y, ycenter-x);
setpixel(xcenter-y, ycenter-x);
}

void circle(int r)
{
int x = 0, y = r;
plotpoints(x,y);
int p = 1 r;
while (x<y) {
x++;
if (p<0) p += 2*x + 1;
else {
y--;
p += 2*(x-y) + 1;
}
plotpoints(x,y);
}
}
57
Ellipse-Generating Algorithms
Ellipse A modified circle whose radius varies from a
maximum value in one direction (major axis) to a minimum
value in the perpendicular direction (minor axis).
P=(x,y) F
1
F
2
d
1
d
2
The sum of the two distances d
1
and d
2
, between the fixed
positions F
1
and F
2
(called the foci of the ellipse) to any
point P on the ellipse, is the same value, i.e.
d
1
+ d
2
= constant
58
Ellipse Properties
Expressing distances d
1
and d
2
in terms of the focal
coordinates F
1
= (x
1
, x
2
) and F
2
= (x
2
, y
2
), we have:







Cartesian coordinates:

Polar coordinates:
2 2 2 2
1 1 2 2
( ) ( ) ( ) ( ) constant x x y y x x y y + + + =
r
y
r
x
2
2
1
c c
x y
x x y y
r r
| |
| |

+ =
|
|
|
\ .
\ .
cos
sin
c x
c y
x x r
y y r
u
u
= +
= +
59
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)
(x, -y)
(-x, -y)
r
x
r
y
60
Ellipse Algorithms
Decision parameter:
2 2 2 2 2 2
( , )
ellipse y x x y
f x y r x r y r r = +
1
Slope = -1
r
x
r
y 2
0 if ( , ) is inside the ellipse
( , ) 0 if ( , ) is on the ellipse
0 if ( , ) is outside the ellipse
ellipse
x y
f x y x y
x y
<

= =

>

2
2
2
2
y
x
r x
dy
Slope
dx r y
= =
61
Ellipse Algorithms
Starting at (0, r
y
) we take unit steps in the x direction until
we reach the boundary between region 1 and region 2.
Then we take unit steps in the y direction over the
remainder of the curve in the first quadrant.
At the boundary


therefore, we move out of region 1 whenever
1
Slope = -1
r
x
r
y 2
2 2
1 2 2
y x
dy
r x r y
dx
= =
2 2
2 2
y x
r x r y >
62
Midpoint Ellipse Algorithm
y
i
y
i
-1
x
i
x
i
+1 x
i
+2
Midpoint
Assuming that we have just plotted the pixels at (x
i
, y
i
).
The next position is determined by:
1
2
2 2 2 2 2 2
1
2
1 ( 1, )
( 1) ( )
i ellipse i i
y i x i x y
p f x y
r x r y r r
= +
= + +
If p1
i
< 0 the midpoint is inside the ellipse y
i
is closer
If p1i 0 the midpoint is outside the ellipse y
i
1 is closer
63
Decision Parameter (Region 1)
At the next position [x
i+1
+ 1 = x
i
+ 2]



OR


where y
i+1
= y
i
or y
i+1
= y
i
1
1
1 1 1 2
2 2 2 2 2 2
1
1 2
1 ( 1, )
( 2) ( )
i ellipse i i
y i x i x y
p f x y
r x r y r r
+ + +
+
= +
= + +
2 2 2 2 2 2
1 1
1 1 2 2
1 1 2 ( 1) ( ) ( )
i i y i y x i i
p p r x r r y y
+ +
(
= + + + +

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



Use only addition and subtraction by obtaining


At initial position (0, r
y
)
2 2
1
2 2 2
1 1
2 if 1 0
2 2 if 1 0
y i y i
y i y x i i
r x r p
increment
r x r r y p
+
+ +

+ <

=

+ >

2 2
2 and 2
y x
r x r y
2
2 2
2 2 2 2 2
1 1
0 2 2
2 2 2
1
4
2 0
2 2
1 (1, ) ( )

y
x x y
ellipse y y x y x y
y x y x
r x
r y r r
p f r r r r r r
r r r r
=
=
= = +
= +
65
Region 2
Over region 2, step in the negative y direction and midpoint is
taken between horizontal pixels at each step.
y
i
y
i
-1
x
i
x
i
+1 x
i
+2
Midpoint
Decision parameter:
1
2
2 2 2 2 2 2
1
2
2 ( , 1)
( ) ( 1)
i ellipse i i
y i x i x y
p f x y
r x r y r r
= +
= + +
If p2
i
> 0 the midpoint is outside the ellipse x
i
is closer
If p2i 0 the midpoint is inside the ellipse x
i
+ 1 is closer
66
Decision Parameter (Region 2)
At the next position [y
i+1
1 = y
i
2]



OR


where x
i+1
= x
i
or x
i+1
= x
i
+ 1
1
1 1 1 2
2 2 2 2 2 2
1
1 2
2 ( , 1)
( ) ( 2)
i ellipse i i
y i x i x y
p f x y
r x r y r r
+ + +
+
= +
= + +
2 2 2 2 2
1 1
1 1 2 2
2 2 2 ( 1) ( ) ( )
i i x i x y i i
p p r y r r x x
+ +
(
= + + + +

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





At initial position (x
0
, y
0
) is taken at the last
position selected in region 1
2 2
1
2 2 2
1 1
2 if 2 0
2 2 if 2 0
x i x i
y i x i x i
r y r p
increment
r x r y r p
+
+ +

+ >

=

+ s

1
0 0 0 2
2 2 2 2 2 2
1
0 0 2
2 ( , 1)
( ) ( 1)
ellipse
y x x y
p f x y
r x r y r r
= +
= + +
68
Midpoint Ellipse Algorithm
1. Input r
x
, r
y
, and ellipse center (x
c
, y
c
), and obtain the first
point on an ellipse centered on the origin as
(x
0
, y
0
) = (0, r
y
)
2. Calculate the initial parameter in region 1 as


3. At each x
i
position, starting at i = 0, if p1
i
< 0, the next
point along the ellipse centered on (0, 0) is (x
i
+ 1, y
i
) and


otherwise, the next point is (x
i
+ 1, y
i
1) and


and continue until
2 2 2
1
0 4
1
y x y x
p r r r r = +
2 2
1 1
1 1 2
i i y i y
p p r x r
+ +
= + +
2 2 2
1 1 1
1 1 2 2
i i y i x i y
p p r x r y r
+ + +
= + +
2 2
2 2
y x
r x r y >
69
Midpoint Ellipse Algorithm
4. (x
0
, y
0
) is the last position calculated in region 1. Calculate the
initial parameter in region 2 as

5. At each y
i
position, starting at i = 0, if p2
i
> 0, the next point
along the ellipse centered on (0, 0) is (x
i
, y
i
1) and

otherwise, the next point is (x
i
+ 1, y
i
1) and

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 (x
c
, y
c
) and plot the coordinate values
x = x + x
c
, y = y + y
c
2 2 2 2 2 2
1
0 0 0 2
2 ( ) ( 1)
y x x y
p r x r y r r = + +
2 2
1 1
2 2 2
i i x i x
p p r y r
+ +
= +
2 2 2
1 1 1
2 2 2 2
i i y i x i x
p p r x r y r
+ + +
= + +
70
Example
i p
i
x
i+1
, y
i+1
2r
y
2
x
i+1
2r
x
2
y
i+1
0 -332 (1, 6) 72 768
1 -224 (2, 6) 144 768
2 -44 (3, 6) 216 768
3 208 (4, 5) 288 640
4 -108 (5, 5) 360 640
5 288 (6, 4) 432 512
6 244 (7, 3) 504 384
r
x
= 8 , r
y
= 6
2r
y
2
x = 0 (with increment 2r
y
2
= 72)
2r
x
2
y = 2r
x
2
r
y
(with increment -2r
x
2
= -128)
Region 1
(x
0
, y
0
) = (0, 6)
2 2 2
1
0 4
1 332
y x y x
p r r r r = + =
Move out of region 1 since
2r
y
2
x > 2r
x
2
y
71
Example
6

5

4

3

2

1

0

0 1 2 3 4 5 6 7 8
i p
i
x
i+1
, y
i+1
2r
y
2
x
i+1
2r
x
2
y
i+1
0 -151 (8, 2) 576 256
1 233 (8, 1) 576 128
2 745 (8, 0) - -
Region 2
(x
0
, y
0
) = (7, 3) (Last position in region 1)
1
0 2
2 (7 , 2) 151
ellipse
p f = + =
Stop at y = 0
72
Exercises
Draw the ellipse with r
x
= 6, r
y
= 8.
Draw the ellipse with r
x
= 10, r
y
= 14.
Draw the ellipse with r
x
= 14, r
y
= 10 and
center at (15, 10).
73
Midpoint Ellipse Function
void ellipse(int Rx, int Ry)
{
int Rx2 = Rx * Rx, Ry2 = Ry * Ry;
int twoRx2 = 2 * Rx2, twoRy2 = Ry2 * Ry2;
int p, x = 0, y = Ry;
int px = 0, py = twoRx2 * y;

ellisePlotPoints(xcenter, ycenter, x, y);
// Region 1
p = round(Ry2 (Rx2 * Ry) + (0.25 * Rx2));
while (px < py) {
x++;
px += twoRy2;
if (p < 0) p += Ry2 + px;
else {
y--;
py -= twoRx2;
p += Ry2 + px py;
}
ellisePlotPoints(xcenter, ycenter, x, y);
}
// Region 2
p = round(Ry2 * (x+0.5) * (x+0.5) + Rx2 * (y-1)*(y-1) Rx2 * Ry2;
while (y > 0) {
y--;
py -= twoRx2;
if (p > 0) p += Rx2 py;
else {
x++;
px += twoRy2;
p += Rx2 py + px;
}
ellisePlotPoints(xcenter, ycenter, x, y);
}
}

Potrebbero piacerti anche