Sie sind auf Seite 1von 31

Master of Computer Application (MCA) Semester 3

Computer Graphics MC0072


Assignment Set 1

Que: 1. Explain Bresenhams Circle Drawing Algorithm. Ans: 1. Bresenhams Circle Drawing Algorithm

1/8 part of the circle The Bresenhams circle drawing algorithm considers the eight-way symmetry of the circle to generate it. It plots 1/8th part of the circle, i.e. from 900 to 450 as shown in the figure. As circle is drawn from 900 to 450, the x moves in positive direction and y moves in the negative direction. To achieve best approximation to the true circle we have to select those pixels in the raster that fall the least distance from the true circle. Let us observe the 900 to 450 portion of the circle. It can be found by applying either of the two options: Increment in positive x direction by one unit or Increment in positive x direction and negative y direction both by one unit

Computer Graphics MC0072

Roll No. 521150974

Scan conversion with Bresenhams algorithm Let us assume point P in Figure as a last scan converted pixel. Now we have two options either to choose pixel A or pixel B. The closer pixel amongst these two can be determined as follows The distances of pixels A and B from the origin are given as

DA=

and

DB= Now, the distances of pixels A and B from the true circle are given as and However, to avoid square root in derivation of decision variable, i.e. to simplify the computation and to make algorithm more efficient the =DA2-r2 =DB2-r2 From fig. 3.16 we can observe that decision variable di as di= + is always positive and is always negative. Therefore we can define and are defined as

Computer Graphics MC0072

Roll No. 521150974

and we say that, if di < 0 i.e

<

then only x is incremented; otherwise x is incremented in positive direction

and y is incremented in negative direction. In other words we can write, for di < 0, xi+1= xi+1 for di 0, xi+1= xi+1 and yi+1=yi-1

the equation for di at starting point, i.e at x=0 and y=r can be simplified as follows di= +

=(xi+1)2 + (yi)2 r2 + (xi+1)2+ (yi-1)2-r2 =(0+1)2 + (r)2-r2+ (0+1)2 + (r-1)2-r2 =3-2r Similarly, the equation for di+1 for both cases are given as For di<0, di+1=di+4xi+6 and For di 0, di+1= di+4(xi-yi)+10

Algorithm to plot 1/8 of the circle 1. Read the radius r of the circle 2. d=3-2r [initialize the decision variable] 3. x=0, y=r [initialize starting point] 4. do { Plot( x , y) If (d < 0) then {

Computer Graphics MC0072

Roll No. 521150974

d=d=4x+6 } Else { d=d+4(x-y)+10 y=y-1 } x=x+1 } while (x<y) 5. stop

Computer Graphics MC0072

Roll No. 521150974

The remaining part of circle can be drawn by reflecting point about y axis, x axis and about origin as shown in Figure

8 way symmetry of the circle Therefore by adding seven more plot commands after the plot command in the step 4 of the algorithm, the circle can be plotted. The remaining 7 plot commands are Plot( y, x) Plot (y, -x) Plot(x, -y) Plot(-x,-y) Plot (-y, -x) Plot (-y, x) Plot (-x , y)

Computer Graphics MC0072

Roll No. 521150974

Que: 2. Derive the matrix for inverse transformation. Ans: 2. Matrix for Inverse Transformation When we apply any transformation to point (x, y) we get a new point . Sometimes it may require to undo . This can be

the applied transformation. In such a case we have to get original point (x, y) from the point

achieved by inverse transformation. The inverse transformation uses the matrix inverse of the transformation matrix to get the original point (x, y). The inverse of a matrix is another matrix such that when the two are multiplied together, we get the identity matrix. TT1 = T1 T = I Where I is the identity matrix with all elements along the major diagonal having value 1, and all other elements having value zero. The element for the inverse matrix T1 can be calculated from the elements of T as

Where

is the element in the ith row and jth column of T1, and Mji is the (n 1) by (n 1) submatrix

obtained by deleting the jth row and ith column of the matrix A. The det Mji and det T is the determinant of the Mji and T matrices. The determinant of a 2 2 matrix is

The determinant of a 3 3 matrix is

In general form, the determinant of T is given by

Computer Graphics MC0072

Roll No. 521150974

Where Mij is the sub matrix formed by deleting row i and column j from matrix T. The inverse of the homogeneous coordinate transformation matrix can be given as

