Computer Graphics Drawing Line 2 D Object Lines
Computer Graphics Drawing Line
2 D Object Lines and Polylines • Polylines: lines drawn between ordered points • Same first and last point make closed polyline or polygon • If it does not intersect itself, called simple polygon Convex vs. Concave Polygons Convex: For every pair of points in the polygon, the line between them is fully contained in the polygon. Concave: Not convex: some two points in the polygon are joined by a line not fully contained in the polygon.
2 D Object Special polygons triangle square rectangle Circles • • Consist of all points equidistant from one predetermined point (the center) (radius) r = c, where c is a constant P 0 • r P 1 y r 0 x On a Cartesian grid with center of circle at origin equation is r 2 = x 2 + y 2
2 D Object Circle as polygon • A circle can be approximated by a polygon with many sides (>15) (Aligned) Ellipses 6 5 4 3 2 1 A circle scaled along the x or y axis 0 1 2 3 4 5 6 7 8 9 10 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 Example: height, on y-axis, remains 3, while length, on x-axis, changes from 3 to 6
2 D to 3 D Object Vertices in motion (“Generative object description”) • • Line is drawn by tracing path of a point as it moves (one dimensional entity) Square drawn by tracing vertices of a line as it moves perpendicularly to itself (two dimensional entity) • Cube drawn by tracing paths of vertices of a square as it moves perpendicularly to itself (three-dimensional entity) • Circle drawn by swinging a point at a fixed length around a center point
Building 3 D Primitives • Triangles and tri-meshes • Parametric polynomials, like the aforementioned splines used to define surface patches.
Points • A point is a list of n numbers referring to a location in n-D • The individual components of a point are often referred to as coordinates – i. e. (2, 3, 4) is a point in 3 -D space • This point’s x-coordinate is 2, it’s y-coordinate is 3, and it’s z-coordinate is 4
Vectors • A vector is a list of n numbers referring to a direction (and magnitude) in n-D
Rays • A ray is just a vector with a starting point – Ray = (Point, Vector)
Points and Vectors • Can define a vector by 2 points – Point - Point = Vector • Can define a new point by a point and a vector – Point + Vector = Point
Planes • How can we define a plane? – 3 non-linear points • Use linear interpolation – A perpendicular vector and an incident point • n • (x-x 0) = 0 – ax + by + cz + d = 0 • Hessian normal form: Normalize n first – n • x = - p
Raster Graphics • Image produced as an array (the raster) of picture elements (pixels) in the frame buffer
Rasterization • In the rasterization step, geometry in device coordinates is converted into fragments in screen coordinates • After this step, there are no longer any “polygons”
Line Drawing • A classic part of the computer graphics (x 2, y 2) curriculum • Input: – Line segment definition • (x 1, y 1), (x 2, y 2) • Output: – List of pixels (x 1, y 1)
How Do They Look? • So now we know how to draw lines • But they don’t look very good:
Towards the Ideal Line • We can only do a discrete approximation • Illuminate pixels as close to the true path as possible, consider bi-level display only – Pixels are either lit or not lit 16
Simple Line Based on slope-intercept algorithm from algebra: y = mx + b Simple approach: increment x, solve for y Floating point arithmetic required
Line drawing algorithm • Need algorithm to figure out which intermediate pixels are on line path • Pixel (x, y) values constrained to integer values • Rounding may be required. E. g. computed point (10. 48, 20. 51) rounded to (10, 21)
Line Drawing Algorithm 8 7 6 5 4 3 2 1 Line: (3, 2) -> (9, 6) ? 0 1 2 3 4 5 6 7 8 9 10 11 12 Which intermediate pixels to turn on?
Line Drawing Algorithm • Slope-intercept line equation – y = mx + b – Given two end points (x 0, y 0), (x 1, y 1), how to compute m and b? (x 1, y 1) dy (x 0, y 0) dx
Does it Work? It seems to work okay for lines with a slope of 1 or less, but doesn’t work well for lines with slope greater than 1 – lines become more discontinuous in appearance and we must add more than 1 pixel per column to make it work. Solution? - use symmetry. increment along x-axis if dy<dx else increment along y-axis
Line Drawing Algorithm • Numerical example of finding slope m: • (Ax, Ay) = (23, 41), (Bx, By) = (125, 96)
Incremental Algorithm Basic Algorithm • Find equation of line that connects two points P and Q • Starting with leftmost point P, increment xi by 1 to calculate yi = m*xi + B where m = slope, B = y intercept • Draw pixel at (xi, Round(yi)) where Round (yi) = Floor (0. 5 + yi) Incremental Algorithm: • Each iteration requires a floating-point multiplication – Modify algorithm to use deltas – (yi+1 – yi) = m*(xi+1 - xi) + B – yi+1 = yi + m*(xi+1 – xi) – If x = 1, then yi+1 = yi + m • At each step, we make incremental calculations based on preceding step to find next y value
Incremental Algorithm • • Start at t = 0 At each step, increment t by dt Choose appropriate value for dt Ensure no pixels are missed: – Implies: and • Set dt to maximum of dx and dy
Example Code // Incremental Line Algorithm // Assume x 0 < x 1 void Line(int x 0, int y 0, int x 1, int y 1) { int x, y; float dy = y 1 – y 0; float dx = x 1 – x 0; float m = dy / dx; y = y 0; for (x = x 0; x < x 1; x++) { Write. Pixel(x, Round(y)); y = y + m; } }
Line Drawing Algorithm Pseudocode compute m; if m < 1: { float y = y 0; // initial value for(int x = x 0; x <= x 1; x++, y += m) set. Pixel(x, round(y)); } else // m > 1 { float x = x 0; // initial value for(int y = y 0; y <= y 1; y++, x += 1/m) set. Pixel(round(x), y); } • Note: set. Pixel(x, y) writes current color into pixel in column x and row y in frame buffer
Observation on lines. while( n-- ) { draw(x, y); move right; if( below line ) move up; } Bresenham Mid-Point algorithm
Testing for the side of a line. • Need a test to determine which side of a line a pixel lies. • Write the line in implicit form: • Easy to prove F<0 for points above the line, F>0 for points below.
Testing for the side of a line. • Need to find coefficients a, b, c. • Recall explicit, slope-intercept form : • So:
Decision variable. Evaluate F at point M Referred to as decision variable NE M E Previous Pixel (xp, yp) Choices for Current pixel Choices for Next pixel
Mid-Point Algorithm Evaluate d for next pixel, Depends on whether E or NE Is chosen : If E chosen : But recall : NE M E Previous Pixel (xp, yp) Choices for Current pixel Choices for Next pixel So :
Decision variable. If NE was chosen : M NE E Previous Choices for Pixel Next pixel Current pixel (xp, yp) So :
Summary of mid-point algorithm • Choose between 2 pixels at each step based upon the sign of a decision variable. • Update the decision variable based upon which pixel is chosen. • Start point is simply first endpoint (x 1, y 1). • Need to calculate the initial value for d
Initial value of d. Start point is (x 1, y 1) But (x 1, y 1) is a point on the line, so F(x 1, y 1) =0 Conventional to multiply by 2 to remove fraction doesn’t effect sign.
algorithm void Midpoint. Line(int x 1, y 1, x 2, y 2) { int dx=x 2 -x 1; int dy=y 2 -y 1; int d=2*dy-dx; int incre. E=2*dy; int incr. NE=2*(dy-dx); x=x 1; y=y 1; Write. Pixel(x, y); while (x < x 2) { if (d<= 0) { d+=incr. E; x++ } else { d+=incr. NE; x++; y++; } Write. Pixel(x, y); } }
It was (not) the end! 2 -step algorithm by Xiaolin Wu: Treat line drawing as an automaton , or finite state machine, ie. looking at next two pixels of a line, easy to see that only a finite set of possibilities exist. The 2 -step algorithm exploits symmetry by simultaneously drawing from both ends towards the midpoint.
Two-step Algorithm Possible positions of next two pixels dependent on slope – current pixel in blue: Slope between 0 and ½ Slope between ½ and 1 Slope between 1 and 2 Slope greater than 2
Bresenham Exercise • Go through the steps of the Bresenham line drawing algorithm for a line going from (21, 12) to (29, 16)
Bresenham Exercise 18 k 17 0 16 1 15 2 14 3 4 13 5 12 6 11 7 10 8 20 21 22 23 24 25 26 27 28 29 30 pk (xk+1, yk+1)
Open. GL • • Glbegin ( GL_LINES); gl. Vertex 2 iv (p 1); gl. Vertex 2 iv (p 2); gl. Vertex 2 iv (p 3); gl. Vertex 2 iv (p 4); gl. Vertex 2 iv (p 5); gl. End ( ); 40
Open. GL • • Glbegin ( GL_LINE_STRIP); gl. Vertex 2 iv (p 1); gl. Vertex 2 iv (p 2); gl. Vertex 2 iv (p 3); gl. Vertex 2 iv (p 4); gl. Vertex 2 iv (p 5); gl. End ( ); 41
Open. GL • • Glbegin ( GL_LINE_LOOP); gl. Vertex 2 iv (p 1); gl. Vertex 2 iv (p 2); gl. Vertex 2 iv (p 3); gl. Vertex 2 iv (p 4); gl. Vertex 2 iv (p 5); gl. End ( ); 42
- Slides: 42