Presentation is loading. Please wait.

Presentation is loading. Please wait.

Raster Graphics.

Similar presentations


Presentation on theme: "Raster Graphics."— Presentation transcript:

1 Raster Graphics

2 Output Primitives Points Lines Polygons DDA Algorithm
Bresenham’s Algorithm Polygons Scan-Line Polygon Fill Inside-Outside Tests Boundary-Fill Algorithm Antialiasing

3 Scan Conversion Rasterization or Scan Conversion is required in order to convert vector data to Raster format for a scanline display device convert each line to a set of regular pixels. determine inside/outside areas when filling polygons. scan-convert curves The scan conversion process is interleaved with other processes to deliver and improve the final image, some of which are not entirely restricted to the discretization of vectors but are involved in generally determining the colour of each pixel in the raster. e.g: shading & illumination, hidden surface removal texture mapping depth testing

4 Points Single Coordinate Position
Set the bit value(color code) corresponding to a specified screen position within the frame buffer y setPixel (x, y) x

5 Pixel Co-ordinates MODELLING CO-ORDINATES
A pixel (10, 6) Y X A point (-12.3, 10.3, 0) X Z Y SCREEN COORDINATES a.k.a device co-ordinates, pixel co-ordinates On display hardware we deal with finite, discrete coordinates X, Y values in positive integers 0,0 is measured from top-left usually with +Y pointing down MODELLING CO-ORDINATES Mathematically vectors are defined in an infinite, “real-number” cartesian co-ordinate system

6 WHERE TO DRAW A LINE?? Line drawing is accomplished by calculating intermediate positions along the line path between specified end points. Precise definition of line drawing Given two points P and Q in the plane, both with integer coordinates, determine which pixels on a raster screen should be on in order to make a picture of a unit-width line segment starting from P and ending at Q.

7 How does a machine draw Lines?
Give it a start and end position. Figure out which pixels to colour in between these… How do we do this? Line-Drawing Algorithms: DDA, Bresenham’s Algorithm

8 Lines Intermediate Positions between Two Endpoints
DDA, Bresenham’s line algorithms Jaggies = Aliasing

9 Line Equation The equation of an infinite line is given by
The gradient m applies to the line as a whole but also to every part of the line. So given any two points on the line we can calculate the slope (x2 , y2) m is a constant which represents the slope/gradient of the line b is the value of x where the line intersects the y-axis (x1, y1) b

10 6 (3, 3) 5 4 3 2 1

11 Line drawing (cont) The thinnest line is of one-pixel wide. We will concentrate on drawing a line of 1 pixel resolution. The Cartesian slope-intercept equation for a straight line is y= m. x + b m is the slope of the line and b is the y intercept. Given the endpoints of a line segment. m = y2-y1 / x2-x1 b= y1-m.x1

12 DDA Algorithm Digital Differential Analyser: an algorithm for scan-converting lines Based on calculating the rate of change of x or y coordinates (Dx or Dy respectively) For each part of the line the following holds true: If Dx = 1 i.e. 1 pixel then … i.e. for each pixel we move right (along the x axis), we need to move down (along the y-axis) by m pixels In pixels, the gradient represents how many pixels we step upwards (Dy) for every step to the right (Dx)

13 Line drawing Cartesian slope-intercept equation for a straight line
y = m.x + c……(1), where m is slope of the line, b is the y intercept Endpoints of a line sgment be (x1, y1) & (x2, y2) then m = (y2 – y1)/(x2 – x1) ……(2), b = y1 – m. x1 ……(3) For any given x interval ∆x along a line, we can compute the corresponding y & x intervals ∆y & ∆x respectively as in eqn (2), ∆y = m ∆x ……(4), & ∆x = ∆y /m ……(5), These equations form the basis for determining the deflection voltages in analog devices. For lines with slope magnitudes |m| < 1, ∆x can be set proportional to a small horizontal deflection voltage and the corresponding vertical deflection is set proportional to ∆y as calculated from eqn (4). For lines with slopes having magnitudes |m| > 1, ∆y can be set proportional to a small vertical deflection voltage and the corresponding horizontal deflection is set proportional to ∆x, calculated from eqn (5). For lines with |m| = 1, ∆x = ∆y and the horizontal & vertical deflection voltages are equal