It is important to note that the elements of inverse matrix T1 can be calculated from the element of T as

In the above equation the term det T is in the denominator. Hence, we can obtain an inverse matrix if and only if the determinant of the matrix is nonzero.

Computer Graphics MC0072

Roll No. 521150974

Que: 3. Discuss the following Raster Graphic Algorithms: a. Basic Concepts in Line Drawing b. Digital Differential Analyzer c. Midpoint Line Drawing Algorithm Ans: a) Basic Concepts in Line Drawing Before discussing specific line drawing algorithms it is useful to note the general requirements for such algorithms. These requirements specify the desired characteristics of line. The line should appear as a straight line and it should start and end accurately. The line should be displayed with constant brightness along its length independent of its length and orientation. The line should be drawn rapidly. Let us see the different lines drawn in figure.

a) Vertical and horizontal lines b) 450 line

Computer Graphics MC0072

Roll No. 521150974

c) Lines with other orientation As shown in Figure a, horizontal and vertical lines are straight and have same width. The 450 line is straight but its width is not constant. On the other hand, the line with any other orientation is neither straight nor has same width. Such cases are due to the finite resolution of display and we have to accept approximate pixels in such situations, shown in Figure c. The brightness of the line is dependent on the orientation of the line. We can observe that the effective spacing between pixels for the 450 line is greater than for the vertical and horizontal lines. This will make the vertical and horizontal lines appear brighter than the 450 line. Complex calculations are required to provide equal brightness along lines of varying length and orientation. Therefore, to draw line rapidly some compromises are made such as Calculate only an approximate line length. Reduce the calculations using simple integer arithmetic Implement the result in hardware or firmware

Ans: b) Digital Differential Analyzer We know that the slope of a straight line is given as

m=

... (1)

Computer Graphics MC0072

Roll No. 521150974

The above differential equation can be used to obtain a rasterized straight line. For any given x interval x along, a line, we can compute the corresponding y interval y from equation (1) as

(2) Similarly, we can obtain the x interval x corresponding to a specified y as

(3) Once the intervals are known the values for next x and next y on the straight line can be obtained as follows xi+1= xi+x

= And

( 4)

yi+1=yi+y

(5)

The equations 4 and 5 represent a recursion relation for successive values of x and y along the required line. Such a way of rasterizing a line is called a digital differential analyzer (DDA). For simple DDA either whichever is larger, is chosen as one raster unit, i.e. ,

if

then

Else

With this simplification if

then we have

Computer Graphics MC0072

Roll No. 521150974

and

If

then we have and

Let us see the digital differential analyzer (DDA) routine for rasterizing a line DDA Line Algorithm 1. Read the line end points (x1,y1 ) and (x2,y2) such that they are not equal. [if equal then plot that point and exit] 2. ?x = 3. If Length= else Length= end if 4. = (x2-x1)/length = (y2-y1)/length This makes either or equal to 1 because the length is either and ?y = then

| x2-x1| or |y2-y1|, the incremental value for either x or y is 1.

Computer Graphics MC0072

Roll No. 521150974

5. x = x1+0.5 * sign( y = y1+0.5*sign( )

[Here the sign function makes the algorithm work in all quadrant. It returns 1, 0, 1 depending on whether its argument is <0, =0, >0 respectively. The factor 0.5 makes it possible to round the values in the integer function rather than truncating them] 6. i = 1 [begins the loop, in this loop points are plotted] 7. while(i { Plot (Integer(x), Integer(y)) x= x+x y= y+y i=i+1 } 8. stop Advantages of DDA Algorithm 1. It is the simplest algorithm and it does not require special skills for implementation. 2. It is a faster method for calculating pixel positions than the direct use of equation y=mx + b. It eliminates the multiplication in the equation by making use of raster characteristics, so that appropriate increments are applied in the x or y direction to find the pixel positions along the line path. Disadvantages of DDA Algorithm 1. Floating point arithmetic in DDA algorithm is still time-consuming. 2. The algorithm is orientation dependent. Hence end point accuracy is poor. length)

Computer Graphics MC0072

Roll No. 521150974

Ans: c) Midpoint Line Drawing Algorithm In the midpoint algorithm, we find on which side of the line the midpoint lies. Then it is easy to see that, if the midpoint lies above the line, pixel B is closer to the line, if the midpoint lies below the line, pixel A is closer to the line. In our case, the midpoint lies below the line and hence pixel A is closer to the line. From above explanation we can realize that we have to calculate on which side of the line the midpoint lies. The slope intercept form of line can be given as y=mx+C;

