1. Trang chủ
  2. » Công Nghệ Thông Tin

Giáo trình đồ họa - Lesson 2 ppt

7 249 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 722,73 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

c SE/FIT/HUT 2002 19Midpoint 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

Trang 1

(c) SE/FIT/HUT 2002

Bài 2:

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

Le Tan Hung hunglt@it-hut.edu.vn 0913030731

Giải thuật 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 thuật VanAken sinh Ellipse

„ Giải thuật sinh đa giác

„ Giải thuật sinh ký tự

Rời rạc hoá điểm ảnh (Scan Conversion rasterization)

pháp xấp xỉ dựa trên lưới phân giải của màn hình

„ smooth

„ continuous

„ pass through specified points

„ uniform brightness

„ efficient

Biểu diễn đoạn thẳng

„ Biểu diễn tường minh

(y-y1)/( x-x1) = ( y2-y1)/( x2-x1)1

y = kx + m

„ k = (y2-y1)/( x2-x1)

„ m = y1- kx1

„ Δy = k Δx

„ Biểu diễn không tường minh

(y2-y1)x - (x2-x1)y + x2y1 - x1y2 = 0 hay rx + sy + t = 0

„ s = -(x2-x1 )

„ r = (y2-y1) và t = x2y1 - x1y2

„ Biểu diễn tham biến P(u) = P1 + u(P2 - P1)

u [0,1]

X = x1 + u( x2 - x1 )

Y = y1 + u( y2 - y1 )

m P(x1, y1)

P(x2 , y2)

u

Thuật toán DDA (Digital Differential Analizer)

Giải thuật DDA

„ Với 0 < k < 1

xi+1= xi+ 1

yi+1 = yi+ k với i=1,2,3

Thuậttoán ddaline (x1, y1, x2, y2) x1, y1, x2, y2 : tọa độ 2 điểm đầu cuối

k : hệ số góc x,y,m :biến begin

m =(x2-x1)/(y2-y1);

x = x1;

k = 1/m;

putpixel(x,y);

while x<x2 begin

x = x+1;

putpixel(round(x),round(y));

end; end;

Giải thuật thông thường

DrawLine(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 );

}}

Giải thuật Bresenham

trên độ phân giai hưu hạn

chia và phép toán làm tròn như ta đã thấy trong gỉai thuật DDA

0 1

2

d2 d1

Trang 2

(c) SE/FIT/HUT 2002 7

Giải thuật Bresenham

„ If d1≤ d2=> yi+1= yi + 1

else d1 > d2=> yi+1= yi

= -2k(xi + 1) + 2yi - 2b + 1

d1 d2

xi x

i+1

yi

yi+1

Pi = -2Δyxi + 2Δxyi + c

Pi+1- Pi

= -2Δy(xi+1- xi) + 2Δx(yi+1- yi)

„ Nếu Pi ≤ 0 ⇒ yi+1= yi + 1

Pi+1= Pi - 2Δy + 2Δx

„ Nếu Pi > 0 ⇒ yi+1= yi

Pi+1= Pi - 2Δy

P1= Δx(d1- d2)

P1= -2Δy + Δx

Giải thuật Bresenham

y i+1 M ( x i , y i )

x i x i+1

Giải thuật trung điểm-Midpoint

„ Jack Bresenham 1965 / Pitteway 1967

„ VanAken áp dụng cho việc sinh các đường

thẳng và đường tròn 1985

„ 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

„ d = F (xi + 1, yi + 1/2) là trung điểm của đoạn

AB

„ Việc so sánh, hay kiểm tra M sẽ được thay

bằng việc xét giá trị d

„ Nếu d > 0 điểm B được chọn, yi+1= yi

„ nếu d < 0 điểm A được chọn ⇒ yi+1= yi+ 1

„ 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

A M B

Bresenham’s Algorithm:

Midpoint Algorithm

„ Sử dụng phương pháp biểu diễn không tường minh

„ Tại mỗi trung điểm của đoạn thẳng giá trị được tính là:

„ Chúng ta gọi dilà biến quyết định của bước thứ i

0

= +

ax

( ) ( ) ( i i)

i i

i 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

( x ) b y c a

⎛ + + +

=

2

1 1

Bresenham’s Algorithm:

Midpoint Algorithm

„ If d i > 0 then chọn điểm A⇒ trung điểm tiếp theo sẽ có dạng:

( )

b a d

c y b x a d y

x

i

i i i i

i

+ +

=

+

⎛ + + +

=

+

2

3 2 2

3 ,

Bresenham’s Algorithm:

Midpoint Algorithm

„ if d i < 0 then chọn điểm B và trung điểm mới là

„ Ta có:

„ Ðiểm đầu

2 2 1 1

2 1 , 1

b a c by ax

c y b x d y

x

start start

start start

start start

start

+ + + +

=

+

+ +

=

( )

a d

c y b x a d y

x

i

i i i i

i

+

=

+

⎛ + + +

=

2

1 2

2

1 ,

C x y x

C c

x x x b

y y y a

start end start end

+ Δ

Δ

=

⎪ Δ

=

= Δ

= Δ

=

where

2

0+a+b

=

Trang 3

(c) SE/FIT/HUT 2002 13

Midpoint Line Algorithm

dx = x_end-x_start

dy = y_end-y_start

