Sunteți pe pagina 1din 33

Scan converting lines

circles and ellipses


Line drawing
Description: Given the specification of the straight
line, find the collection of the addressable pixels
which more closely approximates this line
Goal: (Not all of them are acheivable with the discrete
space of a raster device) .
Straight line should appear straight.
Lines should start and end accurately.
Lines should have constant brightness.
Lines should be drawn as rapidly as possible.
Problems
How do we determine which pixel to illuminate to
satisfy the above goals?

Vertical, horizontal and lines with slope with +/- 1 are


easy to draw.

Others create problems: Stair casing/ jaggies/aliasing

Quality of the line depends on the location of the


pixels and their brightness.
Direct solution
Solve y=mx+b, where (0,b) is the y-intercept and m is
the slope.
Go from X0 to X1
Calculate round(y) from the equation.
Take an example, b=1 [starting point is (0,1)] and
m=3/5.
Then X=1, Y=2= ROUND (8/5)
X=2, Y=2= ROUND (11/5)
X=3, Y=3= ROUND (14/5)
X=4, Y=3= ROUND (17/5)
X=5, Y=4= ROUND (20/5)
Bresenharm’s line drawing
algorithm (Midpoint line algorithm)
• Mathematical description:

A = the distance of pixels


which are lying above
the true line
A B = the distance of pixels
True line
which are lying below
B the true line
• Let us define the decision variable as
di = B – A
If di < 0 then it implies that B > A
i:e pixel below the true line is closer to the
expected line. If di is greater than or equal
to zero then the pixel above the true line is
closet. Hence, if di <0, then we can
increment only x value so that we get a
pixel on the line, and when di >0 increment
both the values of x and y.
Algorithm
1. Input the two line end points and store the left endpoint in
(x0,y0).
2. Load (x0,y0), into the frame buffer, that is, plot the first
point.
3. Calculate constants ∆x, ∆y, 2∆y, and 2∆y - 2∆x, and obtain
the starting value for the decision parameter as
d0 = 2∆y – ∆x.
4. At each xk along the line, starting at k=0, perform the
following test:
if dk <0 then the next point to plot is (xk+1, yk) and
dk+1 = dk+ 2∆y
otherwise the next point to plot is (xk+1, yk+1) and
dk+1 = dk+ 2∆y- 2∆x
5. Repeat step 4 ∆x times.
Problem: Construct a line with
endpoints (6,9) and (17,15).
16

15 k dk (x ,y)
14

13
0
12
1
11
2
10
.
9
.
6 7 8 9 10 11 12 13 14 15 16 17

So do it……..
Note:
• This method is well suited for the line having the
slope less than 45º.
• When the magnitude of the slope is lesser than 1, x
incremented by one, and the decision variable is
made use to find the incremental value of y.
• When the slope is greater than 1, y is incremented
by one and x is incremented based on decision
variable.
• Line is constructed from left to right. For the right
to left appropriate directions need to be reversed.
DDA line algorithm : Disadvantages
• Key disadvantage:
it relies on floating point operations to compute
pixel positions.

• Implications:
 Computationally inefficient because floating-
point operations are slow.
 Round-off errors accumulate, producing incorrect
line drawings (e.g., if m is rounded to 0.9 even
though it is equal to 0.99, lines of length > 10 will
be drawn inaccurately)
Circle algorithm
Starting with the equation of the circle

We could use this equation to calculate the position of points on


a circle by stepping along the x axis in unit steps from x0-r to
x0+r and calculating the corresponding y values at each
position as :
• This works, but is inefficient coz it involves
multiplication and square root operations. It
also creates large gaps in the circle for values
of x values close to r.
Exploiting Symmetry
• Circle can be drawn by
two ways namely :
polynomial method and
trigonometric method.
(y, -x) (y, x)
• Take advantage of
symmetries to minimize
cases (slopes) and amount
(-x, y) (x, y)
of curve drawn. We can
generate all pixel positions
around a circle by (-x, -y) (x, -y)
calculating only the points
within the sector from x=0 (-y, -x) (-y, x)
to x=y
We can actually fix this problem with
slopes greater than one by taking
advantage symmetry.

