Lecture 8 Shear and Line Drawing Algorithms Computer Graphics Lecture 8 Shear and Line Drawing Algorithms
Shear It is a transformation that distorts the shape of an object. It means “pulling or elongating” a graphical object to right, left, up or down. It can be done along x-axis or along y-axis. Shear along x-axis x’ 1 shx 0 x y’ = 0 1 0 . y 1 0 0 1 1 x’ = x + shx.y y’ = y
shx can be assigned any real number. y y x x Positive values of shx shift the coordinate positions towards right and negative values shift them to left.
Example Shear the following object along x-axis, shx = 2 (0,1) (1,1) (2,1) (3,1) (0,0) (1,0) (0,0) (1,0)
For point (0,1) x’ = x + shx.y x’ = 0 + 2.1 = 2 (0,1) = (2,1) For point (1,1) x’ = 1 + 2. 1 = 3 => (1,1) = (3,1)
For shearing in x-direction relative to other reference lines, that is, shifting coordinate positions by an amount equal to its distance from reference line y = yref x’ = x + shx (y – yref) , (0,1) (1,1) (1,1) (2,1) (0,0) (1,0) (1/2, 0) (3/2,0) yref -1
Shear in y-direction In this case x’ = x y’ = y + shy (x – xref)
Line Drawing Algorithms In random scan systems, a straight line can be drawn from one endpoint to other by using line drawing commands from frame buffer. In raster scan systems the color intensities for a line are stored in the frame buffer and are read from the frame buffer. In order to draw lines, we use the equation of straight line which is given as y = mx + b m= slope of line (that is, how steep line is) b = y intercept, y= how far up, x = how far along
y m b x y2 (x2,y2) y1 (x1,y1) x1 x2
The slope m of a line is equal to the change in y divided by change in x, that is: m = change in x = y2 - y1 change in y x2 - x1 m = δy δx => δy = m . δx => δx = δy m
Positive Slope : On increasing x if y also increases then slope is positive. Negative Slope : On increasing x if y decreases then the slope is negative. y y m m positive slope negative slope x x
Digital Differential Analyzer (DDA) It is a scan conversion line drawing algorithm. It is based on calculating either δy or δx using equations δy = m . δx and δx = δy / m In this algorithm, the line is sampled at unit intervals in one coordinate and the integer nearest to the line path for other coordinate are determined. We will consider a line with positive slope.
Line with positive slope
Case 1: slope is less than or equal to 1 (m<=1) In this case, the line is sampled at unit x intervals and then the y-values are computed. δy = m . δx δy = m (δx = 1) => yk+1 – yk = m => yk+1 = yk + m [This means the next point in x is +1 and next point in y is +m]
Case 2: slope is greater than 1 (m > 1) In this case the roles of x and y are exchanged. Now number of steps is determined by y (δy = 1) and the line is sampled at unit y intervals and values of x are computed. δx = δy / m δ x = 1 / m (δy = 1) xk+1 – xk = 1 / m xk+1 = xk + 1 / m
DDA Algorithm Input x1, y1, x2, y2 dx = x2 – x1 dy = y2 – y1 If dx > dy steps = dx Else steps = dy xincrement = dx / steps yincrement = dy / steps x = x1 y = y1 for (i=0; i< steps; i++) x = x + xincrement y = y + yincrement