Where m=

and C is the y intercept

y=

x+C

F(x,y)=dy. x - dx. y+ C. dx=0 =ax+by+c where a =dy, b=- dx, and c= C. dx The F(x,y) is zero on the line, positive for points below for the line, and negative for points above the line. Therefore, to apply the midpoint criterion, we have only to compute F(m)=F(xp+1,yp+1/2) and to test its sign. The result of the computation F(m) gives the decision, hence it is denoted as d (decision variable). By definition d can be given as d=a (xp+1,)+b(yp+1/2)+c. if d>0, we choose pixel A, d<0, we choose B; and if d=0, we can choose either A or B. The value of d for the next grid (di+1) depends on whether we chose A or B. If B is chosen, C is incremented by one step in the x direction. Then di+1=F(xp+2,yp+1/2)=a(xp+2)+b(yp+1/2)+c We know that di=a(xp+1)+b(yp+1/2)+c Subtracting di from di+1 we get the incremental difference as follows.

Computer Graphics MC0072

Roll No. 521150974

di+1=di + a =di + dy Similarly, if A is chosen, C is incremented by one step each in both the x and y directions. Then, di+1=F(xp+2,yp+3/2)=a(xp+2)+b(yp+3/2)+c Subtracting di form di+1 we get the incremental difference as follows di+1=di+ a+ b =di +dy dx a= dy and b= -dx

Therefore, in midpoint algorithm, at each step algorithm chooses between 2 pixels based on the sign of the decision variable calculated in the previous iteration and then it updates the decision variable by adding either dy (incrB) or dy-dx(incrA) to the previous value, depending on the choice of pixel. Now it is necessary to find the initial value of the decision variable. It can be derived as follows. Let us assume that point (x1,y1) is the starting point of the line and thus the first midpoint is at (x1+1,y1+1/2), And F(x1+1,y1+1/2)= a(x1+1)+b(y1+1/2)+c =ax1+a+by1+b/2+c =ax1+by1+a+b/2+c =F(x1,y1)+a+b/2 But point (x1,y1) is on the line and therefore F(x1,y1) is 0. F(x1+1,y1+1/2)=a+b/2 d initial=a+b/2=dy-dx/2 To avoid the fraction term in the initial d value, the original F is multiplied by 2. F(x, y)=2(ax + by + c). This multiplies each constant and the decision variable by 2, but does not affect the sign of the decision variable.

Computer Graphics MC0072

Roll No. 521150974

d initial=2(dy-dx/2)=2dy-dx Algorithm 1. read the line end points (x1, y1) and (x2, y2) such that they are not equal. 2. dx=x2-x1 and dy=y2-y1 3. [initialize value of the decision variable] d=2dy-dx 4. [increment used when point A is chosen] incrA = 2* (dy - dx) 5. [Increment used when point B is chosen] incrB= 2 * dy 6. [Initialize starting point] x=x1 and y=y1 7. plot (x, y)

Computer Graphics MC0072

Roll No. 521150974

8. while (x < x2) { If (d <=0) { d=d+ incrB } else { d=d+incrA y=y+1 } x=x+1 Plot(x, y) } 9. stop

Computer Graphics MC0072

Roll No. 521150974

Que: 4. Explain the Polygon Seed Filling Algorithms: a. Boundary Fill algorithm b. Flood Fill algorithm c. Scan Line algorithm Ans: a. Boundary Fill algorithm: Algorithm that determines the area connected to a given node in a multi-dimensional array. It is used in the "bucket" fill tool of paint programs to determine which parts of a bitmap to fill with color, and in puzzle games such as Minesweeper, Puyo Puyo, Lumines, Samegame and Magical Drop for determining which pieces are cleared. When applied on an image to fill a particular bounded area with color, it is also known as boundary fill.

Alternative Improved Algorithm 1. Fill in row with START pixel 2. Find RIGHTMOST pixel for line above first row - push on stack 3. Find RIGHTMOST pixel for line below first row - push on stack 4. Pop stack and goto 1, Repeat above until stack empty.