In addition to the symmetries about the x


and y axes, a circle is also symmetric
about the lines x = y and x = -y.

Thus, for every point (x,y) we can plot 7


other points:
(-x,y) (-x,-y) (x,-y) (y,x) (-y,x) (-y,-x) (y,-x)
The most efficient method as it avoids the use of trigonometry and
Power functions. It performs the computation with integers and
Simple + and -.
Bresenhams circle drawing algorithm
 1. read the radius of the circle say r
 2. d=3-2r (initial decision variable)
x=0, y=r
 3. plot the initial four points (x,y),(x,-y), (y,x)and
(-y,-x)
 4. if (d<0) then
d=d+4x+6 and x=x+1
Else d=d+4(x-y)+10 then x=x+1 & y=y-1

 Check if x>y stop or else goto step 4 until the


condition is satisfied.
Draw a circle whose radius is 8 units
and centre is (0,0)
• Step1:
Set (h,k) = (0,0)
x=0 and y= 8
d= 3-2r = -13
• Step 2:
check , if x>y then stop
0>8, which is false.
• Step 3: plot the 8 points,
(x+h, y+k)= (0,8)
(y+h, x+k)=(8,0)
(-y+h, x+k)=(-8,0)
(-x +h, y+k)=(0,8)
(-x +h, -y+k)=(0,-8)
(-y +h, -x+k)=(-8,0)
(y +h, -x+k)=(8,0)
(x +h, -y+k)=(0,-8)
• Step 4: d= -13
since d<0 (-13<0)
d= d+4x+6
= -7
And x=x+1
=0 +1
Now , again check if x>y
1>8, answer will be no
Again plotting 8 points while taking x=1
x=1, h=0 , k=0, y=8
Points to be plotted are as follows
(1,8), (8,1), (-8,1), (-1,8), (-1,-8), (-8,-1), (8,-1), (1,-8)
Now checking decision parameter,
Since d<0 i:e -7<0
d= d+4x+6
=-7+4(1)+6 =3
x = x+1=1+1=2
Now, check if x>y, no.
i:e 2>8
Again plot 8 points taking x=2, y=8, h=0,k=0
(2,8), (8,2), (-8,2), (-2,8), (-2,-8),
(-8,-2), (8,-2), (2,-8)
Now d ≥ 0 3 ≥0
d=d+4(x-y)+10
=3+4(2-8)+10
= -11
x= x+1
= 2+1 = 3
y = y-1= 8-1= 7
Now x>y No.
Again plotting 8 points x=3, y=7, h=0, k=0
(3,7), (7,3), (-7,3), (-3,7), (-3,-7), (-7,-3), (7,-3), (3,-7)
d= -11<0
d= d+4x+6
= -11+4(3)+6 = 7
x= x+1 = 3+1 = 4
Check if x>y which is false
So again computing 8 points with x=4
(4,7), (7,4), (-7,4), (-4,7), (-4,-7), (-7,-4), (7,-4),
(4,-7)
now d ≥ 0 i:e 7 >0
d= d+4(x-y)+10
= 7+4(4-8)+10 = 1
x= x+1 = 4+1 = 5
y=y-1 =7-1 = 6
Check if x>y which is false
So again computing 8 points with x=5,y=6
(5,6), (6,5), (-6,5), (-5,6), (-5,-6), (-6,-5), (6,-5),
(5,-6)
now d ≥ 0 i:e d=1
d= d+4(x-y)+10
= 1+4(5-6)+10 = 7
x= x+1 = 5+1 = 6
y=y-1 =6-1 = 5
Checking the condition if x>y i:e 6>5
hence we stop
So again computing 8 points with x=5,y=6
(5,6), (6,5), (-6,5), (-5,6), (-5,-6), (-6,-5), (6,-5),
(5,-6)
10
8
6
4
2
0 Series1
-10 -5 0 5 10
-2
-4
-6
-8
-10

S-ar putea să vă placă și