














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
topics on trnasformation line generation circle generation scaling rotation
Typology: Thesis
1 / 22
This page cannot be seen from the preview
Don't miss anything!















BCA VI Semester BCA 601: Computer Graphics and Multimedia Unit II
Algorithm for line Generation: There is basically two line generating algorithm:
DDA (Digital Differential Algorithm)
Bresenham’s line drawing algorithm
1) DDA: DDA algorithm is an incremental scan conversion method. Here we
perform calculations at each step using the results from the preceding step. The
characteristic of the DDA algorithm is to take unit steps along one coordinate and compute the corresponding values along the other coordinate. The unit steps are
always along the coordinate of greatest change, e.g. if dx = 10 and dy = 5, then we
would take unit steps along x and compute the steps along y.
DDA algorithm: Step 1: Input to the function is two endpoints (x1,y1) and (x2,y2) Step 2: length ← abs(x2-x1); if (abs(y2-y1) > length) then length ←abs(y2-y1); Step 3: xincrement ← (x2-x1) / length; yincrement ← (y2-y1) / length; Step 4: x ←x + 0.5; y ← Y + 0.5; Step 5 : for i ← 1 to length follow steps 6 to 7 Step 6 : plot (trunc(x),trunc(y)); Step 7: x ← x + xincrement ; y ← y + yincrement ; Step 8: stop. The DDA algorithm is faster than the direct use of the line equation since it
calculates points on the line without any floating point multiplication. Advantages of DDA Algorithm
implementation.
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 v direction to
find the pixel positions along the line path.
Disadvantages of DDA Algorithm
Consider the line from (0,0) to (4,6). Use the simple DDA algorithm to rasterize
this line.
Sol. Evaluating steps 1 to 5 in the DDA algorithm we have
Xl = 0 Y 1 = 0 X 2 = 4 Y 2 = 6 Length = |Y 2 -Y 1 l = 6 ∆X = |X 2 - Xl | / Length = 4/6=0. ∆Y = |Y 2 - Yl | / Length = 6/6= Initial value for
X = 0 + 0.5 * Sign (0.66667) = 0. Y = 0 + 0.5 * Sign (1) = 0.
2
The results are plotted as shown in the Fig. It shows that the rasterized line lies on
the actual line and it is 45° line.
2) Bresenham ‘s line drawing algorithm: The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this algorithm is that, it uses only integer calculations. Moving across the x axis in unit intervals and at each step choose between two different y coordinates. For example, as shown in the following illustration, from position (2, 3) you need to choose between (3, 3) and (3, 4). You would like the point that is closer to the original line.
4
At sample position Xk +1, the vertical separations from the mathematical line are labelled as dupper and dlower.
Algorithm:(here m<1)
Step 1: Input the two line endpoints and store the left endpoint in(x 0 ,y 0 )
Step 2: Load ( x 0 ,y 0 ) into the frame buffer; that is , plot the first point. Step 3: Calculate constants ∆x, ∆y,2 ∆y and 2 ∆y -2 ∆x , and obtain the starting value for the decision parameter as: p 0 = 2^ ∆y –^ ∆x Step 4 : .At each xk , the next point the line , starting at k=0, perform the following test: If pk < 0 , the next point to plot is (x (^) k + 1 ,y (^) k ) and p (^) k+1 = p (^) k + 2 ∆y
Efficiency efficient as Bresenham algorithm. and much accurate than DDA algorithm. Drawing DDA algorithm can draw circles and curves but that are not as accurate as Bresenhams algorithm.
Bresenhams algorithm can draw circles and curves with much more accuracy than DDA algorithm. Round Off DDA algorithm round off the coordinates to integer that is nearest to the line.
Bresenhams algorithm does not round off but takes the incremental value in its operation. Expensive DDA algorithm uses an enormous number of floating-point multiplications so it is expensive.
Bresenhams algorithm is less expensive than DDA algorithm as it uses only addition and subtraction.
Circle Generation Algorithm: Drawing a circle on the screen is a little complex than drawing a line. There are two popular algorithms for generating a circle Bresenham’s Algorithm and Midpoint Circle Algorithm. These algorithms are based on the idea of determining the subsequent points required to draw the circle.
The equation of circle is X 2+ Y 2= r 2,X2+Y2=r2, where r is radius.
1) Bresenham Circle Drawing Algorithm: We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest pixel position to complete the arc.
From the following illustration, we can see that we have put the pixel at (X, Y) location and now need to decide where to put the next pixel − at N (X+1, Y) or at S (X+1, Y-1).
This can be decided by the decision parameter d.
Let Xc and Yc denote the x – coordinate and y – coordinate of the center of the
circle. R is the radius. Step 1: Set X = 0 and Y = R
Step 2: Set D = 3 – 2R
Step 3: Repeat While (X < Y)
Step 4: Call Draw Circle(Xc, Yc, X, Y)
Step 5: Set X = X + 1
Step 6: If (D < 0) Then
Step 7: D = D + 4X + 6
Step 8: Else
Step 9: Set Y = Y – 1
Step 10: D = D + 4(X – Y) + 10 [End of If]
Step 11: Call Draw Circle(Xc, Yc, X, Y)
[End of While]
Step 12: Exit
Draw Circle (Xc, Yc, X, Y):
8
Example of Mid Point circle Drawing Algorithm: Let r=10 and determine positions along the circle octant in the first quadrant from x=0 to x=y. The initial value of the decision parameter is P 0 =1-r =- For the circle centered on the coordinate origin, the initial point is (x0,y0)=(0,10), and initial increment terms for calculating the decision parameters are 2x 0 =0 ,^ 2y^0 =
Successive decision parameter values and positions along the circle path are calculated using the mid- point method as
K P (^) k (x (^) k+1 ,yk+1 ) 2xk+1 2yk+ 0 -9 (1,10) 2 20 1 -6 (2,10) 4 20 2 -1 (3,10) 6 20 3 6 (4,9) 8 18 4 -3 (5,9) 10 18 5 8 (6,8) 12 16 6 5 (7,7) 14 14
Polygon Generation and Filling Algorithm: Polygon is an ordered list of vertices as shown in the following figure. For filling polygons with particular colors, you need to determine the pixels falling on the border of the polygon and those which fall inside the polygon. In this chapter, we will see how we can fill polygons using different techniques.
Scan Line Algorithm
10
This algorithm works by intersecting scanline with polygon edges and fills the polygon between pairs of intersections. The following steps depict how this algorithm works. Step 1 − Find out the Ymin and Ymax from the given polygon.
Step 2 − ScanLine intersects with each edge of the polygon from Ymin to Ymax. Name each intersection point of the polygon. As per the figure shown above, they are named as p0, p1, p2, p3. Step 3 − Sort the intersection point in the increasing order of X coordinate i.e. (p0, p1), (p1, p2), and (p2, p3). Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the alternate pairs. Flood Fill Algorithm
Sometimes we come across an object where we want to fill the area and its boundary with different colors. We can paint such objects with a specified interior color instead of searching for particular boundary color as in boundary filling algorithm. Instead of relying on the boundary of the object, it relies on the fill color. In other words, it replaces the interior color of the object with the fill color. When no more pixels of the original interior color exist, the algorithm is completed. Once again, this algorithm relies on the Four-connect or Eight-connect method of filling in the pixels. But instead of looking for the boundary color, it is looking for all adjacent pixels that are a part of the interior.
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol. Step 2 − Define the boundary values of the polygon. Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and 5 till the boundary pixels reached. If getpixel(x, y) = dcol then repeat step 4 and 5 Step 4 − Change the default color with the fill color at the seed point. setPixel(seedx, seedy, fcol) Step 5 − Recursively follow the procedure with four neighborhood points. FloodFill (seedx – 1, seedy, fcol, dcol) FloodFill (seedx + 1, seedy, fcol, dcol) FloodFill (seedx, seedy - 1, fcol, dcol) FloodFill (seedx – 1, seedy + 1, fcol, dcol) Step 6 − Exit There is a problem with this technique. Consider the case as shown below where we tried to fill the entire region. Here, the image is filled only partially. In such cases, 4-connected pixels technique cannot be used.
8-Connected Polygon
In this technique 8-connected pixels are used as shown in the figure. We are putting pixels above, below, right and left side of the current pixels as we were doing in 4-connected technique. In addition to this, we are also putting pixels in diagonals so that entire area of the current pixel is covered. This process will continue until we find a boundary with different color.
Algorithm
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol. Step 2 − Define the boundary values of the polygon. Step 3 − Check if the current seed point is of default color then repeat the steps 4 and 5 till the boundary pixels reached If getpixel(x,y) = dcol then repeat step 4 and 5 Step 4 − Change the default color with the fill color at the seed point. setPixel(seedx, seedy, fcol) Step 5 − Recursively follow the procedure with four neighbourhood points FloodFill (seedx – 1, seedy, fcol, dcol) FloodFill (seedx + 1, seedy, fcol, dcol) FloodFill (seedx, seedy - 1, fcol, dcol) FloodFill (seedx, seedy + 1, fcol, dcol) FloodFill (seedx – 1, seedy + 1, fcol, dcol) FloodFill (seedx + 1, seedy + 1, fcol, dcol) FloodFill (seedx + 1, seedy - 1, fcol, dcol) FloodFill (seedx – 1, seedy - 1, fcol, dcol) Step 6 − Exit The 4-connected pixel technique failed to fill the area as marked in the following figure which won’t happen with the 8-connected technique.
14
In this way, we can represent the point by 3 numbers instead of 2 numbers, which is called Homogenous Coordinate system. In this system, we can represent all the transformation equations in matrix multiplication. Any Cartesian point P(X, Y) can be converted to homogenous coordinates by P’ (X (^) h, Y^ h, h).
Translation A translation moves an object to a different position on the screen. You can translate a point in 2D by adding translation coordinate (t (^) x, t^ y) to the original coordinate (X, Y) to get the new coordinate (X’, Y’).
From the above figure, you can write that − X’ = X + t (^) x Y’ = Y + ty The pair (t (^) x, ty ) is called the translation vector or shift vector. The above equations can also be represented using the column vectors. P =[ X ][ Y ]P=[X][Y] p' = [ X ′][ Y ′][X′][Y′]T = [ tx ][ ty ][tx][ty] We can write it as − P’ = P + T Rotation In rotation, we rotate the object at particular angle θ (theta) from its origin. From the following figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate with distance r from the origin. Let us suppose you want to rotate it at the angle θ. After rotating it to a new location, you will get a new point P’ (X’, Y’).
16
Using standard trigonometric the original coordinate of point P(X, Y) can be represented as − X = rcosϕ ......(1)X=rcosφ......(1) Y = rsinϕ ......(2)Y=rsinφ......(2) Same way we can represent the point P’ (X’, Y’) as − x ′= rcos ( ϕ + θ )= rcosϕcosθ − rsinϕsinθ .......(3)x′=rcos(φ+θ)=rcosϕcosθ −rsinϕsinθ.......(3) y ′= rsin ( ϕ + θ )= rcosϕsinθ + rsinϕcosθ .......(4)y′=rsin(φ+θ)=rcosϕsinθ +rsinϕcosθ.......(4) Substituting equation (1) & (2) in (3) & (4) respectively, we will get x ′= xcosθ − ysinθ x′=xcosθ−ysinθ y ′= xsinθ + ycosθ y′=xsinθ+ycosθ Representing the above equation in matrix form, [ X ′ Y ′]=[ X ′ Y ′][ cosθ − sinθsinθcosθ ] OR [X′Y′]=[X′Y′][cosθsinθ−sinθcosθ]OR P’ = P. R Where R is the rotation matrix R =[ cosθ − sinθsinθcosθ ]R=[cosθsinθ−sinθcosθ] The rotation angle can be positive and negative. For positive rotation angle, we can use the above rotation matrix. However, for negative angle rotation, the matrix will change as shown below − R =[ cos (− θ )− sin (− θ ) sin (− θ ) cos (− θ )]R=[cos(−θ)sin(−θ)−sin(−θ)cos(−θ)] =[ cosθsinθ − sinθcosθ ](∵ cos (− θ )= cosθandsin (− θ )=− sinθ )=[cosθ−sinθsinθcosθ] (∵cos(−θ)=cosθandsin(−θ)=−sinθ)
Scaling
If we provide values less than 1 to the scaling factor S, then we can reduce the size of the object. If we provide values greater than 1, then we can increase the size of the object.
Reflection Reflection is the mirror image of original object. In other words, we can say that it is a rotation operation with 180°. In reflection transformation, the size of the object does not change. The following figures show reflections with respect to X and Y axes, and about the origin respectively.
Shear A transformation that slants the shape of an object is called the shear transformation. There are two shear transformations X-Shear and Y-Shear. One shifts X coordinates values and other shifts Y coordinate values. However; in both the cases only one coordinate changes its coordinates and other preserves its values. Shearing is also termed as Skewing. X-Shear
The X-Shear preserves the Y coordinate and changes are made to X coordinates, which causes the vertical lines to tilt right or left as shown in below figure.
20