b. Flood Fill algorithm:

Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multidimensional array. It is used in the "bucket" fill tool of paint programs to determine which parts of a bitmap to fill with color, and in puzzle games such as Minesweeper, Puyo Puyo, Lumines, Samegame and Magical Drop for determining which pieces are cleared.

Computer Graphics MC0072

Roll No. 521150974

A method exists that uses essentially no memory for four-connected regions by pretending to be a painter trying to paint the region without painting themselves into a corner. This is also a method for solving mazes. The four pixels making the primary boundary are examined to see what action should be taken. The painter could find themselves in one of several conditions: 1.All four boundary pixels are filled. 2.Three of the boundary pixels are filled. 3.Two of the boundary pixels are filled. 4.One boundary pixel is filled. 5.Zero boundary pixels are filled.

c. Scan Line algorithm: Scanline rendering is an algorithm for visible surface determination, in 3D computer graphics, that works on a row-by-row basis rather than a polygon-by-polygon or pixel-by-pixel basis. All of the polygons to be rendered are first sorted by the top y coordinate at which they first appear, then each row or scan line of the image is computed using the intersection of a scan line with the polygons on the front of the sorted list, while the sorted list is updated to discard no-longer-visible polygons as the active scan line is advanced down the picture.

The asset of this method is that it is not necessary to translate the coordinates of all vertices from the main memory into the working memoryonly vertices defining edges that intersect the current scan line need to be in active memory, and each vertex is read in only once. The main memory is often very slow compared to the link between the central processing unit and cache memory, and thus avoiding re-accessing vertices in main memory can provide a substantial speedup.

This kind of algorithm can be easily integrated with the Phong reflection model, the Z-buffer algorithm, and many other graphics techniques.All intersection calculations are performed by a bivariate Newton-Raphson solution of the defining equations. If the solution does not converge, it is due to the scan plane passing a local

Computer Graphics MC0072

Roll No. 521150974

minimum, causing segments to be deleted from the active list. Finally, within one scan line, an X scan must be performed to generate the Z information about the surface for each picture element. This is also performed by a bivariate Newton-Raphson iteration with a different set of defining functions.

Transformation of hardcopy ship drawings to electronic ones is usually accomplished through scanning and raster-to-vector conversions. Such conversions are, however, limited to produce low-degree vector entities, such as line segments, poly-lines and circular arcs. As a consequence, free-form curves, appearing in the original hardcopy, are usually disintegrated to a significant number of overlapping line and/or arc segments. The algorithm presented in this paper, consists of a scan-line processing of line segments that are grouped (clustered) with the aid of a moving scan-line and an appropriately defined distance to previously grouped entities. The performance of the algorithm is illustrated for the body-plan of a bulk carrier.

Computer Graphics MC0072

Roll No. 521150974

Master of Computer Application (MCA) Semester 3

Computer Graphics MC0072


Assignment Set 2

Que. 1) Explain the midpoint line algorithm. Ans: Midpoint Line Drawing Algorithm In the midpoint algorithm, we find on which side of the line the midpoint lies. Then it is easy to see that, if the midpoint lies above the line, pixel B is closer to the line, if the midpoint lies below the line, pixel A is closer to the line. In our case, the midpoint lies below the line and hence pixel A is closer to the line. From above explanation we can realize that we have to calculate on which side of the line the midpoint lies. The slope intercept form of line can be given as y=mx+C;

Where m=

and C is the y intercept

y=

x+C

F(x,y)=dy. x - dx. y+ C. dx=0 =ax+by+c where a =dy, b=- dx, and c= C. dx The F(x,y) is zero on the line, positive for points below for the line, and negative for points above the line. Therefore, to apply the midpoint criterion, we have only to compute F(m)=F(xp+1,yp+1/2) and to test its sign. The result of the computation F(m) gives the decision, hence it is denoted as d (decision variable). By definition d can

Computer Graphics MC0072

Roll No. 521150974

