vits cse1 midpoint circle & ellipse generarting algorithms circle generating algorithm...
Post on 05-Jan-2016
265 Views
Preview:
TRANSCRIPT
VITS CSE 1
MIDPOINT CIRCLE & ELLIPSE GENERARTING
ALGORITHMS
Circle Generating Algorithm Properties of circle Midpoint circle algorithm
Ellipse-generating algorithm Properties of ellipse Midpoint ellipse algorithm
VITS CSE 2
Circle Generating Algorithm Properties of Circle
Circle is defined as the set of points that are all at a given distance r from a center point (xc,yc)
For any circle point (x,y), the distance relationship is expressed by the Pythagorean theorem in Cartesian coordinate as:
ryc
өө
(x,y)
xc
222 )()( ryyxx cc
VITS CSE 3
Circle Generating Algorithm We could use this equation to calculate the
points on a circle circumference by stepping along x-axis in unit steps from xc–r to xc+r and calculate the corresponding y values as
22 )( xxryy cc
VITS CSE 4
Circle Generating Algorithm The problems:
Involves many computation at each stepi.e., square root and additions /subtractions
Spacing between plotted pixel positions is not uniform
Adjustment: interchanging x & y (step through y values and calculate x values)
Involves many computation too!
VITS CSE 5
Circle Generating Algorithm Another way:
Calculate points along a circular boundary using polar coordinates r and ө
x = xc + r cos ө
y = yc + r sin ө
Using fixed angular step size, a circle is plotted with equally spaced points along the circumference
Problem: trigonometric calculations are still time consuming
VITS CSE 6
Symmetry of Circles (8-way symmetry) Proposed Approach:symmetry of circles
Shape of the circle is similar in each quadrant
i.e. if we determine the curve positions in the 1st quadrant, we can generate the circle section in the 2nd quadrant of the xy plane (the 2 circle sections are symmetric with respect to the y axis)
The circle section in the 3rd and 4th quadrant can be obtained by considering symmetry about the x axis
One step further symmetry between octants
VITS CSE 7
8-way symmetry Circle sections in adjacent octants within 1
quadrant are symmetric with respect to the 45° line dividing the 2 octants
Calculation of a circle point (x, y) in 1 octant yields the circle points for the other 7 octants
450
(y,x)(-y,x)
(x,y)
(x,-y)
(y,-x)(-y,-x)
(-x,-y)
(-x,y)
VITS CSE 8
Midpoint Circle Algorithm As in raster algorithm, we sample at unit
intervals & determine the closest pixel position to the specified circle path at each step
For a given radius, r and screen center position (xc,yc) , we can set up our algorithm to calculate pixel positions around a circle path centered at the coordinate origin (0,0)
Each calculated position (x, y) is moved to its proper screen position by adding xc to x and yc
to y
VITS CSE 9
Midpoint Circle Algorithm Along a circle section from x=0 to x=y in the 1st
quadrant, the slope (m) of the curve varies from 0 to -1.0
i.e. we can take unit steps in the +ve x direction over the octant & use decision parameter to determine which 2 possible positions is vertically closer to the circle path
Positions in the other 7 octants are obtained by symmetry
VITS CSE 10
< 0, if (x,y) is inside the circle boundary
fcirc(x,y) = 0, if (x,y) is on the circle boundary
> 0, if (x,y) is outside the circle boundary
Midpoint Circle Algorithm
To apply the midpoint method, we define a circle function as
fcirc(x,y) = x2 + y2 - r2
The relative positions of any point (x,y) can be determined by checking the sign of the circle function:
VITS CSE 11
Midpoint Circle Algorithm
Consider current position (xk, yk)
next pixel position is either(xk+1, yk) or (xk+1, yk-1)?
xk
yk
xk+1 xk+2
yk-1
yk-1
yk
Circle path
xk
Midpoint
VITS CSE 12
Midpoint Circle Algorithm Our decision parameter is the earlier circle
function evaluated at the mid point between the 2 pixels
< 0: midpoint is inside the circle; plot (xk+1, yk)
+ve: midpoint is outside the circle; plot
(xk+1, yk-1)
Successive decision parameters are obtained using incremental calculation
22
212
21
1
,1
ryx
yxfp
kk
kkk
pk
VITS CSE 13
Midpoint Circle Algorithm To ensure things are as efficient as possible
we can do all of our calculations incrementally
First consider:
or
where yk+1 is either yk or yk-1 depending on
the sign of pk
22
12
111
21]1)1[(
21,1
ryx
yxfp
kk
kkcirck
1)()()1(2 122
11 kkkkkkk yyyyxpp
VITS CSE 14
Midpoint Circle Algorithm
Initial decision parameter is obtained by evaluating the circle function at the start position (x0, y0) = (0,r)
p0 = f(1, r–1/2) = 12 + (r – 1/2)2 – r2
p0 = 5/4 – r
If the radius is specified as an integer, we can simple round p0 to p0 = 1 – r
Then if pk < 0 then the next decision variable is given as:
If pk > 0 then the decision variable is:
12 11 kkk xpp
1212 11 kkkk yxpp
VITS CSE 15
Midpoint Circle Algorithm1. Input radius, r, and circle center (xc, yc), then set the coordinates
for the 1st point on the circumference of a circle centered at the origin as (x0, y0) = (0,r)
2. Calculate initial value of decision parameter:
3. At each xk, starting at k = 0, test the value of pk :
If pk < 0, next point will be (xk+1, yk) and
else, next point will be (xk+1, yk – 1) and
where 2xk+1 = 2xk + 2 and 2yk+1 = 2yk – 2
4. Determine symmetry points in the other 7 octants.
5. Get the actual point for circle centered at (xc,yc) that is (x+xc, y+yc).
6. Repeat step 3 to 5 until x y.
rp 4
50
,12 11 kkk xpp
.212 111 kkkk yxpp
VITS CSE 16
Try this out!
Given a circle radius r=10, demonstrate the midpoint circle algorithm by determining positions along the circle octant in the 1st quadrant
p0 = ?
(x0,y0) = ?
k pk (xk+1,yk+1) 2xk+1 2yk+1
0
1
2
3
4
5
6
VITS CSE 17
Plot pixel positions
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10
VITS CSE 18
Try this again!
Given a circle with r=8, calculate each pixel positions along the circumference at the 2nd quadrant
VITS CSE 19
Ellipse Generating Algorithm Ellipse – an elongated circle.
A modified circle whose radius varies from a maximum value in one direction to a minimum value in the perpendicular direction.
A precise definition in terms of distance from any point on the ellipse to two fixed position, called the foci of the ellipse.
The sum of these two distances is the same value for all points on the ellipse.
P=(x,y)
F2
F1d2
d1
VITS CSE 20
Ellipse Generating Algorithm
If the distance of the two focus positions from any point P=(x, y) on the ellipse are labeled d1 and d2, the general equation of an ellipse:
d1 + d2 = constant
Expressing distance d1 and d2 in terms of the focal coordinates F1=(x1, y1) and F2=(x2, y2), we have constant2
22
22
12
1 yyxxyyxx
VITS CSE 21
Ellipse Generating Algorithm
However, we will only consider ‘standard’ ellipse:
1
22
y
c
x
c
r
yy
r
xxry
xc
rx
yc
VITS CSE 22
2-way symmetry
An ellipse only has a 2-way symmetry.
(x,y)
(x,-y)
(-x,y)
(-x,-y)
rx
ry
VITS CSE 23
Equation of an ellipse Consider an ellipse centered at the origin,
(xc,yc)=(0,0):
Ellipse function
…and its properties: fellipse(x,y) < 0 if (x,y) is inside the ellipse
fellipse(x,y) = 0 if (x,y) is on the ellipse
fellipse(x,y) > 0 if (x,y) is outside the ellipse
222222, yxxye rryrxryxf
1
22
yx r
y
r
x
VITS CSE 24
Midpoint Ellipse Algorithm Ellipse is different from circle. Similar approach with circle, different in
sampling direction. Slope of ellipse is: dy/dx = - 2ry
2x/2rx2y
Region 1: Sampling is at x direction Choose between (xk+1, yk), or (xk+1,
yk-1)
Move of region 1 if 2r2yx >= 2r2
xy Region 2:
Sampling is at y direction Choose between (xk, yk-1), or (xk+1,
yk-1)
Slope = -1
Region 1
Region 2
ry
rx
VITS CSE 25
Decision parameters
Region 1: Decision parameter
p1k < 0: midpoint is inside choose pixel (xk+1, yk)
p1k >= 0: midpoint is outside/on choose pixel (xk+1, yk-1)
21,11 kkek yxfp
Region 2:• Decision parameter
p2k <= 0:
• midpoint is inside/on
• choose pixel (xk+1, yk-1)
p2k > 0:
• midpoint is outside
• choose pixel (xk, yk-1)
1,2 21 kkek yxfp
VITS CSE 26
Midpoint Ellipse Algorithm1. Input rx, ry and ellipse center (xc, yc). Obtain the first point on an ellipse
centered on the origin (x0, y0) = (0, ry).
2. Calculate initial value for decision parameter in region 1 as:
3. At each xk in region 1, starting from k = 0, test p1k :
If p1k < 0, next point (xk+1, yk) and
else, next point (xk+1, yk-1) and
with 2ry2xk+1 = 2ry
2xk + 2ry2, 2rx
2yk+1 = 2rx2yk – 2rx
2
and repeat step 1 to 3 until 2ry2x 2rx
2y
2
4122
01 xyxy rrrrp
21
21 211 ykykk rxrpp
21
21
21 2211 ykxkykk ryrxrpp
VITS CSE 27
Midpoint Ellipse Algorithm4. Initial value for decision parameter in region 2:
where (x0, y0) is the last position calculate in region 1
5. At each yk in region 2, starting from k = 0, test p2k:
If p2k > 0, next point is (xk, yk-1) and
else, next point is (xk+1, yk-1) and
continue until y=0
21
21 222 xkxkk ryrpp
2220
22
21
02
0 12 yxxy rryrxrp
21
21
21 2222 xkxkykk ryrxrpp
VITS CSE 28
Midpoint Ellipse Algorithm
6. For both region determine symmetry points in the other 3 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
VITS CSE 29
Try this out!
Given input ellipse parameter rx=8 and ry=6, determine pixel positions along the ellipse path in the first quadrant using the midpoint ellipse algorithm
2ry2x = ?
2rx2y = 2rx
2ry = ?
p10 = ?
k p1k (xk+1,yk+
1)2ry
2 xk+1 2 rx2yk+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 52
6 244 (7,3) 504 384
k p2k (xk+1,yk+
1)2rv
2 xk+1 2 rx2yk+1
0 -151 (8,2) 576 256
1 233 (8,1) 576 128
2 745 (8,0) -- --
We now move out from R1 since2ry2 x> 2 rx
2y
For R2,initial point is(xo,yo)=(7,3)& p2o=f(7+1/2,2)=-151
VITS CSE 30
Plot pixel positions
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
top related