1-1. Application of CG. (1) Computer-Aided Design (2) Presentation Graphics (3) Computer Art (4) Entertainment (5) Education and Training (6) Visualization (7) Image Processing (8) Graphical User Interfaces 1-2. CG and IP Although methods used in computer graphics and image processing overlap, the two areas are concerned with fundamentally different operations. In computer graphics, a computer is used to create a picture. Image processing, on the other hand, applies techniques to modify or interpret existing pictures, such as photographs and TV scans. 2-1. CRT 组成及工作原理。 2-2. Input Devices and Output Devices. Input Devices: keyboard、mouse、trackball and spaceball、joysticks、data glove、touch panel、 image scanner、voice system、light pen、digitizer Output Devices: screen、printer、plotter. 3-1. An example: Draw a line from point (0, 0) to point (4, 4.8). 1 (0, 0) → (0, 0) Solution: (1)○ (8, 4.8) → (8, 5) 2 m = (5-0) / (8-0) = 0.625, b = 0 ○ 3 ∵m<1 ∴ xi+1 = xi+1 ○ yi+1 = mxi+1+b = 0.625 xi+1 (2) Data Table i 1 2 3 4 5 6 7 8 xi 1 2 3 4 5 6 7 8 yi 0.625 1.25 1.875 2.5 3.125 3.75 4.375 5 [yi] 1 1 2 3 3 4 4 5 (xi, yi) (1, 1) (2, 1) (3, 2) (4, 3) (5, 3) (6, 4) (7, 4) (8, 5) (3) Plot the Figure 3-2. An example: Draw a line from point (0, 0) to point (4, 7) with the DDA algorithm. Solution: (1) Write the DDA algorithm. begin if abs (x2′-x1′)≥abs(y2′-y1′) then length = abs (x2′-x1′) else length = abs(y2′-y1′) endif △x = (x2′-x1′) / length △y = (y2′-y1′) / length k = 1; x = x1′; y = y1′; while (k ≤ length + 1) putpixel(x, y) k = k + 1; x = x + △x; y = y + △y; endwhile end (2) Operation and data table (0, 0) → (0, 0) (4, 7) → (4, 7) abs(4-0) = 4 abs(7-0) = 7 putpixel (x, y) Δx=4/7 length = 7 Δy =7/7=1.0 (0, 0) (1, 1) (1, 2) (2, 3) (2, 4) (3, 5) (3, 6) (4, 7) k 1 2 3 4 5 6 7 8 9 x 0 4/7 8/7 12/7 16/7 20/7 24/7 4 32/7 y 0 1 2 3 4 5 6 7 8 Outside while loop Inside while loop Outside while loop (3) Plot the figure 3-3. An example: Draw a line from the Point P1(0, 0) to Point P2(10, 6) with the Bresenham Algorithm. Solution: (1) Write the Bresenham algorithm. int x = x’1, y = y’1; int dx = x’2 - x’1, dy = y’2 - y’1, dT = 2(dy - dx), dS = 2dy; int d = 2dy - dx; setPixel(x, y); while (x < x’2) { x++; if (d < 0) d = d + dS; else { y++; d = d + dT; } setPixel(x, y); } (2) Data Table. x=0, y=0, dx=10-0=10, dy=6-0=6, dT=2*(6-10)=-8, dS=2*6=12, d=2*6-10=2, setPixel(0,0) while (x≥x’2) while (x< x’2) x 1 2 3 y 1 d -6 6 -2 (x, y) (1, 2) (2, 1) (3, 2) 4 5 6 3 4 10 2 -6 6 -2 10 2 (4, 2) (5, 3) (6, 4) (7, 4) (8, 5) (9, 5) (10, 6) 2 7 8 9 5 10 6 (3) Plot the figure. 3-4. An example: Indicate which raster locations would be chosen by Bresenham’s algorithm when scan-converting a circle centered at the origin with the radius of 8 pixels (from 90° to 45°). Solution: (1) Write the algorithm. int x = 0, y = r, d = 3 - 2r; while (x <= y) { setPixel(x, y); if (d < 0) d = d + 4x + 6; else { d = d + 4(x - y) +10; y--; } x++; } (2) Data Table. x = 0, y = 8, d = 3-2*8 = -13 while (x>y) while (x≤y) (x, y) (0, 8) (1, 8) (2, 8) (3, 7) (4, 7) (5, 6) d -7 3 -11 7 5 11 6 5 5 6 y x 7 1 2 3 4 (3) Plot the figure. 3-5. An example: (1) Write 4_connected boundary algorithm and the order in which pixel are filled in the following Figs. The number “1” represents a seed. (2) Write 8_connected boundary algorithm Solution: (1) Write the algorithm. void boundaryFill4 (int x, int y, int fill, int boundary) { int current; current = getpixel (x, y); if ((current != boundary) && (current != fill)) { setcolor (fill); setpixel (x, y); boundaryFill4 (x+1, y, fill, boundary); boundaryFill4 (x-1, y, fill, boundary); boundaryFill4 (x, y+l, fill, boundary); boundaryFill4 (x, y-1, fill, boundary); } } (2) Plot the result. 4-1. Give the coordinates of three points P0(4,4), P1(24,4), P2(36,3) respectively and their slopes D0(8.832, 5.547), D1(8.832, -5.547), D2(8.832, 5.547). Generate a Hermit spline, compute the coordinates of points at the spline when u=0, 0.25, 0.5, 0.75 and 1, and plot the curve. Solution: A Hermit spline can be described as P(u)= pkH0(u) + pk+1H1(u) + DpkH2(u) + Dpk+1H3(u) where H0(u) =2u3 – 3u2 + 1 H1(u)=-2u3 + 3u H2(u) =u3 – 2u2 + u H3(u)= u3 – u2 (1) For the line P0, P1: P0=[4 4] D0(8.832, 5.547) P1=[24 4] D1(8.832, -5.547) At u=0 and u=1 we have P(0)= P0 =[4 4] P(1)= P1 =[24 4] At u=0.5 H0(0.5)=2*0.53-3*0.52+1=0.5 H1(0.5)=0.5 H2(0.5)=0.125 H3(0.5)= -0.125 Therefore we obtain: P(0.5)= H0(0.5) P0+ H1(0.5) P1+ H2(0.5) D0+ H3(0.5) D1 =0.5[4 4]+0.5[24 4]+0.125[8.832 5.547]-0.125[8.832 -5.547] =[14 5.386] Similarly At u=0.25 P(0.25)=[7.953 5.04] At u=0.75 P(0.75)=[20.04 5.04] (2) For the line P1P2: P1=[24 4] D1 (8.832, -5.547) P2=[36 3] D2 (8.832, 5.547) At u=0 and u=1 we have P(0)= P1 =[24 4] P(1)= P2 =[36 3] At u=0.5 H0(0.5) = 0.5, H1(0.5)=0.5, H2(0.5)=0.125, H3(0.5)= -0.125 P(0.5)=[30 2.11] At u=0.25 P(0.25)=[26.7 2.8] At u=0.75 P(0.75)=[33.3 2.12] Y 5 4 P1 P0 P2 3 2 1 0 4 8 12 16 20 24 28 32 36 X 4-2. Construct a cubic Bezier curve with four points P1(0, 0, 0), P2(1, 1, 1), P3 (2, -1, -1), P4(3, 0, 0), and compute the values of coordinates at u=0, 1/3, 2/3, 1. Solution: A Cubic Bezier Curve can be described as P(u)=B0,3(u)P1+B1,3(u)P2+B2,3(u)P3+B3,3(u)P4 where: B0,3(u)=(1-u)3 B1,3(u)=3u(1-u)2 B2,3(u)=3u2(1-u) B3,3(u)=u3 At u=0 P(0)=P1=[0 0 0] At u=1 P(1)=P4=[3 0 0] At u=1/3 B0,3(1/3)=(1-1/3)3=8/27 B1,3(1/3)=3*(1/3)*(1-1/3)2=4/9 B2,3(1/3)=3*(1/3)2*(1-1/3)=2/9 B3,3(1/3)=(1/3)3=1/27 Thus P(1/3)=8/27[0 0 0]+4/9[1 1 1]+2/9[2 -1 -1]+1/27[3 0 0]=[1 2/9 2/9] At u=2/3 B0,3(2/3)=(1-2/3)3=1/27 B1,3(2/3)=3*(2/3)*(1-2/3)2=2/9 B2,3(2/3)=3*(2/3)2*(1-2/3)=4/9 B3,3(2/3)=(2/3)3=8/27 Thus P(2/3)=1/27[0 0 0]+2/9[1 1 1]+4/9[2 -1 -1]+8/27[3 0 0]=[2 -2/9 -2/9] 4-3. Construct a cubic B-spline curves with 5 points P0(1, 0), P1(0, 2), P2(2, 3), P3 (5, 2.5), P4(6, 1), and compute the values of coordinates at u=0, 1/2, 1, draw the curves. Solution: A Cubic B-spline Curve can be described as P(u)=F0,3(u)P0+F1,3(u)P1+F2,3(u)P2+F3,3(u)P3 where: F0,3(u)=1/6(1-u)3 F1,3(u)= 1/6 (3u3 – 6u2 + 4) F2,3(u)= 1/6 (– 3u3 + 3u2 + 3u +1) F3,3(u)= 1/6 u3 (1) For P0~P3: At u=0 F0,3(0)=1/6 F1,3(0)= 4/6 F2,3(0)= 1/6 F3,3(0)=0 P(0)=[1/6 4/6 1/6 1 0] 0 2 5 =[0.5 2.5 0 2 3 1.83] At u=1 F0,3(1)=0 F1,3(0)= 1/6 F2,3(1)= 4/6 F3,3(1)=1/6 1 1/6] 0 2 5 P(1) [0 1/6 4/6 =[2.17 2.5 0 2 3 2.75] At u=0.5 F0,3(0.5)=0.021 F1,3(0.5)= 0.479 F2,3(0.5)= 0.479 F3,3(0.5)=0.021 1 P(0.5)=[0.021 0.479 0.479 0.021] 0 2 5 =[1.08 2.5 0 2 3 2.45] (2) For P1~P4: P(0)=[2.17 2.75] P(1)=[4.67 2.33] P(0.5)=[3.479 2.698] P2 3 2 P3 P1 1 0 P4 P0 1 2 3 4 5 6 5-1. The three vertices a triangle are expressed by A(0, 20), B(40, 40)), and C(0, 60). We translate the triangle x direction and y direction respectively 30, 20. Compute and draw the new position of the triangle. Solution: xA t x x A 30 0 30 y t y 20 20 40 A y A xB t x x B 30 40 70 y t y 20 40 60 B y B xC t x xC 30 0 30 y t y 20 60 80 C y C 5-2. A triangle shown in the following figure is rotated by 30° about the origin. Compute and draw the new triangle. Solution: x1 cos 30 sin 30 0 0 P1 cos 30 0 0 y1 sin 30 x2 cos 30 sin 30 60 52 P2 cos 30 0 30 y 2 sin 30 x3 cos 30 sin 30 30 4 P3 6 cos 30 0 67 y3 sin 30 Y P3(30,60) 60 30 P2(60,0) 0 P1(0,0) 30 60 X 5-3. Show that the compositiom of two rotations is additive by concatenating the matrix representations for R(θ1) and R(θ2) to obtain R(θ1)*R(θ2) = R(θ1+θ2). Solution: cos1 sin 1 0 cos 2 sin 2 0 R (1 ) sin 1 cos1 0 R( 2 ) sin 2 cos 2 0 0 0 0 1 0 1 cos1 cos 2 sin 1 sin 2 cos1 sin 2 sin 1 cos 2 0 R (1 ) R ( 2 ) sin 1 cos 2 cos1 sin 2 sin 1 sin 2 cos1 cos 2 0 0 0 1 cos(1 2 ) sin( 1 2 ) 0 sin( 1 2 ) cos(1 2 ) 0 0 0 1 cos(1 2 ) sin( 1 2 ) 0 R (1 2 ) sin( 1 2 ) cos(1 2 ) 0 0 0 1 R (1 ) R ( 2 ) R (1 2 ) 6-1. Let R be the rectangular window whose lower left-hand corner is at L(-1, -1) and upper right-hand corner is at R(1, 1). The points P1, P2, P5 are represented with (-3/2, 1/6), (1/2, 3/2) and (3/2, 3/4). Clip the line segment P1P2, P2P5. Solution: (1) The line segment P1P2: P1: 0001, P2: 1000, 0001and1000=0000 So the line segment P1P2 is a candidate for clipping. To find the intersection points with the boundaries of window. The line slop: m=(y2-y1)/(x2-x1)=(3/2-1/6)/(1/2+3/2)=2/3 The line equation: y=1/6+2/3*(x+3/2) The left: x=-1, y=1/6+2/3(-1+3/2)=1/2: saved The right x=1, y=1/6+2/3*(1+3/2)=11/6>ymax: discard The top: y=1, 1=1/6+2/3*(x+3/2)----------x=-1/4, y=1: saved The bottom: y=-1, -1=1/6+2/3*(x+3/2)-----------x=-13/4<xmin, y=-1: discard So the visible portion of line P1P2 is from (-1, 1/2) to (-1/4, 1). (2) The line segment P2P5: P5: 0010, P2: 1000, 0010and1000=0000 So the line segment P2P5 is a candidate for clipping. To find the intersection points with the boundaries of window. The line slop: m=(y2-y1)/(x2-x1)=(3/4-3/2)/(3/2-1/2)=-3/4 The line equation: y=3/2-3/4*(x-1/2) The left: x=-1, y=2.625>ymax: discard The right x=1, y=1.125>ymax: discard The top: y=1, x=1.167>xmax: discard The bottom: y=-1, x=3.833>xmax: discard So the line P2P5 should be deleted(invisible). Y P2 R(1,1) P5 P1 X L(-1,-1) 6-2. Use the Liang-barsky algorithm to clip the Line AB in the following figure. Solution:△x=x2-x1=4, △y=y2-y1=1 Calculate pk, qk, rk: p1=-△x, q1=-1, r1=1/4 p2=△x=4, q2=3, r2=3/4 p3=-△y=-1, q3=0, r3=0 p4=△y=1, q4=2, r4=2 Since p1<0, p3<0: u1=max{0, r1, r3}=max{0, 1/4, 0}=1/4 p2>0, p4>0: u2=min{1, r2, r4}=min{1, 3/4, 2}=3/4 To calculate the visible portion of line u1=1/4 x=x1+△x*u1=1+4*(1/4)=2 y=y1+△y*u1=2+1*(1/4)=9/4 u2=3/4 x=x1+△x*u2=1+4*(3/4)=4 y=y1+△y*u2=2+1*(3/4)=11/4 So the line segment from (2, 9/4) to (4, 11/4) is visible. X (4,4) 4 3 2 B(5,3) A(0,2) (2,2) 1 0 1 2 3 4 5 X