be given as d=a (xp+1,)+b(yp+1/2)+c. if d>0, we choose pixel A, d<0, we choose B; and if d=0, we can choose either A or B. The value of d for the next grid (di+1) depends on whether we chose A or B. If B is chosen, C is incremented by one step in the x direction. Then di+1=F(xp+2,yp+1/2)=a(xp+2)+b(yp+1/2)+c We know that di=a(xp+1)+b(yp+1/2)+c Subtracting di from di+1 we get the incremental difference as follows. di+1=di + a =di + dy Similarly, if A is chosen, C is incremented by one step each in both the x and y directions. Then, di+1=F(xp+2,yp+3/2)=a(xp+2)+b(yp+3/2)+c Subtracting di form di+1 we get the incremental difference as follows di+1=di+ a+ b =di +dy dx a= dy and b= -dx

Therefore, in midpoint algorithm, at each step algorithm chooses between 2 pixels based on the sign of the decision variable calculated in the previous iteration and then it updates the decision variable by adding either dy (incrB) or dy-dx(incrA) to the previous value, depending on the choice of pixel. Now it is necessary to find the initial value of the decision variable. It can be derived as follows. Let us assume that point (x1,y1) is the starting point of the line and thus the first midpoint is at (x1+1,y1+1/2), And F(x1+1,y1+1/2)= a(x1+1)+b(y1+1/2)+c =ax1+a+by1+b/2+c

Computer Graphics MC0072

Roll No. 521150974

=ax1+by1+a+b/2+c =F(x1,y1)+a+b/2 But point (x1,y1) is on the line and therefore F(x1,y1) is 0. F(x1+1,y1+1/2)=a+b/2 d initial=a+b/2=dy-dx/2 To avoid the fraction term in the initial d value, the original F is multiplied by 2. F(x, y)=2(ax + by + c). This multiplies each constant and the decision variable by 2, but does not affect the sign of the decision variable. d initial=2(dy-dx/2)=2dy-dx Algorithm 1. read the line end points (x1, y1) and (x2, y2) such that they are not equal. 2. dx=x2-x1 and dy=y2-y1 3. [initialize value of the decision variable] d=2dy-dx 4. [increment used when point A is chosen] incrA = 2* (dy - dx) 5. [Increment used when point B is chosen] incrB= 2 * dy 6. [Initialize starting point] x=x1 and y=y1 7. plot (x, y)

Computer Graphics MC0072

Roll No. 521150974

8. while (x < x2) { If (d <=0) { d=d+ incrB } else { d=d+incrA y=y+1 } x=x+1 Plot(x, y) } 9. stop

Computer Graphics MC0072

Roll No. 521150974

Que: 2. Explain the following in relation to the concept of Filling Rectangles and Polygons a. Pattern filling b. Thick primitives c. Line Style and Pen Style Ans: a. Pattern filling: A color, shade or pattern used to fill an area of an image. Signals transmitted by a LAN station when not receiving or transmitting data in order to maintain synchronization. An act or instance of filling. Something used to fill a space, cavity, or container: a gold filling in a tooth. An edible mixture used to fill pastries, sandwiches, or cakes: a pie filling. See Regional Note at frosting. The horizontal threads that cross the warp in weaving; weft.Rather than filling a polygon with a solid color, we may want to fill it with a pattern or texture. A screensaver is a type of computer program initially designed to prevent phosphor burn-in on CRT and plasma computer monitors by blanking the screen or filling it with moving images or patterns when the computer is not in use. Contemporary screensavers are used primarily for entertainment or security.

b. Thick primitives: A graphics system and method with which thick graphic primitives are efficiently drawn by minimizing dependence on drawing algorithms that require appreciable setup time. The method contemplates drawing a thick primitive in which an offset or displacement value is first calculated, based upon the thickness of the graphic primitive. The offset is approximately one half of the thickness of the primitive. Following calculation of the offset value, line drawing parameter values are determined for a line that is parallel to the origin line and displaced from the origin line in a minor axis direction by the displacement or offset value. A loop is then repeated for each grip point in the major axis range of the line. The loop includes an initial step in which a boundary pixel of the thick graphic primitive is drawn using the line drawing algorithm and the line drawing parameter values calculated for the offset line. After the boundary pixel has been drawn, one or more adjacent pixels are drawn using a stepping routine in which the mirior axis coordinate of the selected pixel is either Computer Graphics MC0072 Roll No. 521150974

