Các giải thuật sinh các thực thể cơ sở

7 637 3
Các giải thuật sinh  các thực thể cơ sở

Đang tải... (xem toàn văn)

Thông tin tài liệu

Các giải thuật sinh các thực thể cơ sở

KHoa CNTT-DDHBK Hà nộihunglt@it-hut.edu.vn09130307311(c) SE/FIT/HUT 2002Bài 2:Các giảithuật sinh các thực thể sởLe Tan Hunghunglt@it-hut.edu.vn0913030731(c) SE/FIT/HUT 20022Giảithuậtxâydựng cácthựcthể sởGiảithuậtsinhđường thẳng – LineGiảithuậtsinhđường tròn - CircleGiảithuật VanAken sinh EllipseGiảithuậtsinhđagiácGiảithuậtsinhkýtự(c) SE/FIT/HUT 20023Rờirạchoáđiểm ảnh(Scan Conversion rasterization)Là tiếntrìnhsinhcácđốitượng hình họccơ sở bằng phươngpháp xấpxỉ dựatrênlưới phân giảicủamànhìnhTính chất các đốitượng cần đảmbảo:smoothcontinuouspass through specified pointsuniform brightnessefficient(c) SE/FIT/HUT 20024Biểudiễn đoạnthẳngBiểudiễntường minh(y-y1)/( x-x1) = ( y2-y1)/( x2-x1)1y = kx + mk = (y2-y1)/( x2-x1) m = y1- kx1Δy = k Δx Biểudiễn không tường minh(y2-y1)x - (x2-x1)y + x2y1 - x1y2 = 0hay rx + sy + t = 0 s = -(x2-x1 )r = (y2-y1) và t = x2y1 - x1y2 BiểudiễnthambiếnP(u) = P1 + u(P2 - P1)u [0,1]X = x1 + u( x2 - x1 )Y = y1 + u( y2 - y1 )mP(x1, y1)P(x2 , y2)u(c) SE/FIT/HUT 20025Thuật toán DDA (Digital Differential Analizer)Giảithuật DDAVới 0 < k < 1 xi+1= xi+ 1 yi+1 = yi+ kvới i=1,2,3 Thuậttoán ddaline (x1, y1, x2, y2)x1, y1, x2, y2 : tọa độ 2 điểm đầucuốik : hệ số gócx,y,m :biếnbeginm =(x2-x1)/(y2-y1);x = x1;y = y1;k = 1/m;putpixel(x,y);while x<x2 beginx = x+1;y = y+k;putpixel(round(x),round(y));end; end;Giảithuật thông thườngDrawLine(int x1,int y1, int x2,int y2, int color){float y;int x;for (x=x1; x<=x2; x++) {y = y1 + (x-x1)*(y2-y1)/(x2-x1)WritePixel(x, Round(y), color );}}(c) SE/FIT/HUT 20026Giảithuật Bresenham1960 Bresenham thuộcIBM điểmgầnvới đường thẳng dựatrên độ phân giai hưuhạnloạibỏđược các phép toánchia và phép toán làm trònnhư ta đãthấytronggỉai thuậtDDA Xét đoạnthẳng với 0 < k < 1012012d2d1 KHoa CNTT-DDHBK Hà nộihunglt@it-hut.edu.vn09130307312(c) SE/FIT/HUT 20027Giảithuật Bresenhamd2= y - yi = k(xi +1) + b - yid1= yi+1 - y = yi + 1 - k(xi + 1) - bIf d1≤ d2=> yi+1= yi + 1else d1 > d2=> yi+1= yiD = d1-d2= -2k(xi + 1) + 2yi - 2b + 1Pi = ΔxD = Δx (d1-d2)d1d2xixi+1yiyi+1(c) SE/FIT/HUT 20028Pi = -2Δyxi + 2Δxyi + cPi+1-Pi= -2Δy(xi+1- xi) + 2Δx(yi+1-yi) NếuPi ≤ 0 ⇒ yi+1= yi + 1Pi+1= Pi - 2Δy + 2ΔxNếu Pi > 0 ⇒ yi+1= yiPi+1= Pi - 2Δy P1= Δx(d1-d2)P1= -2Δy + ΔxGiảithuật Bresenham(c) SE/FIT/HUT 20029yi+1M( xi , yi) xixi+1Giảithuật trung điểm-Midpoint Jack Bresenham 1965 / Pitteway 1967 VanAken áp dụng cho việc sinh các đườngthẳng và đường tròn 1985Các công thức đơngiảnhơn, tạo đượccácđiểmtương tự như với Bresenhamd = F (xi + 1, yi + 1/2) là trung điểmcủa đoạnABViệc so sánh, hay kiểmtraM sẽđược thaybằng việc xét giá trị d.Nếud > 0 điểmB đượcchọn, yi+1= yinếud < 0 điểmA đượcchọn. ⇒ yi+1= yi+ 1Trong trường hợp d = 0 chúng ta thểchọn điểmbấtkỳ hoặc A, hoặcB.AMB(c) SE/FIT/HUT 200210Bresenham’s Algorithm: Midpoint AlgorithmSử dụng phương pháp biểudiễn không tường minhTạimỗi trung điểmcủa đoạnthẳng giá trịđượctínhlà: Chúng ta gọi dilà biến quyết định củabướcthứ i0=++ cbyax()()()iiiiiiiiiiiiyxcbyaxyxcbyaxyxcbyax,0,0,0⇒>++⇒<++⇒=++on lineabove linebelow line()cybxadiii+⎟⎠⎞⎜⎝⎛+++=211(c) SE/FIT/HUT 200211Bresenham’s Algorithm: Midpoint AlgorithmIf di> 0 then chọn điểmA ⇒ trung điểmtiếptheosẽ dạng:()badcybxadyxiiiiii++=+⎟⎠⎞⎜⎝⎛+++=⇒⎟⎠⎞⎜⎝⎛+++23223,21(c) SE/FIT/HUT 200212Bresenham’s Algorithm: Midpoint Algorithmif di< 0 then chọn điểm B và trung điểmmớilàTa có:Ðiểm đầu()[]221121,1bacbyaxcybxadyxstartstartstartstartstartstartstart++++=+⎟⎠⎞⎜⎝⎛+++=⇒⎟⎠⎞⎜⎝⎛++()adcybxadyxiiiiii+=+⎟⎠⎞⎜⎝⎛+++=⇒⎟⎠⎞⎜⎝⎛+++21221,21CxxyyxCcxxxbyyyastartendstartend+ΔΔ=⎪⎭⎪⎬⎫Δ=−=Δ−=−=Δ= where20ba ++= KHoa CNTT-DDHBK Hà nộihunglt@it-hut.edu.vn09130307313(c) SE/FIT/HUT 200213Midpoint Line Algorithmdx = x_end-x_startdy = y_end-y_startd = 2*dy-dxx = x_starty = y_startwhile x < x_end if d <= 0 thend = d+(2*dy)x = x+1elsed = d+2*(dy-dx)x = x+1y = y+1endifSetPixel(x,y)endwhileinitialisationchoose Bchoose A(c) SE/FIT/HUT 200214GiảithuậtBresenham's Midpointd = a(xi + 1) + b(yi + 1/2) + cNếu điểm đượcchọnlàB thiM sẽ tang theo x một đơnvịdi+1= F(xi +2, yi + 1/2)= a(xi +2) + b(yi + 1/2) + cdi = a(xi + 1) + b(yi + 1/2) + cNếu điểmA đượcchọnthi` M tăng theo 2 hướng x và y với cùng một đơnvị.di+ 1 = F (xi + 2, yi + 3/2) = a(xi + 2) + b(yi +3/2) + cdi+ 1= di + a + b.¾Với a + b = dy - dx.d <= 0B¾t ®Çux = x1 ;y = y1;dx = x2 - x1;dy = y2 - y1;d = dy - dx/2;Putpixel (x ,y);x < x2KÕt thócd = d + dyd = d + dy - dxy = y + 1yesnoNoyesx = x + 1(c) SE/FIT/HUT 200215Sinh đường trònScan Converting CirclesImplicit: f(x) = x2+y2-R2Explicit: y = f(x)Parametric:22yRx=± −cossinxRyRθθ==If f(x,y) = 0 then it is on the circle.f(x,y) > 0 then it is outside the circle.f(x,y) < 0 then it is inside the circle.Usually, we draw a quarter circle by incrementing x from 0 to R in unit steps and solving for +y for each step.- by stepping the angle from 0 to 90- avoids large gaps but still insufficient.(c) SE/FIT/HUT 200216Midpoint Circle AlgorithmSử dụng phương pháp biểudiễn khôngtường minh trong giảithuậtThựchiệngiảithuậttrên1/8 đườngtròn và lấy đốixứng xho các góc cònlại.Với dilà giá trị của đường tròn tạimột điểmbấtkỳ ta có( ) ( )0222=−−+−ryyxxcc( )()() circle outside is , if 0circleon is , if 0circle inside is , if 0⎪⎩⎪⎨⎧>=<=iiiiiiiyxyxyxd(c) SE/FIT/HUT 200217Midpoint 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 circle:If di< 0 we choose pixel A otherwise we choose pixel BNote: we currently assume the circle is centered at the origin()222211 ryxdiii−⎟⎠⎞⎜⎝⎛−++=(c) SE/FIT/HUT 200218Midpoint Circle AlgorithmAgain, as with the line algorithm, the choice of A or B can be used to determine the new value of di+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:()3221221,22221++=−⎟⎠⎞⎜⎝⎛−++=⇒⎟⎠⎞⎜⎝⎛−++iiiiiiixdryxdyx()52223223,22221+−+=−⎟⎠⎞⎜⎝⎛−++=⇒⎟⎠⎞⎜⎝⎛−++iiiiiiiiyxdryxdyx KHoa CNTT-DDHBK Hà nộihunglt@it-hut.edu.vn09130307314(c) SE/FIT/HUT 200219Midpoint Circle Algorithm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.⇒we can round down:rrrrdr−=−⎟⎠⎞⎜⎝⎛+−+=⇒⎟⎠⎞⎜⎝⎛−4541121,1220rd −=10(c) SE/FIT/HUT 200220Midpoint Circle Algorithmd = 1-rx = 0y = rwhile y < x if d < 0 thend = d+2*x+3x = x+1elsed = d+2*(x-y)+5x = x+1y = y-1endifSetPixel(cx+x,cy+y)endwhileinitialisationchoose Bchoose ATranslate to the circle centerstop at diagonal ⇒ end of octant(c) SE/FIT/HUT 200221Scan Converting Ellipses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 symmetry22 22 22(, ) 0Fxybxayab=+−=(c) SE/FIT/HUT 200222Scan Converting Ellipses: Algorithm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 22(, ) / / 2 2gradFxy Fx Fy bx ay=∂ ∂ +∂ ∂ = +ijijAM tiep tuyen = -1B gradientB CMi(c) SE/FIT/HUT 200223Ellipses: Algorithm (cont.)At the next midpoint, if a2(yp-0.5)<=b2(xp+1), we switch region 1=>2In region 1, choices are E and SEInitial condition: dinit= b2+a2(-b+0.25)For a move to E, dnew= dold+DeltaEwith DeltaE= b2(2xp+3)For a move to SE, dnew= dold+DeltaSEwith DeltaSE= b2(2xp+3)+a2(-2yp+2)In region 2, choices are S and SEInitial condition: dinit= b2(xp+0.5)2+a2((y-1)2-b2)For a move to S, dnew= dold+Deltaswith Deltas= a2(-2yp+3)For a move to SE, dnew= dold+DeltaSEwith DeltaSE= b2(2xp+2)+a2(-2yp+3)Stop in region 2 when the y value is zero. (c) SE/FIT/HUT 200224Ký tự BitmapTrên sỏđịnh nghĩamỗikýtự vớimột font chư cho trướclàmộtbitmap chữ nhậtnhỏFont/typeface: set of character shapesfontcachecác ký tự theo chuỗiliêntiếp nhau trongbộ nhớDạng bản(thường N, nghiêng I, đậm B, nghiêngđậmB+I) ThuộctínhAlso colour, size, spacing and orientationab KHoa CNTT-DDHBK Hà nộihunglt@it-hut.edu.vn09130307315(c) SE/FIT/HUT 200225Cấutrúcfont chữTypedef struct{int leftx, int width; } Char location; //Vị trí củatextTypedef struct{CacheId;Heiglit; // Độ rộng chữCharSpace; // Khoảng cáchgiữacáckýtựCharlocation Table [128];} fontcache(c) SE/FIT/HUT 200226Ký tự vectorXây dựng theo phương phápđịnh nghĩa các ký tự bởiđường cong mềm bao ngoàicủa chúng. Tốnkémnhấtvề mặt tínhtoánChấtlượngcao(c) SE/FIT/HUT 200227So sánhĐơngiảntrôngviệcsinhkýtự( copypixel) Lưutrữ lớnCác phép biến đổi (I,B, scale) đòi hỏilưutrữ thêmKích thước không dổiPhứctạp(Tínhtoánphươngtrình)Lưutrữ gọnnhẹCác phép biến đổidựa vào cáccông thứcbiến đổiKích thướcphụ thuôc vào môitrường ( ko kích thướccốđịnh)(c) SE/FIT/HUT 200228Giải thuật đường quét sinh đa giácPolygon Scan ConversionTồn tại rất nhiều giải thuật sinh đa giác.Mỗi giải thuật phục vụ cho 1 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 holestriangular convex non-convex self-intersecting religious(c) SE/FIT/HUT 200229Polygon Scan ConversionPolygon scan conversion là giải thuật chung kinh điển cho các loại khác nhau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 đoạn thẳng 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.(c) SE/FIT/HUT 200230Polygon Scan ConversionDùng giảithuật (trung điểm) để xácđịnh các điểmbiênchomỗi đagiáctheo thứ tự tăng củax.Các diểmphải:Không bị chia sẻ bởicácđagiáclâncậnCác đagiácchỉ toàn các điểmcạnh( điểmbiên)Đảmbảocácđagiácchiasẻđiểm biênmà không chia sẻ các điểm ảnh bêntrong của mình. KHoa CNTT-DDHBK Hà nộihunglt@it-hut.edu.vn09130307316(c) SE/FIT/HUT 200231Polygon Scan ConversionThủ tục chung:Xác định giao của đường thẳng quét với cạnh đa giácSắp xếp các giao điểm theo mức độ tăng dần của x valueĐ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?• if on left of span assume its interior otherwise exteriorhow do we handle shared vertices?• ignore pixel associated with ymaxof an edge how do we handle horizontal edges?• handled as a result of previous rule (lower edges not drawn)(c) SE/FIT/HUT 200232Polygon Scan Conversionrounded down for Arounded up for Binteger x value is onright = exteriorymaxnot includedhorizontal edgeremoved(c) SE/FIT/HUT 200233Polygon Scan ConversionDetermining intersections with polygon edges is expensiverather than re-computing all intersections at each iteration, use incremental calculationsi.e. if we intersect edge e on scan-line i then it is likely we will intersect the edge on scan-line i+1 (this is known as edge-coherence)Assume slope of the edge > 1 (other edges obtained via symmetries)incremental DDA calculation was:slope m is given bynote that numerator and denominator are integral ⇒ we can use integer DDA.mxxyyiiii1,111+=+=++( )()startendstartendxxyym−−=(c) SE/FIT/HUT 200234Giảithuật đường quétScan-Line AlgorithmThe scan-line algorithm uses edge-coherenceand incremental integer calculationsfor maximum efficiency:Tạobảng edge table (ET) tậpcủacáccạnh đagiáctheothứ tự giá trịymincủa chúngTạobảng active edge table (AET) tậpcáccạnh giao vớI đoạnthẳngquét scan-lineTrong tiến trình quét các cạnh sẽ chuyểntừ ET ra AET. Các cạnh sẽởtrong AET cho đến khi giá trịymaxcủacạnh đạttới = scanlineLúc nay cạnh sẽ bị loạirakhỏiAET.(c) SE/FIT/HUT 200235Edge Table (ET)Note: line (8,6) → (13,6) has been deleted according to the scan rulesymaxxminnumeratordenominatorscan-line(0,0)(15,15)531−=⇒m(c) SE/FIT/HUT 200236Active Edge Table (AET)ymaxcurrent x denominatorAET =current numeratorround upround down KHoa CNTT-DDHBK Hà nộihunglt@it-hut.edu.vn09130307317(c) SE/FIT/HUT 200237Scan-Line Algorithmy = y of first non empty entry in ETAET = nullrepeatmove all ET entries in slot y to AETsort AET entries according to xminfill spans using pairs of AET entriesfor all AET membersif ymax= y then remove from AETy = y+1for all AET membersupdate numeratorif numerator>denominatornumerator=numerator-denominatorx = x+1until AET and ET empty . 2002Bài 2 :Các giảithuật sinh các thực thể cơ sởLe Tan Hunghunglt@it-hut.edu.vn0913030731(c) SE/FIT/HUT 20022Giảithuậtxâydựng cácthựcthể cơ sở Giảithuậtsinhđường. sở Giảithuậtsinhđường thẳng – LineGiảithuậtsinhđường tròn - CircleGiảithuật VanAken sinh EllipseGiảithuậtsinhđagiácGiảithuậtsinhkýtự(c) SE/FIT/HUT 20023Rờirạchoáđiểm

