Các giải thuật sinh các thực thể cơ sở
Trang 1
Bai 2:
Cac giai thuat sinh các thực thể cơ sở
Le Tan Hung hunglt@it-hut.edu.vn
0913030731
(c) SE/FIT/HUT 2002
Giai thuat xây dựng các thực thê cơ sở
Giải thuật sinh đường thắng — Line
Giải thuật sinh đường tròn - Circle Giải thuat VanAken sinh Ellipse Giải thuật sinh đa giác Giải thuật sinh ký tự
(c) SE/FIT/HUT 2002
Rời rạc hoá điểm ảnh
(Scan Conversion rasterization)
m Là tiến trình sinh các đôi tượng hình học cơ sở bằng phương pháp xắp xi dya trên lưới phân giải của màn hình
m Tính chất các đối tượng cần đảm bảo :
m smooth continuous pass through specified points uniform brightness efficient
(c) SE/FIT/HUT 2002
Biéu dién doan thang
m Biểu diễn tường minh (-y1)/( x-xÙ) =(y2-+yD/(x2-xU)L PŒ; y;)
y=kxtrm
= k~(y2y1/G2-xl)
= m=yl-kxi
m Biểu diễn không tường minh (y2-yLhx - (x2-xUy + x2yl - xIy2 =0 Pu, yị)
hay rx + sy tt=Œ
= s=-(x2-x1)
= r=(y2-yl) va t= x2yl - xly2 li
m Biểu diễn tham biến 1
P(u) = PI + u(P2 - PI)
u [0,1]
X=xIl +u(x2-xl}
Y=yl+u(y2-yl)
(c) SE/FIT/HUT 2002
Thuat toan DDA
(Digital Differential Analizer)
Giải thuật thông thường
DrawLine(int x1,int y1, int x2,int y2, Thuattoan ddaline (x1, yl, x2, y2) xl, yl, x2, y2 : tọa độ 2 điểm đầu cuối
int color) k: hệ số góc
{ xy,m ‘bién
float y;
for (x=x1; x<=x2; x++) HET LST
y =yl + (x-x1)* (y2-y1)/(x2-x1) HH
WritePixel(x, Round(y), color ); putpixel(x,y);
}} while x<x2
Giải thuật DDA ea
eau! putpixel(round(x),round(y));
với 1=1,2,3
(c) SE/FIT/HUT 2002
Giai thuat Bresenham
= 1960 Bresenham thudéc IBM
m điệm gân với đường thăng dựa
trên độ phân giai hưu hạn
m loại bỏ được các phép toán
chia và phép toán làm tròn nhu ta da thay trong giaI thuật
m Xét doan thang vo10<k <1
2
(c) SE/FIT/HUT 2002
Trang 2
a I Giải thuật Bresenham —
Giai thuật Bresenham
d,=y-yi =k(xi 1) + b- yi NHI
h 0 h DA" = i
d,=yitl-y =yi+1-k(@i+1)-b = -2Ay(x;,1 - xi) + 2Ax(yi,, - yi)
yi+1 Nếu Pi<0=yi¡=yi + l
If d, <d,=>y;;=yI+ | ee bàn
Stee eee Pi,, = Pi- 2Ay + 2Ax
y chế HH Hị Hài |2 Ði m Nếu Pi >0=yi,¡=yi
= -2k(xi + 1) + 2yi-2b+1
Pi= AxD = Ax (dy - d rie:
XI va THÍ: HH, P, = -2Ay + Ax
(c) SE/FIT/HUT 2002 7 (c) SE/FIT/HUT 2002 8
Giải thuật trung điểm-Midpoint
m Jack Bresenham 1965 / Pitteway 1967
m= VanAken áp dụng cho việc sinh các đường
thăng và đường tròn 1985
m Các công thức đơn giản hơn, tạo được các điểm
tương tự như với Bresenham
" ae F (Œxi+ I, yi + 1⁄2) là trung điểm của đoạn
m Việc so sánh, hay kiểm tra M sẽ được thay
băng việc xét giả trị d
=m Nêu d>0 điểm B được chọn, y,.¡ = Vy,
" aL d<0 diem A duoc chon > y,,, =y;+
= Trong trường hợp d=0 chúng ta có thể
chọn điểm bất kỳ hoặc A, hoặc B
(c) SE/FIT/HUT 2002 9
Bresenham’s Algorithm:
Midpoint Algorithm
m Sử dụng phương pháp biểu diễn không tường minh
ax+by+c=0
ax, +by,+c=0 => (x,,y,} on line
ax, +by,+¢<0 > (x,, y,) above line
aX, +by, +¢> OS tị y,) below line
= Tai moi trung diém cia doan thang giá trị được tính là:
d, = a(x, +l}+b %, tệ te
= Ching ta gọi ở, là biến quyết định của bước thứ ¡
(c) SE/FIT/HUT 2002 10
Bresenham’s Algorithm:
Midpoint Algorithm
= Ifd, > 0 then chon diém A = trung diém tiếp theo sẽ có dang:
[x+2w+2] Bi mals, +2)+if 43}
=d.,+a+b d>0 d<0
(c) SE/FIT/HUT 2002 11
Bresenham’s Algorithm:
Midpoint Algorithm
= ifd, < 0 then chọn điểm B vả trung điểm mới la
Ề +2, y; 3) > d= ax, 240» +5) +e
a = Ay Faw TS ean
Ay
c=CAx
m Điểm đầu
X san + L Y start 1p) => aa i HT as 1)+0j Y start 7) +e
= [ax start 1D gq te}ba+s Tino
(c) SE/FIT/HUT 2002 12
Trang 3
Midpoint Line Algorithm
đx=12 - xỈ;
Ht " dy =y2- yl;
dy = y_end-y_start = Néu diém duge chon là B thi M sẽ tang
d = 2*dy-dx initialisation theo x một đơn vị
while x < x_end
1£ d <= 0 then
d = dt (2*dy) — B x+1
Giải thuật
x=xl;
Bresenham's Midpoint] =
a di=a(xit 1)+b(yit 1/2)+¢
a x=x+l
Nêu điểm A được chọn thí M tang theo 1]
hướng x va y với cùng một đơn vị
else al a ata4 (aye di,, b = F (xi+ : + 2, | i + 3/2)
x = x+1 choose (al m =a(xi + 2) + b(yi +3/2) +c LE
SetPixel (x,y)
Sinh đường tròn Scan Converting Circles
m Explicit: y = f(x) (0.17)
y= ty R? — x?
Usually, we draw a quarter circle by incrementing x from 0 to Rin unit steps and solving for + y for each step
Midpoint Circle Algorithm
m Sử dụng phương pháp biêu diễn không
tường minh trong giải thuật
m Thực hiện giải thuật trên 1/8 đường tròn và lây đôi xứng xho các góc còn lại
(x-x,} +(y-y,F -7 =0
= Parametric: (17, 0) m Với ở, là giá trị của đường tròn tại
một điềm bất kỳ ta có x=Rcosé - by stepping the angle from 0 to 90 | ïï xy +
HH VI HD), - avoids large gaps but still insufficient Lo if (x,, y,)isinsidecircle [ ]
TE l d, =\=0 if (x,,y, jisoncircle
If f(x,y) = 0 then it is on the circle >0 if (x, y, jis outsidecirele le
f(x,y) > 0 then it is outside the circle ~X - f(x,y) < 0 then it is inside the circle
Midpoint Circle Algorithm
= 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
2
d, =(x +1) (a ¬] `
= Ifd,<0 we choose pixel A otherwise we choose pixel B
= Note: we currently assume the circle is centered at the origin
d>0 da<0
(c)SE/FIT/HUT2002 - Ty
Midpoint Circle Algorithm
= Again, as with the line algorithm, the choice of A or B can be used to determine the new value of d,,;
= IfA chosen then next midpoint has the following decision variable:
*
[x+2~3] =| a} =h¡+2Ÿ 3Â =| _r
=d,+2x,+3
= Otherwise if B is chosen then the next decision variable is given by:
2
Ề +2 ¬] = địa = (x, +2) fs 3) Tí
mú/T21 T29 T9
(c) SE/FIT/HUT 2002 18
Trang 4
Midpoint Circle Algorithm
= Ifwe assume that the radius is an integral value, then the first pixel
drawn ts (0, r) and the initial value for the decision variable is given
Lr-—| => d,=1+)/ Pr -r+—|-r
3
"ni
4
= Although the initial value is fractional, we note that all other values are
integers
=> we can round down: dy =l-r
(c) SE/FIT/HUT 2002
Midpoint Circle Algorithm
a = 1-r
Mii stop at diagonal > end of octant
while y < <a
if d < 0 then
da = dđ+2*x+3
ape che choose A
else
dad = d+2* (x-y) +
x = xt1 Fe B
¥) =| Val
endif
SetPixel (c,+x, c.+y)
endwhile
Translate to the circle center (c) SE/FIT/HUT 2002 20
Scan Converting Ellipses
Scan Converting Ellipses: Algorithm
F(x, y)=b’x +a’ y’ —a’b’ =0 C
NN | ⁄
+
2a is the length of the major axis along the x axis
2b is the length of the minor axis along the y axis
The midpoint can also be applied to ellipses
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
fiep tuyen = -1 gradient
= Firstly we divide the quadrant into two regions
= Boundary between the two regions 1s
= 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
grad F(x, y)=OF / Oxi+ OF / Oy j=2b’xit2a’yj
(c) SE/FIT/HUT 2002 22
Ellipses: Algorithm (cont.)
= At the next midpoint, if a(y,-0.5)<=b?(x,+1), we switch region 1=>2
= Inregion 1, choices are E and SE
=b+a?-b+0.25) du„+Delta, with Deltay = b2(2x +3)
= Foramove to SE, d,,,, =d,4y+Delta, with Delta, = b?(2x,+3)+a2(-2y,+2)
= Inregion 2, choices are S and SE
= bx, +0.5P+a?((y-1P-b?)
= dụ¡†Delta, with Delta, = a2(-2y,+3)
= Foramove to SE, d,,,, =d,4+Deltag with Deltag, = b?(2x,+2)+a(-2y,+3)
= Stop in region 2 when the y value is zero
= Initial condition: d,,,,
= Foramove to E, d new -
= Initial condition: d,,.,
m Foramove to S, d new
(c) SE/FIT/HUT 2002 23
Ky tu Bitmap
m Trên cơ sỏ định nghĩa mỗi ký tự với
một font chư cho trước là một bitmap chir nhat nhỏ
= Font/typeface: set of character shapes
m fontcache
m các ký tự theo chuỗi liên tiếp nhau trong
bộ nhớ
m Dạng cơ bản
m (thường N, nghiêng I, đậm B, nghiêng dam B+)
m Thuộc tính
= Also colour, size, spacing and orientation
(c) SE/FIT/HUT 2002
—> ab
24
Trang 5
Cấu trúc font chữ
Typedef struct
int leftx,
int width;
} Char location; //Vi trí của text
Typedef struct
{
Cacheld;
Heiglit; // Độ rộng chữ
CharSpace; // Khoang cach
giữa các ký tự Charlocation Table [128];
} fontcache
(c) SE/FIT/HUT 2002 25
Ký tự vector
m Xây dựng theo phương pháp định nghĩa các ký tự bởi đường cong mềm bao ngoài của chúng
m Tốn kém nhất về mặt tính toán
= Chất lượngcao
(c) SE/FIT/HUT 2002 26
So sanh
Đơn giản trông việc sinh ký tự 1 DU NHN DEN
( copypixel) HÌHEHHHII
Lưu trữ lớn il m Các phép biến đối dựa vào các Các phép biên đổi (LB, scale) công thức biên đôi
đòi hỏi lưu trữ thêm m Kích thước phụ thuôc vào môi Kích thước không dõi trường ( ko có kích thước cô
định)
(c) SE/FIT/HUT 2002 27
Giải thuật đường quét sinh đa giác
Polygon Scan Conversion
m Tôn tại rât nhiêu giải thuật sinh đa giác
m Môi giải thuật phục vụ cho I loại đa giác nhật định:
= some algorithms allow triangular polygons only
= others require that the polygons are convex and non self- intersecting and have no holes
triangula™ convex e4 ex self m 4 Dp
(c) SE/FIT/HUT 2002 28
Polygon Scan Conversion
= Polygon scan conversion la giai thuat chung kinh dién cho các loại
khác nhau
m Cho mỗi đoạn thăng quét, chúng ta xác định các cạnh của đa giác cắt
doan thang compute spans representing the interior portions of the polygons along this scan-line and fill the associated pixels
= This represents the heart of a scan-line rendering algorithm used in
many commercial products including Renderman and 3D Studio MAX
scan-line
pixel spans (c) SE/FIT/HUT 2002 29
Polygon Scan Conversion
m Dùng giải thuật (trung điểm) đề xác định các điểm biên cho mỗi đa giác theo thứ tự tăng của x
m Các diễm phải:
m Không bị chia sẻ bởi các đa giác lân cạn
m Các đa giác chỉ toản các điểm cạnh(
điểm biên)
m Đảm bảo các đa giác chia sẻ điểm biên
mà không chia sẻ các điệm ảnh bên trong của mình
[L' polygon A interior
LI polygon B interior L] edge pixels
(c) SE/FIT/HUT 2002 30
Trang 6
Polygon Scan Conversion
m Thủ tục chung:
m Xác định giao của đường thẳng quét với cạnh đa giác
m Sắp xếp các giao điểm theo mức độ tăng dần của x value
m Điện các điểm ảnh vào giữa cặp các điểm x
= Need to handle 4 cases to prevent pixel sharing:
= if intersection has fractional x value, do we round up or down?
¢ if inside (on left of span) round up, if outside (on right) round down
= what happens if intersection is at an integer x value?
* ifon left of span assume its interior otherwise exterior
= how do we handle shared vertices?
* ignore pixel associated with y,,,, of an edge
= how do we handle horizontal edges?
¢ handled as a result of previous rule (lower edges not drawn)
Polygon Scan Conversion
integer x value is on right = exterior
removed rounded down for A included
rounded up for B (c) SE/FIT/HUT 2002 32
Giải thuật đường quét
Scan-Line Algorithm
m The scan-line algorithm uses edge-coherence and incremental integer calculations for maximum efficiency:
= Tao bang edge fabie (ET) tập của các cạnh đa giác theo thứ tự giá trị y„„ của chúng
= Tao bang active edge table (AET) tap các cạnh giao với đoạn thẳng quét scan-line
= Trong tién trình quét các cạnh sẽ chuyên từ ET ra AET
m Các cạnh sẽ ở trong AET cho đến khi giá trị y„„„ của cạnh đạt
toi = scanline
m Lúc nay cạnh sẽ bị loại ra khỏi AE'T
(c) SE/FIT/HUT 2002 34
(c) SE/FIT/HUT 2002 Si
= Determining intersections with polygon edges is expensive
= rather than re-computing all intersections at each iteration, use incremental calculations
= ie if we intersect edge e on scan-line i then it is likely we will intersect the edge on scan-line i+/ (this is known as edge-coherence) m= Assume slope of the edge > 1 (other edges obtained via symmetries)
= incremental DDA calculation was: ul s-
Weary Yala | ted Oe Di
m
= slope m is given by as (y end mY, hd)
GL a xen)
= note that numerator and denominator are integral > we can use integer DDA
(c) SE/FIT/HUT 2002 33
(0,0)
BS —>
af L numerator 41 _3
L J Ymax Xmin denominator 7 5
(15,15) '†' scan-line Note: line (8,6) —> (13,6) has been deleted according to the scan rules
(c) SE/FIT/HUT 2002 35
Active Edge Table (AET)
ji round down 5 —— +—[[ 3ƒ 8[Zj
a a + 4
|
AET=[iil3a[s[la|—isÏl+z[s[a
HH:
numerator Ymax current x denominator
(c) SE/FIT/HUT 2002 36
Trang 7
Scan-Line Algorithm
y = y of first non empty entry in ET AET = null
repeat
move all ET entries in slot y to AET sort AET entries according to x,,, fill spans using pairs of AET entries for all AET members
if yy = y then remove from AET
¥o= vad for all AET members update numerator
if numerator>denominator
numerator=numerator—denominator
x = xt1 until AET and ET empty
(c) SE/FIT/HUT 2002 37