decremented or incremented, depending upon the slope of the line, to write the pixels adjacent the boundary pixel. In this fashion, the present invention draws a thick primitive as a sequence of segments that are parallel to the minor axis of the origin line. In the preferred embodiment, the line drawing routine is preferably comprised of a Bresenham line drawing algorithm or a similar derivative algorithm. In the preferred embodiment, the displacement D is equal to FLOOR((W1)/2), where W is the thickness of the primitive and FLOOR(X) is the integer portion of X. The present invention provides a graphics system and method with which thick graphic primitives are efficiently drawn by minimizing dependence on drawing algorithms that require appreciable setup time. Broadly speaking, the present invention contemplates a method of drawing a thick primitive in which an offset or displacement value is first calculated based upon the thickness of the graphic primitive. In one presently preferred embodiment, the offset is approximately one half of the thickness of the primitive. Following calculation of the offset value, line drawing parameter values are determined for a line that is parallel to the origin line and displaced from the origin line in a minor axis direction by the displacement or offset value. A loop is then repeated for each grip point in the major axis range of the line. The loop includes an initial step in which a boundary pixel of the thick graphic primitive is drawn using the line drawing algorithm and the line drawing parameter values calculated for the offset line. After the boundary pixel has been drawn, one or more adjacent pixels are drawn using a stepping routine in which the minor axis coordinate of the selected pixel is either decremented or incremented, depending upon the slope of the line, to write the pixels adjacent the boundary pixel. In this fashion, the present invention draws a thick primitive as a sequence of segments that are parallel to the minor axis of the origin line. Because line parameters are calculated for only a single line using the present invention, the setup time required to initiate the drawing of the thick primitive is achieved with a minimum of overhead. Line Style and Pen Style The line-style attribute of SRGP can affect any outline primitive. These attributes interact in thick outline primitives. The line style attribute is also used to calculate the rectangles for each dash; each rectangle is filled with the selected pattern.

Computer Graphics MC0072

Roll No. 521150974

Que: 3. Describe the following Line Clipping Algorithms: a. Sutherland and Cohen Subdivision Line Clipping Algorithm b. Generalized Clipping with Cyrus-beck Algorithm c. Liang - Barsky Line Clipping Algorithm

Ans: a. Sutherland and Cohen Subdivision Line Clipping Algorithm: The calcode routine generates the "code" (same as in Cohen Sutherland algorithm) for the point, which is passed as an argument, against the clipping window boundaries. void lineclip(int x0,int y0,int x1,int y1,int xwmin,int ywmin,int xwmax,int ywmax) { outcode code0,code1; int midx,midy; code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); code1=calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); if( !(code0 | code1) ) { /*The two points are completely inside the window*/ line(x0,y0,x1,y1); return; } else if(code0 & code1) /*The two points are completely outside the window.*/ return ; midx=(x0+x1)/2; midy=(y0+y1)/2;

Computer Graphics MC0072

Roll No. 521150974

lineclip(midx,midy,x1,y1,xwmin,ywmin,xwmax,ywmax); lineclip(x0,y0,midx,midy,xwmin,ywmin,xwmax,ywmax); }

b. Generalized Clipping with Cyrus-beck Algorithm:

Clipping is the word formation process which consists in the reduction of a word to one of its parts (Marchand:1969). Clipping is also known as "truncation" or "shortening." According to Marchand (1969), clippings are not coined as words belonging to the standard vocabulary of a language. They originate as terms of a special group like schools, army, police, the medical profession, etc., in the intimacy of a milieu where a hint is sufficient to indicate the whole. For example, exam(ination), math(ematics), and lab(oratory) originated in school slang; spec(ulation) and tick(et = credit) in stock-exchange slang; and vet(eran) and cap(tain) in army slang. While clipping terms of some influential groups can pass into common usage, becoming part of Standard English, clippings of a socially unimportant class or group will remain group slang.

c. Liang - Barsky Line Clipping Algorithm: There are two common algorithms for line clipping: Cohen-Sutherland and Liang-Barsky Specificaton of algorithm: In fact, line clipping means finding two outer points of intersection between rectangle(clipping) and line(clipped). Line segment acquired by junction of these two points is then called clipped line. Liang-Barsky's Line Clipping Algorithm uses the parametric equations for a line and solves four inequalities to find the range of the parameter for which the line is in the intersection with rectangle. In steps: 1. Let P(x1,y1) and Q(x2,y2) be the outer points of initial line 2. Set Tmin = 0 and Tmax = 1 (Tmin represents the starting position and Tmax represents ending position of actual phase of clipping the line relatively to initial line; at beginning positions of initial line)