Ngày đăng: 27/08/2012, 13:54

Hình ảnh liên quan

„ Giải thuật sinh đường thẳng – Line - Các giải thuật sinh  các thực thể cơ sở

i.

ải thuật sinh đường thẳng – Line Xem tại trang 1 của tài liệu.
pháp xấp xỉ dựa trên lưới phân giải của màn hình „ Tính chất cácđối tượng cầnđảm bảo :„Tính chất cácđối tượng cầnđảm bảo : - Các giải thuật sinh  các thực thể cơ sở

ph.

áp xấp xỉ dựa trên lưới phân giải của màn hình „ Tính chất cácđối tượng cầnđảm bảo :„Tính chất cácđối tượng cầnđảm bảo : Xem tại trang 1 của tài liệu.
„ Giải thuật sinh ký tự - Các giải thuật sinh  các thực thể cơ sở

i.

ải thuật sinh ký tự Xem tại trang 1 của tài liệu.
„ Giải thuật sinh đường trò n- Circle - Các giải thuật sinh  các thực thể cơ sở

i.

ải thuật sinh đường trò n- Circle Xem tại trang 1 của tài liệu.
„ Giải thuật sinh đa giác - Các giải thuật sinh  các thực thể cơ sở

i.

ải thuật sinh đa giác Xem tại trang 1 của tài liệu.
thực thể cơ sở - Các giải thuật sinh  các thực thể cơ sở

