Scan Conversion for Straight Lines

Slides:



Advertisements
Similar presentations
Objective - To graph linear equations using the slope and y-intercept.
Advertisements

2.4 Writing the Equation of a Line
Writing equations of parallel and perpendicular lines.
NOTES: GRAPHING USING SLOPE & Y-INTERCEPT DAY 2. STEPS REWRITE IN SLOPE INTERCEPT FORM REWRITE IN SLOPE INTERCEPT FORM (Y = MX+B) LABEL SLOPE (M) AND.
Objective The student will be able to: 1) write equations using slope-intercept form. 2) identify slope and y-intercept from an equation. 3) write equations.
Slope-Intercept and Point-Slope Forms of a Linear Equation
Graphing Using Slope - Intercept STEPS : 1. Equation must be in y = mx + b form 2. Plot your y – intercept ( 0, y ) 3. Using your y – intercept as a starting.
Slope-Intercept and Point-Slope Forms of a Linear Equation.
Table of Contents Lines: Point-Slope Equation The point-slope equation of a nonvertical line is y – y c = m(x – x c ). Here, m is the slope and x c, y.
Writing Linear Equation using slope-intercept form.
Mathematical Studies for the IB Diploma Second Edition © Hodder & Stoughton Ltd Equation of a straight line.
3.3 Slope.
Writing the Equation of a Line
Slope-Intercept Form of a Line
Writing equations given a point and the slope of a line
x y Straight Line Graphs m gives the gradient of the line and.
Graphing Lines Section 4.6.
2.4 Linear Functions: Graphs and Slopes. Slope is the steepness of the line (the slant of the line) and is defined by rise the change in y run the change.
2.4 “Writing Linear Equations” ***When writing equations of lines, substitute values for: y = mx + b Given: 1.Slope and y-intercept m = -3 b = 5 Step:
Do Now Write the slope-intercept equation of this line.
Point slope form of an equation Y - y₁ = m(X- x₁) (x₁, y₁) An ordered pair on the line m slope.
Point slope form of an equation Y - y₁ = m(X- x₁) (x₁, y₁) An ordered pair on the line m slope.
Point Slope Form. Write the equation of the line with slope 3 and passing through the point (1, 5). y – y 1 = m(x – x 1 )
Do Now 1)Write the slope-intercept equation of this line.
© The Visual Classroom Review of Straight Lines y = mx + b rise run rise run positive slope negative slope.
Writing equations given 2 points on the line. Write the equation of the line that goes through the points (1, 6) and (3, -4) m = y₂ - y₁ x₂ - x₁ -4 -
Linear Graphs and Modelling Plotting straight line graphs Plotting linear graphs on the calculator Finding gradients of straight lines Equations of straight.
Lines of Best Fit When data show a correlation, you can estimate and draw a line of best fit that approximates a trend for a set of data and use it to.
Improve Accuracy with Software: S 0 Calibration. Calibrating the System Plot Draw the best straight line fit for the above function –You can find the.
SLOPE-INTERCEPT FUNCTIONS ALGEBRA 1. DO YOU REMEMBER? Ordered Pairs? Two-Dimensional Graphs? Functions? Plotting Points? Domain and Range? Single-Variable.
Point-Slope Formula I can write an equation given a point and the slope.
How to Write an Equation of a Line Given TWO points
Writing Linear Equations
Lesson 5.6 Point-Slope Form of the Equation of a Line
Unit 2 Day 4 Slope-Intercept Form.
Quick Graphing Using Slope-Intercept Form
Lines of Best Fit When data show a correlation, you can estimate and draw a line of best fit that approximates a trend for a set of data and use it to.
Scatter Plots Line of Best Fit
Objective- To use slope and y-intercept to
4.4 Scatter Plots.
Equations of straight lines
Class Averages Second % Third % Fourth % Seventh %
Chapter 1: Lesson 1.3 Slope-Intercept Form of a Line
2.4 Writing the Equation of a Line
Any two equations for the same line are equivalent.
Objectives Graph a line and write a linear equation using point-slope form. Write a linear equation given two points.
Objective The student will be able to:
2.4 Linear Functions: Graphs and Slope
Scatter Plots Line of Best Fit
2.4 Writing the Equation of a Line
8/29/12 Writing the Equation of a Line
Writing Linear Equations When Given Two Points on the Line
Quick Graphs of Linear Equations
Today’s Target…. I can identify patterns and trends on a graph. Today I will identify patterns and trends on a graph showing the motion of an object.
Writing the Equation of a Line
Forms of a linear equation
5.6 – point slope formula Textbook pg. 315 Objective:
Graphing Linear Equations
8-6 Slope-Intercept Form
Slope-intercept Form of Equations of Straight Lines
Section 3.3 The Slope of a Line.
Point-slope Form of Equations of Straight Lines
Distance – Time Graphs Time is usually the independent variable (plotted on the x-axis) Distance is usually the dependent variable (plotted on the y-axis)
5-3 slope-intercept form
Warm-up # 34 Tuesday, 12/8/ −3= Simplify 4 2 −2 4
5-4 point-slope form.
Homework Lesson 4.03_ pg 287_ #8-11.
2.4 Writing the Equation of a Line
9.4 Absolute Value Functions and Graphs
Graphing using Slope-Intercept Form
Presentation transcript:

Scan Conversion for Straight Lines Problem: Given a line y = mx + b, which goes through the two points (x1, y1) and (x2, y2) plot the pixels which are closest to the line. An additional constraint is that the slope of the line m be between 0 and 1. That is to say 0<= m <= 1 Line equations: m = dy / dx (constant slope) b is the y intercept y = mx + b (slope-intercept form) m = (y2-y1)/(x2-x1) (by endpoints definition) m = (y – y1)/(x – x1)  y – y1 = m (x – x1)  y = m (x-x1) + y1 (point-slope form)

Algorithm of Scan Conversion for Line drawing Brute-force method: Observing that the slope of the line is between 0 and 1 leads us to write the following code. Draw_line(x1, x2, y1, y2) length = x2 - x1; Notice that here we are relying on the fact that 0 <= m <= 1 m = (y2-y1) / (x2 – x1) b = y1 – m * x1 for i from 0 to length by 1 (Notice: step in x direction, x2 > x1) Set_Pixel (x1 + i, round(m*(x1+i) +b) end for end Draw_line Note: This code will draw the required line but there are several problems with this implementation, which are described as follows:

Algorithm of Scan Conversion for Line drawing Brute-force method: Problems: (1) A multiplication m*(x1+1) is performed at each step. This Is a floating point multiply. This method is too slow (2) The variables are floating point variables. This could lead to round-off error. We can use the fact that 0<=m<=1 to notice that for each unit step in x there is a y step which is <=1 (3) If m > 1 (I.e., |y2 –y1| > |x2-x1|) there will be gaps. In that case should step in y direction.

Algorithm of Scan Conversion for Line drawing Digital Differential Analyzer (DDA): Using the above observation we can rewrite the above code: Draw_line(x1, x2, y1, y2) length = x2 – x1; Notice that here we are relying on the fact that 0 <= m <= 1 m = (y2-y1) / (x2 – x1) y = y1 for i from 0 to length by 1 (Notice: step in x direction, x2 > x1) Set_Pixel (x1 + i, round(y)) y = y + m end for end Draw_line Note: This implementation replaces a floating point multiply at each step by a floating point addition. The problem of floating point numbers is still there

Algorithm of Scan Conversion for Line drawing Bresenham’s Development: Assumptions: 0 <= m <= 1 x1 < x2 Ti (r+1, q+1) t s Pi-1 (r,q) Si (r+1, q)

Algorithm of Scan Conversion for Line drawing Bresenham’s Development: In the above diagram we see a scenario which will be used to develop Bresenham’s line drawing algorithm. Assume that the point P(i-1) has just been correctly set, how do we determine the next point to be set By examination, if we evaluate the two values s and t, we simply compare their values: If s < t choose Si else choose Ti

Algorithm of Scan Conversion for Line drawing Bresenham’s Development: The development of the algorithm is somewhat simpler if we move the line (x1, x2, y1, y2) so that the left most point is on the origin. This is accomplished by translating the line by (-x1, -y1). If we remember that we have translated the line, we simply add (x1, y1) to the resulting pixel values to draw the original line. New Problem: The task of drawing the line (x1, y1, x2, y2) is thus reduced to drawing the line (0, 0, dx, dy), where dx = x2 –x1, and dy = y2 – y1. Looking at the above diagram we see that:

Algorithm of Scan Conversion for Line drawing Bresenham’s Development: New Problem: Looking at the above diagram we see that: s = m * (r+1) –q, or s = dy/dx * (r+1) –q t = (q+1) - dy/dx * (r+1) As mentioned earlier the decision whether to choose Ti or Si depends on the comparison of s and t If s<t choose Si, else choose Ti This decision can be reformulated as follows: If s-t < 0 choose Si, else choose T.

Algorithm of Scan Conversion for Line drawing Bresenham’s Development: Using the definition of s and t we can compute their difference, namely s – t = (dy/dx * (r+1) – q ) – (q+1 - dy/dx * (r+1)) = 2* dy/dx * (r+1) – 2q –1 Rearranging this equation, and multiplying by dx yields: dx(s-t) = 2dy * (r+1) – 2q * dx - dx or = 2 * (r * dy – q * dx) + 2 * dy – dx But: notice that the quantity dx is non-zero positive. This means we can replace the decision quantity s-t with dx(s-t) Also: notice that this quantity is an integer value

Algorithm of Scan Conversion for Line drawing Bresenham’s Development: Decision Variable Defining the quantity 2 * (r* dy – q * dx) + 2 * dy – dx as the decision variable Di, We can rewrite in terms of Xi-1 and Yi-1 Di = 2 * (Xi-1 * dy – Yi-1 * dx) + 2 * dy – dx Subtracting Di from Di+1 will tell us how to compute the next decision variable Di+1 – Di = 2* (dy ( Xi – Xi-1 ) – dx (Yi – Yi-1)) We call this error variable deltaD. We also know that Xi – Xi-1 = 1, so we have deltaD = 2dy – 2dx (Yi – Yi-1) Notice that this gives us two different deltaD’s depending on whether Yi – Yi-1is 1 or 0 The algorithm for drawing lines in the first octant is then described as follows:

Algorithm of Scan Conversion for Line drawing Bresenham’s Development: Bresenham’s Algorithm: Draw_line(x1, x2, y1, y2) Integer dx = x2 –x1 Integer dy = y2 – y1 d= 2dy –dx deltaD1 = 2dy deltaD2 = 2(dy – dx) y = y1 for x from x1 to x2 by incremental step 1 set_pixel(x, y) if d < 0 then d = d + deltaD1 else d = d + deltaD2 y = y + 1 end if end for end Draw_line

Algorithm of Scan Conversion for Line drawing Bresenham’s Development: Property of Bresenham’s Algorithm: In the discussion above we have made some assumptions: I.e., The slope of the line is between 0 and 1; The first point of the line is to the left of the second point; If we want to handle the cases in other octants: We can transform any line into the equivalent property as the case that first octant has. (e.g., swap the two end points; swap the x and y values, etc….) Simple integer arithmetic (addition operation). No floating operation, no multiplication, no rounding function. Extension to Mid-point algorithm (comparing the intersection point with mid-point)

Scan Converting Circles Brute-force method Equation of a circle centered at the origin with radius of r: x2 + y2 = r2 If the center is at (xc, yc), we may translate it to the origin, then apply scan conversion. y = +/- sqrt(r2 – x2) Not efficient: multiplication and square-root operation non-uniform: large gap at the place of the slope of the circle becoming infinite r yc xc

Scan Converting Circles Parametric Polar form: Equation of a circle centered at the origin with radius of r: x = r cos() y = r sin() If the center is at (xc, yc), we may translate it to the origin, then apply scan conversion. Using a fixed angular step size, plotting the circle with equally spaced points along the circumference. For example: The step angle delta() can be set as 1/r (= (2 ) / (2  r)) Not efficient: multiplication and trigonometric operation

Scan Converting Circles Symmetry between octants in a circle: Eight-way symmetry: we need to compute only one 45 degree sector (octant from X=0 to x = y) to determine the circle completely. Map octant I to other seven octants: I: (x,y) II: (y,x) III: (-y, x) IV: (-x, y) V: (-x, -y) VI: (-y, -x) VII: (y, -x) VIII: (x, -y) Reducing the computation greatly III II IV I V VIII VI VII

Scan Converting Circles Mid-point circle algorithm: Move Center (xc, yc) to (0, 0) Calculate the circle sector (octant II), where the slope of the curve varies from 0 to –1 Step in x positive direction, calculating the decision variable (or called decision parameter) to determine the y position in each x step. Derive the position in other seven octants by symmetry Move the center (0, 0) to (xc, yc)

Scan Converting Circles Mid-point circle algorithm: Definition of Circle function: fc(x, y) = x2 + y2 – r2 If (x, y) is on the circle boundary  fc(x, y) = 0 If (x, y) is inside the circle boundary  fc(x, y) < 0 If (x, y) is outside the circle boundary  fc(x, y) > 0

Scan Converting Circles Mid-point circle algorithm: Assume the (x[k], y[k]) is determined, we need to determine the pixel position of (x[k+1], y[k+1]), which is located either at (x[k]+1, y[k]) or at (x[k]+1, y[k]-1) Definition of Decision Variable P[k]: The circle function in the midpoint position P[k] = fc(x[k] + 1, y[k]- ½ ) = (x[k] + 1) 2 + (y[k] – ½ ) 2 – r2 If p[k] < 0  the midpoint is inside the circle  pixel on scan line y[k] is closer to the circle boundary  (x[k+1], y[k+1]) = (x[k] +1, y[k]) Else  the midpoint is on or outside of the circle  pixel on scan line y[k] –1 is selected  (x[k+1], y[k+1]) = (x[k] +1, y[k] - 1)

Scan Converting Circles Mid-point circle algorithm: y[k] midpoint x2 + y2 – r2 =0 y[k] -1 x[k] x[k+1]= x[k] + 1 x[k+2]= x[k] + 2 p[k]= fc(….) p[k+1] = fc(…)

Scan Converting Circles Mid-point circle algorithm: Successive decision variable P[k+1]: P[k+1] = fc(x[k+1] + 1, y[k+1]- ½ ) = (x[k] + 2)2 + (y[k+1] – ½ )2 – r2 p[k+1] = p[k] + 2(x[k]+1) + (y[k+1]2 – y[k]2 ) – (y[k+1] – y[k]) +1 If p[k] < 0  y[k+1] = y[k] Else  y[k+1] = y[k] –1 Incremental calculation of decision variable: If p[k] < 0  p[k+1] – p[k] = 2x[k+1] + 1 = 2x[k] + 3 Else  p[k+1] – p[k] = 2x[k+1] – 2y[k+1] + 1 = 2x[k] – 2y[k] + 5 Initial starting position at (x[0], y[0]) = (0, r) decision variable p[0] = fc(1, r- ½) = 5/4 - r

Scan Converting Circles Mid-point circle algorithm: /* Assume the center is at origin (0, 0) */ Input initial position of the starting point, circle center and radius Calculate initial decision variable p[0] While(x < y) { if p < 0  {p = p + 2x + 3;} else  {p = p + 2x – 2y + 5; y = y –1;} x = x+1; Set circle points in the other seven octants; }

Scan Converting Ellipse Mid-point ellipse algorithm: Standard ellipse function at center of (0, 0): (x/Rx) 2 + (y/Ry) 2 = 1 Ellipse function: fe(x, y) = Ry2 * x2 + Rx2 * y2 – Rx2 * Ry2 If (x, y) is on the ellipse boundary  Fe(x, y) =0 If (x, y) is inside the ellipse boundary  Fe(x, y) <0 If (x, y) is outside the ellipse boundary  Fe(x, y) >0

Scan Converting Ellipse Mid-point ellipse algorithm: Quadrant symmetry and two regions splitting within one quadrant: Region 1: the magnitude of the slope of the curve < 1 Region 2: the magnitude of the slope of the curve >= 1 If f(x, y) = 0  dy/dx = - (df/dx) / (df/dy) = - (2* Ry2 *x ) / (2* Rx2 * y) At the boundary between region 1 and region2: dy/dx = -1 So (2* Ry2 *x ) < (2* Rx2 * y)  region1 (2* Ry2 *x ) >= (2* Rx2 * y)  region2 Decision Variable (or called decision parameter): y Slope=1 Region 1 Ry x Rx Region 2

Scan Converting Ellipse Mid-point ellipse algorithm: Region 1: (draw region 1 until (2* Ry2 *x ) >= (2* Rx2 * y), increment in x direction) Decision Variable (or called decision parameter):  P1[k] = fe(x[k] +1, y[k] – ½) = Ry2 (x[k]+1) 2 + Rx2 (y[k]-1/2) 2 - Rx2 Ry2 If p1[k] < 0  y[k+1] = y[k] If p1[k] >= 0  y[k+1] = y[k] –1  P1[k+1] = fe(x[k+1] +1, y[k+1] – ½) Increment of decision variable: If p1[k] < 0  p1[k+1] – p1[k] = 2 Ry2 * x[k+1] + Ry2 If p1[k] >= 0  p1[k+1] – p1[k] = 2 Ry2 * x[k+1] + Ry2 – 2 Rx2 * y[k+1] Initial point: (x[0], y[0]) = (0, Ry), Initial decision parameter: p1[0] = Ry2 – Rx2 * Ry + (Rx2 /4)

Scan Converting Ellipse Mid-point ellipse algorithm: Region 2: (Draw region 2 until y <=0, increment in –y direction) Decision Variable (or called decision parameter):  P2[k] = fe(x[k] +1/2, y[k] – 1) If p2[k] > 0  x[k+1] = x[k], y[k+1] = y[k] –1 (midpoint is outside the ellipse boundary) If p2[k] <= 0  x[k+1] = x[k] +1, y[k+1] = y[k] –1 (midpoint is inside the ellipse boundary)  P2[k+1] = fe(x[k+1] +1/2, y[k+1] – 1) Increment of decision variable: If p2[k] > 0  p2[k+1] – p2[k] = -2 Rx2 * y[k+1] + Rx2 If p2[k] <= 0  p2[k+1] – p2[k] = 2 Ry2 * x[k+1] + Rx2 – 2 Rx2 * y[k+1] Initial point: (x[0], y[0]) = last point calculated in region 1 Initial decision parameter: p2[0] = Ry2 * (x[0] + ½) 2 – Rx2 * (y[0] –1) 2 – Rx2 *Ry2

Scan Converting Other Curves Conics (circle, ellipse, parabolas, hyperbolas: Ax2 + By2 + Cxy + Dx + Ey + F=0) trigonometric (e.g., periodical symmetry or repeating) exponential functions probability distributions (e.g., symmetry along the mean) general polynomial and spline curves y = a[0] + a[1] x + ….+ a[n-1] xn-1 + a[n] x n Quadratic: n=2 Cubic polynomial: n = 3 Quartic: n =4 Straight line: n = 1 E.g., Spline curves: formed by cubic polynomial pieces on a set of discrete points, Which is constrained by a certain smooth and continuous conditions Applications: E.g., object surface modeling; Animation path design data and function graphing and visualization

Scan Converting Other Curves Function representation: explicit y = f(x) parametric representation: x = f(); y = f()  implicit f(x, y) = 0 Drawing methods: explicit: direct drawing (brute-force) or Incremental method (DDA, mid-point, Bresenham) parametric: direct drawing (brute-force) or incremental method (DDA, mid-point, Bresenham)  implicit: f(x, y) = 0: Incremental method similar to the mid-point method For function of y = f(x): If the slope had magnitude <= 1, drawing in the x incremental direction If the slope had magnitude > 1, drawing in the y incremental direction Taking advantage of the symmetry property, if any Curve functions: example in OpenGL (curve and surface evaluator) Discrete data set: Approximation/curve-fitting and interpolation(passing through control points) Piece-wise line segment representation for curves and discrete data set

Screen grid & screen pixel Screen coordinate: Grid representation Pixel center representation Note: the decision variable can be measured by screen grid separation, Other than the pixel center differences. Transform the object description into screen description, pixel size cannot be ignored pixel grid

Issues to be considered Aliasing Jagged or stairstep appearance due to the raster algorithm The sampling process digitizes coordinate points on an object to discrete integer pixel positions. Low-frequency sampling (undersampling) Antialiasing Reducing the aliasing effect, improving the undersampling process.