14 DDA Algorithm Digital Differential Analyzer 0 < Slope <= 1 x1 y1
Unit x interval, ∆x = 1 Subscript k takes integer values starting from 1, for the 1st point, & increases by 1 until the final endpt is reached. Since m can be any real no. between 0 & 1, the calculated y values must be rounded to the nearest integer. x1 y1 x2 y2

15 DDA Algorithm Digital Differential Analyzer 0 < Slope <= 1
Unit x interval, ∆x = 1 Slope > 1 Unit y interval, ∆y = 1 x1 y1 x2 y2

16 DDA Algorithm Digital Differential Analyzer 0 < Slope <= 1
Unit x interval, ∆x = 1 Slope > 1 Unit y interval, ∆y = 1 -1 <= Slope < 0 Unit x interval, ∆x = -1 x1 y2 x2 y1

17 DDA Algorithm Digital Differential Analyzer 0 < Slope <= 1
Unit x interval, ∆x = 1 Slope > 1 Unit y interval, ∆y = 1 -1 <= Slope < 0 Unit x interval, ∆x = -1 Slope < -1 Unit y interval, ∆y = -1 x1 y1 x2 y2

18 Digital differential analyser
lineDDA (xa, ya, xb, yb) { dx = xb – xa; dy = yb – ya; x = xa; y = ya; If(abs(dx) > abs(dy)) del = abs(dx); else del = abs(dy); delx = dx / del; dely = dy / del; setPixel(ROUND(x), ROUND(y)); for( i=0; i<del; i++){ x += delx; y += dely; }

19 DDA ( Digital Differential Algorithm )

20 DDA ( Digital Differential Algorithm )

21 DDA ( Digital Differential Algorithm )

22 Example e.g. p1 = (5, 10), p2 = (25, 20) Starting at p1, we move right by one pixel and down by 0.5 pixels each time. But we can’t move by “half-a pixel” so in pixel coordinates – we need to round-off to the nearest pixel value p1 (5, 10) p2 (25, 20)

23 Rounding Off Note that the actual pixel position is actually stored as a REAL number (in C/C++/java a float or a double) But we Round Off to the nearest whole number just before we draw the pixel. e.g. if m=.33 … X Y Rounded { x, y } 1.0 2.0 3.0 4.0 0.33 { 1, 0 } 0.66 { 2, 0 } 0.99 { 3, 1 } 1.32 { 4, 1 }

24 DDA Illustration (xi+1, Round(yj+m)) (xi, yj) (xi+1, yj+m)
Desired Line (xi+1, Round(yj+m)) (xi, yj) (xi+1, yj+m) (xi, Round(yj)) y2 y1 x1 x2

25 Symmetry If we could draw lines with positive slope (0<=slope<=1) we would be done. For a line with negative slope (0>=slope>=-1) We negate all Y values For a line with slope > 1 or slope <-1 we just swap x and y axes 450 (y,x) (x,y) (y,-x) (-y,x) (x,-y) (-x,-y) (-y,-x) (x,-y)

26 Bresenham’s Algorithm
One disadvantage of DDA is the ROUNDing part which can be expensive Bresenham’s Algorithm is based on essentially the same principles but is completely based on integer variables One of the earliest algorithms in computer graphics

27 Bresenham’s Line Algorithm
An accurate, efficient raster line drawing algorithm developed by Bresenham, scan converts lines using only incremental integer calculations that can be adapted to display circles and other curves. Keeping in mind the symmetry property of lines, lets derive a more efficient way of drawing a line. Starting from the left end point (x0,y0) of a given line , we step to each successive column (x position) and plot the pixel whose scan-line y value closest to the line path Assuming we have determined that the pixel at (xk,yk) is to be displayed, we next need to decide which pixel to plot in column xk+1.

28

29 Bresenham's line algorithm
y = mx + b y = m(x+1) + b d2 d1 y x x+1

30 Bresenham Line Algorithm (cont)
The difference between these 2 separations is A decision parameter pk for the kth step in the line algorithm can be obtained by rearranging above equation so that it involves only integer calculations Choices are(xk +1, yk) and (xk+1, yK+1) d1 = y – yk = m(xk + 1) + b – yk d2 = (yk + 1) – y = yk + 1- m(xk + 1) – b d1-d2 = 2m(xk + 1) – 2 yk + 2b – 1

31 Bresenham’s Line Algorithm
Define Pk = Δx ( d1 - d2) = 2Δyxk - 2 Δxyk + c The sign of Pk is the same as the sign of d1 - d2, since Δx > 0. Parameter c is a constant and has the value 2Δy + Δx(2b-1) (independent of pixel position) If pixel at yk is closer to line-path than pixel at yk +1 (i.e, if d1 < d2) then pk is negative. We plot lower pixel in such a case. Otherwise , upper pixel will be plotted.

32 Bresenham’s algorithm (cont)
At step k + 1, the decision parameter can be evaluated as, pk+1 = 2Δyxk+1 - 2Δxyk+1 + c Taking the difference of pk+ 1 and pk we get the following. pk+1 – pk = 2Δy(xk+1- xk)-2Δx(yk+1 – yk) But, xk+1 = xk +1, so that pk+1 = pk + 2Δy - 2 Δx(yk+1 – yk) Where the term yk+1 - yk is either 0 or 1, depending on the sign of parameter pk

33 Bresenham’s Line Algorithm
The first parameter p0 is directly computed p0 = 2 Δyxk - 2 Δxyk + c = 2 Δyxk – 2 Δy + Δx (2b-1) Since (x0,y0) satisfies the line equation , we also have y0 = Δy/ Δx * x0 + b Combining the above 2 equations , we will have p0 = 2Δy – Δx The constants 2Δy and 2Δy-2Δx are calculated once for each time to be scan converted

34 Bresenham’s Line Algorithm
So, the arithmetic involves only integer addition and subtraction of 2 constants Input the two end points and store the left end point in (x0,y0) Load (x0,y0) into the frame buffer (plot the first point) Calculate the constants Δx, Δy, 2Δy and 2Δy-2Δx and obtain the starting value for the decision parameter as p0 = 2Δy- Δx

35 Bresenham’s Line Algorithm
At each xk along the line, starting at k=0, perform the following test: If pk < 0 , the next point is (xk+1, yk) and pk+1 = pk + 2Δy Otherwise Point to plot is (xk+1, yk+1) pk+1 = pk + 2Δy - 2Δx Repeat step 4 (above step) Δx times

36 The Bresenham Line Algorithm
BRESENHAM’S LINE DRAWING ALGORITHM (for |m| < 1.0) Input the two line end-points, storing the left end-point in (x0, y0) Plot the point (x0, y0) Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the decision parameter as: At each xk along the line, starting at k = 0, perform the following test. If pk < 0, the next point to plot is (xk+1, yk) and:

37 The Bresenham Line Algorithm (cont…)
Otherwise, the next point to plot is (xk+1, yk+1) and: Repeat step 4 (Δx – 1) times NOTE! The algorithm and derivation above assumes slopes are less than 1. for other slopes we need to adjust the algorithm slightly

38 DDA versus Bresenham’s Algorithm
DDA works with floating point arithmetic Rounding to integers necessary Bresenham’s algorithm uses integer arithmetic Constants need to be computed only once Bresenham’s algorithm generally faster than DDA

39 Bresenham Example Let’s have a go at this
Let’s plot the line from (20, 10) to (30, 18) First off calculate all of the constants: Δx: 10 Δy: 8 2Δy: 16 2Δy - 2Δx: -4 Calculate the initial decision parameter p0: p0 = 2Δy – Δx = 6

40 Bresenham Example (cont…)
k pk (xk+1,yk+1) 1 2 3 4 5 6 7 8 9 -2 14 10 (21, 11) (22, 12) (23, 12) (24, 13) (25, 14) (26, 15) (27, 16) (28, 16) (29, 17) (30, 18) 17 16 15 14 13 12 11 10 18 29 27 26 25 24 23 22 21 20 28 30

41 Bresenham Exercise Go through the steps of the Bresenham line drawing algorithm for a line going from (21,12) to (29,16)

42 Bresenham Exercise (cont…)
k pk (xk+1,yk+1) 1 2 3 4 5 6 7 8 17 16 15 14 13 12 11 10 18 29 27 26 25 24 23 22 21 20 28 30

43 Bresenham Line Algorithm Summary
The Bresenham line algorithm has the following advantages: An fast incremental algorithm Uses only integer calculations Comparing this to the DDA algorithm, DDA has the following problems: Accumulation of round-off errors can make the pixelated line drift away from what was intended The rounding operations and floating point arithmetic involved are time consuming

44 Other Quadrants Note that this only applies to lines with a positive gradient But you can easily write a separate case for each other case Also if the gradient is too steep you need to step for x instead of y (as we saw in DDA) Swap y0 for y1 Step for x instead of y Increment by -x Step for y instead of x Increment by -y Swap y0 for y1 Increment by -y Swap x0 for x1 Step for x instead of y Increment by x - Swap x0 for x1 - Step for y instead of x

45 A Simple Circle Drawing Algorithm
The equation for a circle is: where r is the radius of the circle So, we can write a simple circle drawing algorithm by solving the equation for y at unit x intervals using:

46 A Simple Circle Drawing Algorithm (cont…)

47 A Simple Circle Drawing Algorithm (cont…)
However, unsurprisingly this is not a brilliant solution! Firstly, the resulting circle has large gaps where the slope approaches the vertical Secondly, the calculations are not very efficient The square (multiply) operations The square root operation – try really hard to avoid these! We need a more efficient, more accurate solution

48 Eight-Way Symmetry The first thing we can notice to make our circle drawing algorithm more efficient is that circles centred at (0, 0) have eight-way symmetry (x, y) (y, x) (y, -x) (x, -y) (-x, -y) (-y, -x) (-y, x) (-x, y)

49 Circle Generating Algorithm
Properties of Circle Circle is defined as the set of points that are all at a given distance r from a center point (xc,yc) For any circle point (x,y), the distance relationship is expressed by the Pythagorean theorem in Cartesian coordinate as: r yc ө (x,y) xc

50 Circle Generating Algorithm
We could use this equation to calculate the points on a circle circumference by stepping along x-axis in unit steps from xc–r to xc+r and calculate the corresponding y values as

51 The problems: Involves many computation at each step
Spacing between plotted pixel positions is not uniform Adjustment: interchanging x & y (step through y values and calculate x values) Involves many computation too!

52 Another way: Calculate points along a circular boundary using polar coordinates r and ө x = xc + r cos ө y = yc + r sin ө Using fixed angular step size, a circle is plotted with equally spaced points along the circumference Problem: trigonometric calculations are still time consuming

53 Consider symmetry of circles
Shape of the circle is similar in each quadrant i.e. if we determine the curve positions in the 1st quadrant, we can generate the circle section in the 2nd quadrant of the xy plane (the 2 circle sections are symmetric with respect to the y axis) The circle section in the 3rd and 4th quadrant can be obtained by considering symmetry about the x axis One step further  symmetry between octants

54 Circle sections in adjacent octants within 1 quadrant are symmetric with respect to the 45° line dividing the 2 octants Calculation of a circle point (x, y) in 1 octant yields the circle points for the other 7 octants 450 (y,x) (-y,x) (x,y) (x,-y) (y,-x) (-y,-x) (-x,-y) (-x,y)

55 Midpoint Circle Algorithm
As in raster algorithm, we sample at unit intervals & determine the closest pixel position to the specified circle path at each step For a given radius, r and screen center position (xc,yc) , we can set up our algorithm to calculate pixel positions around a circle path centered at the coordinate origin (0,0) Each calculated position (x, y) is moved to its proper screen position by adding xc to x and yc to y

56 Midpoint Circle Algorithm
Along a circle section from x=0 to x=y in the 1st quadrant, the slope (m) of the curve varies from 0 to -1.0 i.e. we can take unit steps in the +ve x direction over the octant & use decision parameter to determine which 2 possible positions is vertically closer to the circle path Positions in the other 7 octants are obtained by symmetry

57 Mid-Point Circle Algorithm
Similarly to the case with lines, there is an incremental algorithm for drawing circles – the mid-point circle algorithm In the mid-point circle algorithm we use eight-way symmetry so only ever calculate the points for the top right eighth of a circle, and then use symmetry to get the rest of the points The mid-point circle algorithm was developed by Jack Bresenham, who we heard about earlier. Bresenham’s patent for the algorithm can be viewed here.

58 Mid-Point Circle Algorithm (cont…)
Assume that we have just plotted point (xk, yk) The next point is a choice between (xk+1, yk) and (xk+1, yk-1) We would like to choose the point that is nearest to the actual circle So how do we make this choice? (xk+1, yk) (xk+1, yk-1) (xk, yk)

59 Mid-Point Circle Algorithm (cont…)
Let’s re-jig the equation of the circle slightly to give us: The equation evaluates as follows: By evaluating this function at the midpoint between the candidate pixels we can make our decision

60 Mid-Point Circle Algorithm (cont…)
Assuming we have just plotted the pixel at (xk,yk) so we need to choose between (xk+1,yk) and (xk+1,yk-1) Our decision variable can be defined as: If pk < 0 the midpoint is inside the circle and and the pixel at yk is closer to the circle Otherwise the midpoint is outside and yk-1 is closer

61 Mid-Point Circle Algorithm (cont…)
To ensure things are as efficient as possible we can do all of our calculations incrementally First consider: or: where yk+1 is either yk or yk-1 depending on the sign of pk

62 Mid-Point Circle Algorithm (cont…)
The first decision variable is given as: (xo, yo=(0,r)) Then if pk < 0 then the next decision variable is given as: If pk > 0 then the decision variable is:

63 The Mid-Point Circle Algorithm
Input radius r and circle centre (xc, yc), then set the coordinates for the first point on the circumference of a circle centred on the origin as: Calculate the initial value of the decision parameter as: Starting with k = 0 at each position xk, perform the following test. If pk < 0, the next point along the circle centred on (0, 0) is (xk+1, yk) and:

64 The Mid-Point Circle Algorithm (cont…)
Otherwise the next point along the circle is (xk+1, yk-1) and: Determine symmetry points in the other seven octants Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to plot the coordinate values: Repeat steps 3 to 5 until x >= y

65 Mid-Point Circle Algorithm Example
To see the mid-point circle algorithm in action lets use it to draw a circle centred at (0,0) with radius 10

66 Mid-Point Circle Algorithm Example (cont…)
9 7 6 5 4 3 2 1 8 10 k pk (xk+1,yk+1) 2xk+1 2yk+1 1 2 3 4 5 6 -9 -6 -1 -3 8 (1,10) (2,10) (3,10) (4,9) (5,9) (6,8) (7,7) 10 12 14 20 18 16

67 Mid-Point Circle Algorithm Exercise
Use the mid-point circle algorithm to draw the circle centred at (0,0) with radius 15

68 Mid-Point Circle Algorithm Example (cont…)
9 7 6 5 4 3 2 1 8 10 13 12 11 14 15 16 k pk (xk+1,yk+1) 2xk+1 2yk+1 1 2 3 4 5 6 7 8 9 10 11 12

69 Mid-Point Circle Algorithm Summary
The key insights in the mid-point circle algorithm are: Eight-way symmetry can hugely reduce the work in drawing a circle Moving in unit steps along the x axis at each point along the circle’s edge we need to choose between two possible y coordinates

70 Filling Polygons So we can figure out how to draw lines and circles
How do we go about drawing polygons? We use an incremental algorithm known as the scan-line algorithm

71 Scan-Line Polygon Fill Algorithm
2 4 6 8 10 Scan Line 12 14 16

72 Scan-Line Polygon Fill Algorithm
The basic scan-line algorithm is as follows: Find the intersections of the scan line with all edges of the polygon Sort the intersections by increasing x coordinate Fill in all pixels between pairs of intersections that lie interior to the polygon

73 Scan-Line Polygon Fill Algorithm (cont…)

74 Line Drawing Summary Over the last couple of lectures we have looked at the idea of scan converting lines The key thing to remember is this has to be FAST For lines we have either DDA or Bresenham For circles the mid-point algorithm

75 Polygon Scan-conversion
First scan convert all edges very coarsely, increment along y and don’t worry about gaps For each scan line, fill between start and end pixel

76 Area Filling A simple recursive flood-fill algorithm: user selects a pixel S. S and all adjacent pixels of the same colour are changed, and the adjacent pixels of those etc… leads to some wasted processing Recursive Flood Fill Let orig_col = original colour of S Let current_pixel = S Change Colour of current_pixel to new_col For all adjacent pixels p IF (colour == orig_col) THEN let p be the current_pixel Repeat from Step 3

77 Antialiasing

78 Aliasing Distortion of information due to low-frequency sampling
In raster images – leads to jagged edges with staircase effect. Commonly occurs when a pattern has details that are smaller than a pixel in width We can reduce effects by antialiasing methods to compensate for undersampling sampling frequency What we “see”

79 Antialiasing Simple perspective projected texture
With some antialiasing

80 Antialiasing Effectively: BLUR edges to make them look smooth

81 Supersampling Example
Use twice the available resolution 4 pixels (2x2 square) for every 1 in the original

82 But we don’t actually render this hi-res line
But we don’t actually render this hi-res line. Instead we downsample it using a “trick” Average out the colours in the 4 pixel squares

83 Antialiasing by polygon boundary

84 Anti-aliasing with Accumulation


Download ppt "Raster Graphics."

Similar presentations


Ads by Google