d = 2*dy-dx

x = x_start

y = y_start while x < x_end

if d <= 0 then

d = d+(2*dy)

x = x+1

else

d = d+2*(dy-dx)

x = x+1

y = y+1

endif SetPixel(x,y) endwhile

initialisation

choose B

choose A

Giải thuật Bresenham's Midpoint

„ d = a(xi + 1) + b(yi + 1/2) + c

„ Nếu điểm được chọn là B thi M sẽ tang theo x một đơn vị

„ d i +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ểm A được chọn thi` M tăng theo 2 hướng x và y với cùng một đơn vị

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 <= 0

B¾t ®Çu

x = x1 ;

y = y1;

dx = x2 - x1;

d = dy - dx/2;

Putpixel (x ,y);

x < x2

KÕt thóc

d = d + dy

d = d + dy - dx

y = y + 1

yes

no

No

yes

x = x + 1

Sinh đường tròn Scan Converting Circles

„ Implicit: f(x) = x2+y2-R2

„ Explicit: y = f(x)

„ Parametric:

2 2

y = ± Rx

cos sin

θ θ

=

=

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

Midpoint Circle Algorithm

„ Sử dụng phương pháp biểu diễn không tường minh trong giải thuật

„ 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

„ Với d ilà giá trị của đường tròn tại một điểm bất kỳ ta có

( xxc) (2+ yyc)2− r2= 0

( , )isoutsidecircle if

circle

on is , if

circle inside is , if

>

=

<

=

i i i i i i i

y x y x y x d

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 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

2

1

x

di= i+ + ⎜ ⎛ −i ⎟ −

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 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 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 i

i

y x d

r y x d y

x

Trang 4

(c) SE/FIT/HUT 2002 19

Midpoint 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:

r

r r r d r

=

+

=

4 5

4

1 1

2

1 ,

0

r

d0=1 −

Midpoint Circle Algorithm

d = 1-r

x = 0

y = r while y < x

if d < 0 then

d = d+2*x+3

x = x+1

else

d = d+2*(x-y)+5

x = x+1

y = y-1

endif SetPixel(c x +x,c y +y) endwhile

initialisation

choose B choose A

Translate to the circle center stop at diagonal ⇒ end of octant

Scan Converting Ellipses

in the first quadrant, the other three quadrants can be drawn

by symmetry

Scan 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

2 2

grad F x y =∂ ∂ +∂ ∂ = F x i F y j b x i + a y j

A

M tiep tuyen = -1

B gradient

B C M

i

Ellipses: Algorithm (cont.)

„ At the next midpoint, if a 2 (y p -0.5)<=b 2 (x p +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

Ký tự Bitmap

một font chư cho trước là một bitmap chữ nhật nhỏ

shapes

„các ký tự theo chuỗi liên tiếp nhau trong

bộ nhớ

„ (thường N, nghiêng I, đậm B, nghiêng đậm B+I)

„Also colour, size, spacing and orientation

ab

Trang 5

(c) SE/FIT/HUT 2002 25

Cấu trúc font chữ

Typedef struct

{

int leftx,

int width;

} Char location; //Vị trí của text

Typedef struct

{

CacheId;

Heiglit; // Độ rộng chữ

CharSpace; // Khoảng cách

giữa các ký tự Charlocation Table [128];

} fontcache

Ký tự vector

định nghĩa các ký tự bởi đường cong mềm bao ngoài của chúng

toán

So sánh

( copypixel)

đòi hỏi lưu trữ thêm

trình)

công thức biến đổi

trường ( ko có kích thước cố định)

Giải thuật đường quét sinh đa giác Polygon 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 holes

triangular convex non-convex self-intersecting religious

Polygon 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

Polygon Scan Conversion

„ 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

„ Các diểm phải:

„ Không bị chia sẻ bởi các đa giác lân cận

„ Các đa giác chỉ toàn các điểm cạnh(

điểm biên)

„ Đả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

Trang 6

(c) SE/FIT/HUT 2002 31

Polygon Scan Conversion

„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

„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 y maxof an edge

„how do we handle horizontal edges?

• handled as a result of previous rule (lower edges not drawn)

Polygon Scan Conversion

rounded down for A rounded up for B

integer x value is on

right = exterior

ymaxnot included horizontal edge

removed

Polygon 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

m x x y

,

1= + + = +

+

( end start)

start end

x x y y m

=

Giải thuật đường quét Scan-Line Algorithm

integer calculations for maximum efficiency:

„ Tạo bảng edge table (ET) tập của các cạnh đa giác theo thứ tự giá trị

y mincủa chúng

„ 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

tới = scanline

Edge Table (ET)

Note: line (8,6) → (13,6) has been deleted according to the scan rules

ymax xmin

numerator denominator

scan-line

(0,0)

(15,15)

5 3

1 −

m

Active Edge Table (AET)

ymax current x denominator

AET =

current numerator round up

round down

Trang 7

(c) SE/FIT/HUT 2002 37

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 min fill spans using pairs of AET entries for all AET members

if y max = y then remove from AET

y = y+1 for all AET members update numerator

if numerator>denominator numerator=numerator-denominator

x = x+1 until AET and ET empty

Ngày đăng: 12/08/2014, 09:22

TỪ KHÓA LIÊN QUAN