Computer Graphics MC0072

Roll No. 521150974

3. Use coordinates of clipping edges to find values of individual inequalities (generally t or individualy tL,tR,tT,tB) From parametric equation x = x1 + (x2-x1)*t, y = y1 + (y2-y1)*t (0<=t<=1): - let R (R = x1 + t*(x2 - x1)) is x coordinate of right edge - tL = (L-x1)/(x2-x1) - let L (L = x1 + t*(x2 - x1))is x coordinate of left edge - tR = (R-x1)/(x2-x1) - let T is y coordinate of top edge - tT = (T-y1)/(y2-y1) - let B is y coordinate of bottom edge - tB = (B-y1)/(y2-y1) If ttmax ignore it and go to the next edge otherwise classify the value of t as entering or exiting value (using inner product to classify). If t is entering value set tmin = t. If t is exiting value set tmax =t. 4. If tmin < tmax then draw a line from (x1 + dx*tmin, y1 + dy*tmin) to (x1 + dx*tmax, y1 + dy*tmax). 5. If the line crosses over the rectangle, you will see (x1 + dx*tmin, y1 + dy*tmin) and (x1 + dx*tmax, y1 + dy*tmax) are intersection between line and edge.

Computer Graphics MC0072

Roll No. 521150974

Que: 4. Discuss the Transformation from World Coordinate to Viewing Coordinates.

Ans: Viewing Transformations from World Coordinate The viewing transformations allow you to specify the position of the eye in the world coordinate system, and to specify the direction toward which it is looking. The polarview and lookat subroutines provide convenient ways to do this. polarview Subroutine The polarview subroutine assumes that the object you are viewing is near the origin. The eye's position is specified by a radius (distance from the origin) and by angles measuring the azimuth and elevation. The specification is similar to polar coordinates, hence, the name. There is still one degree of freedom because these values tell only where the eye is relative to the object. A twist parameter tells which direction is up. The angle of incidence equals the angle between the Z-axis in world coordinates and the location of the origin of viewing coordinates. The angle of azimuth equals the angle between the X-axis in world coordinates and the x,y coordinates of the origin of the viewing coordinates. To understand incidence and azimuth, imagine that you are standing at the origin in world coordinates. You are facing north, along the Y-axis, with the X-axis on your right. The Z-axis points straight up, towards the zenith. There is a very large eye in the sky, looking down at you. It is located at the origin of the viewing coordinate system. This eye is the system, and whatever that eye sees appears on the screen. Where is the eye? The azimuth is the compass point at which it is located: 0 degrees if straight north, 90 degrees if straight east, and so on, in a clockwise fashion (in conformance with astronomical usage). The incidence is the angle down from zenith: 0 degrees means the eye is directly overhead; 90 degrees means that the eye is on the horizon. The altitude (again following astronomical usage) is precisely equal to 90 degrees minus the incidence. This coordinate system is called horizon coordinates or topocentric coordinates. The syntax is as follows: void polarview(Coord distance, Angle azimuth, Angle incidence,

Computer Graphics MC0072

Roll No. 521150974

Angle twist) The following figure illustrates this viewpoint concept.

Lookat Subroutine
The lookat subroutine allows you to specify the eye's position in space and a point at which it is looking. Both points are given with Cartesian x, y, and z coordinates. A twist parameter specifies the angle of rotation. Once you specify the eye position, the point you are looking at could be any point along a line, and the identical transformation is specified. This viewpoint concept is illustrated in the following figure.

Computer Graphics MC0072

Roll No. 521150974

Both viewing subroutines work in conjunction with a projection subroutine. If you wish to view point (1, 2, 3) from point (4, 5, 6) in perspective, use the perspective and lookat subroutines in conjunction. When the orthogonal projections are used, the exact position of the eye used in the viewing subroutines does not make any difference. The only thing that matters is the viewing direction. The viewing transformations work mathematically by transforming, by means of rotations and translations, the position of the eye to the origin and by adjusting the viewing direction so that it lies along the negative Z axis. The polarview and lookat subroutines create a matrix and premultiply it into the current matrix.

Computer Graphics MC0072

Roll No. 521150974

Das könnte Ihnen auch gefallen