th.

ực thể cơ sở Xem tại trang 1 của tài liệu.
Polygon Scan Conversion - Các giải thuật sinh  các thực thể cơ sở

olygon.

Scan Conversion Xem tại trang 6 của tài liệu.
„ Tạo bảng edge table (ET) tập của các cạnh đa giác theo thứ tự giá trị - Các giải thuật sinh  các thực thể cơ sở

o.

bảng edge table (ET) tập của các cạnh đa giác theo thứ tự giá trị Xem tại trang 6 của tài liệu.
integer calculations for maximum efficiency: - Các giải thuật sinh  các thực thể cơ sở

integer.

calculations for maximum efficiency: Xem tại trang 6 của tài liệu.
tớ i= scanline - Các giải thuật sinh  các thực thể cơ sở

t.

ớ i= scanline Xem tại trang 6 của tài liệu.
„ Tạo bảng active edge table (AET) tập các cạnh giao vớI đoạn thẳng quét scan-line - Các giải thuật sinh  các thực thể cơ sở

o.

bảng active edge table (AET) tập các cạnh giao vớI đoạn thẳng quét scan-line Xem tại trang 6 của tài liệu.
Polygon Scan Conversion - Các giải thuật sinh  các thực thể cơ sở

olygon.

Scan Conversion Xem tại trang 6 của tài liệu.
Scan-Line Algorithm - Các giải thuật sinh  các thực thể cơ sở

can.

Line Algorithm Xem tại trang 6 của tài liệu.
Polygon Scan Conversion „ Determining intersections with polygon edges is expensive - Các giải thuật sinh  các thực thể cơ sở

olygon.

Scan Conversion „ Determining intersections with polygon edges is expensive Xem tại trang 6 của tài liệu.

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan