Trang 7 Problems■ It seems to work okay for lines with a slope of 1 or less■ Doesn’t work well for lines with slope greater than 1■ Lines become more discontinuous in appearance■ We must
Trang 1Lesson 3
Basic entity algorithms
Trinh Thanh Trung School of ICT, HUST
Trang 3Rasterization: Background
■ Converting a continuous object in the world into
a discrete object in the computer
■ We need to lit the pixels instead of drawing a continuous line
Trang 4Line representation
Trang 7discontinuous in
appearance
than 1 pixel per column to make it work
Trang 8DDA algorithm
■ DDA = Digital Differential Analyser
■ Treat line as parametric equation in t
Trang 9DDA algorithm: Pseudo code
ddaline(x1, y1, x2, y2)
Trang 10DDA algorithm: General algorithms
■ Choose appropriate value
for the number of steps (n)
Trang 11DDA algorithm: General algorithms
ddaline(int x1, int y1, int x2, int y2)
Trang 12DDA Algorithm: Cons
■ Need a lot of floating point arithmetic
□ 2 ‘round’s and 2 adds per pixel
■ Can we use only integer arithmetic?
□ Easier to implement in hardware
Trang 13Bresenham algorithm
■ Developed in 1962 at IBM
■ Uses only integer addition, subtraction and bit
shifting, all of which are very cheap operations in
standard computer architectures
0 1
2
d2 d1
■ Chooses the integer y
corresponding to the pixel
center that is closest to the
ideal (fractional) y for the
same x; on successive
columns y can remain the
same or increase by 1
Trang 14Bresenham algorithm: pseudo code
Trang 15Mid-point algorithm
■ At step p, assume pixel (x p , y p ) was lit
■ Check where the line intersects with x= x p +1
■ Lit (x p+1 , y p ) or (x p+1 , y p+1 ) , whichever is
■ closer to the intersection
Trang 16i i i
i
i i i
i
y x c
by ax
y x c
by ax
y x c
by ax
,0
,0
,0
++
++
=+
above line below line
Trang 17Mid-point algorithm
■ Assume slope is less than 1 and current pixel is (xi, yi)
■ Next mid-point will be (xi + 1, yi + ½)
■ We calculate di (called decision variable)
+
=
2 1 1
Trang 18Choices for Current pixel
Choices for Next pixel
M A
B
Trang 19Mid-point algorithm
■ If A is chosen: We update the decision variable
b a d
c y
b x
a d
y x
i
i i
i i
i
++
2
3,
Previous Pixel
(xi,yi)
Choices for Current pixel
Choices for Next pixel
M A
B
Trang 20Mid-point algorithm
■ If B is chosen: We update the decision variable
Previous Pixel
(xi,yi)
Choices for Current pixel
Choices for Next pixel
M A
B
a d
c y
b x
a d
y x
i
i i
i i
2
1,
Trang 21Mid-point algorithm: Pseudo code
d = d+2*(dy-dx)
x = x+1
y = y+1 endif
Trang 22Mid-point algorithm: Summary
■ Choose between 2 pixels at each step based upon the sign of a decision variable
■ Update the decision variable based upon which pixel is chosen
■ Start point is simply first endpoint (xi,yi)
■ Need to calculate the initial value for d
Trang 23What if slope is not
Trang 24Circle & elipse representation
Trang 25Circle representation
■ Use mid-point algorithm for 1/8 circle, then use symmetry
Trang 26Circle representation
■ Implicit form of a circle
■ With d i is the decision variable at a specific point
circle
on is,
if 0
circleinside
is,
if 0
i i
i i i
y x
y x
y x d
Trang 27Circle representation
■ As with the line, we determine the value of the decision variable by substituting the mid-point of the next pixel into the implicit form of the circle:
■ If d i < 0 we choose pixel A otherwise we choose pixel B
□ Note: we currently assume the circle is centered at the origin
2 2
+
=
Trang 28Circle representation
■ Again, as with the line algorithm, the choice of A
or B can be used to determine the new value of d i+1
■ If A chosen then next midpoint has the following decision variable:
■ Otherwise if B is chosen then the next decision variable is given by:
3 2
2
1 2
2
1 ,
2 2
1
+ +
i i
i i
i
x d
r y
x d
y x
5 2
2
2
3 2
2
3 ,
2 2
1
+
− +
i
i i
i i
i
y x
d
r y
x d
y x
Trang 29Circle representation
■ If we assume that the radius is an integral value, then the first pixel drawn is (0, r) and the initial
value for the decision variable is given by:
■ Although the initial value is fractional, we note that all other values are integers.
r
r r
r d
4
11
2
1,
r
d0 =1 −
Trang 30Circle representation: pseudo code
d = d+2*(x-y)+5
x = x+1
y = y-1 end if
SetPixel(cx+x,cy+y)
End while
initialisation
choose B choose A
Translate to the circle center stop at diagonal end of octant
Trang 31Ellipse representation
■ Use mid-point algorithm
■ Implicit form of an ellipse
□ 2a is the length of the major axis along the x axis.
□ 2b is the length of the minor axis along the y axis.
F x y = b x + a y − a b =
■ For simplicity, we draw only
the arc of the ellipse that lies in
the first quadrant, the other
three quadrants can be drawn
by symmetry
Trang 32Ellipse representation
■ Firstly we divide the quadrant into two regions
■ Boundary between the two regions is
□ the point at which the curve has a slope of -1
□ the point at which the gradient vector has the i and j components of equal magnitude
Trang 33Character representation
Trang 35Bitmap character: structure
Trang 36Vector font
■ Also called True Type Font (ttf)
■ Flexible, suitable for any screen
Trang 37Any questions?
Trang 38Lecture notes provided by School of Information and
Communication Technology, Hanoi University of Science and
Technology.
Composed by Huynh Quyet Thang, Le Tan Hung, Trinh Thanh Trung and others
Edited by Trinh Thanh Trung
Special thanks to all the people who made and released these awesome resources for free:
■ Presentation template by SlidesCarnival
■ Photographs by Death to the Stock Photo ( license )
■ Diverse device hand photos by Facebook Design Resources