1 / 42

Computer Graphics

Computer Graphics. Drawing Line. 2D Object. Polylines : lines drawn between ordered points. Lines and Polylines. Same first and last point make closed polyline or polygon If it does not intersect itself, called simple polygon. Convex vs. Concave Polygons.

small
Download Presentation

Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Graphics Drawing Line

  2. 2D Object • Polylines: lines drawn between ordered points Lines and Polylines • 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.

  3. triangle square rectangle 2D Object Special polygons Circles • Consist of all points equidistant from one predetermined point (the center) • (radius) r = c, where c is a constant • On a Cartesian grid with center of circle at origin equation is r2 = x2 + y2 P1 y r r P0 0 x

  4. 6 6 5 5 4 4 3 3 2 2 1 1 0 0 1 2 3 4 5 6 7 8 9 10 10 1 3 4 5 6 7 8 9 2 2D Object Circle as polygon • A circle can be approximated by a polygon with many sides (>15) (Aligned) Ellipses A circle scaled along the x or y axis Example: height, on y-axis, remains 3, while length, on x-axis, changes from 3 to 6

  5. 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 2D to 3D Object

  6. Building 3D Primitives • Triangles and tri-meshes • Parametric polynomials, like the aforementioned splines used to define surface patches.

  7. 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

  8. Vectors • A vector is a list of n numbers referring to a direction (and magnitude) in n-D

  9. Rays • A ray is just a vector with a starting point • Ray = (Point, Vector)

  10. 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

  11. Planes • How can we define a plane? • 3 non-linear points • Use linear interpolation • A perpendicular vector and an incident point • n • (x-x0) = 0 • ax + by + cz + d = 0 • Hessian normal form: Normalize n first • n • x = - p

  12. Raster Graphics • Image produced as an array (the raster) of picture elements (pixels) in the frame buffer

  13. 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”

  14. (x2, y2) (x1, y1) Line Drawing • A classic part of the computer graphics curriculum • Input: • Line segment definition • (x1, y1), (x2, y2) • Output: • List of pixels

  15. How Do They Look? • So now we know how to draw lines • But they don’t look very good:

  16. 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

  17. Simple Line Based on slope-intercept algorithm from algebra: y = mx + b Simple approach: increment x, solve for y Floating point arithmetic required

  18. 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)

  19. 8 7 6 5 4 3 2 1 ? 0 1 2 3 4 5 6 7 8 9 10 11 12 Line Drawing Algorithm Line: (3,2) -> (9,6) Which intermediate pixels to turn on?

  20. (x1,y1) dy (x0,y0) dx Line Drawing Algorithm • Slope-intercept line equation • y = mx + b • Given two end points (x0,y0), (x1, y1), how to compute m and b?

  21. 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

  22. Line Drawing Algorithm • Numerical example of finding slope m: • (Ax, Ay) = (23, 41), (Bx, By) = (125, 96)

  23. 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 – 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

  24. 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

  25. Example Code // Incremental Line Algorithm // Assume x0 < x1 void Line(int x0, int y0, int x1, int y1) { int x, y; float dy = y1 – y0; float dx = x1 – x0; float m = dy / dx; y = y0; for (x = x0; x < x1; x++) { WritePixel(x, Round(y)); y = y + m; } }

  26. Line Drawing Algorithm Pseudocode compute m; if m < 1: { float y = y0; // initial value for(int x = x0;x <= x1; x++, y += m) setPixel(x, round(y)); } else // m > 1 { float x = x0; // initial value for(int y = y0;y <= y1; y++, x += 1/m) setPixel(round(x), y); } • Note: setPixel(x, y) writes current color into pixel in column x and row y in frame buffer

  27. Observation on lines. while( n-- ) { draw(x,y); move right; if( below line ) move up; } Bresenham Mid-Point algorithm

  28. 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.

  29. Testing for the side of a line. • Need to find coefficients a,b,c. • Recall explicit, slope-intercept form : • So:

  30. 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

  31. But recall : So : Mid-Point Algorithm Evaluate d for next pixel, Depends on whether E or NE Is chosen : If E chosen : NE M E Previous Pixel (xp,yp) Choices for Next pixel Choices for Current pixel

  32. So : Decision variable. If NE was chosen : M NE E Previous Pixel (xp,yp) Choices for Next pixel Choices for Current pixel

  33. 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 (x1,y1). • Need to calculate the initial value for d

  34. Initial value of d. Start point is (x1,y1) But (x1,y1) is a point on the line, so F(x1,y1) =0 Conventional to multiply by 2 to remove fraction  doesn’t effect sign.

  35. algorithm void MidpointLine(int x1,y1,x2,y2) { int dx=x2-x1; int dy=y2-y1; int d=2*dy-dx; int increE=2*dy; int incrNE=2*(dy-dx); x=x1; y=y1; WritePixel(x,y); while (x < x2) { if (d<= 0) { d+=incrE; x++ } else { d+=incrNE; x++; y++; } WritePixel(x,y); } }

  36. 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.

  37. 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

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

  39. 18 17 16 15 14 13 12 11 10 20 21 22 23 24 25 26 27 28 29 30 Bresenham Exercise

  40. OpenGL • Glbegin ( GL_LINES); • glVertex2iv (p1); • glVertex2iv (p2); • glVertex2iv (p3); • glVertex2iv (p4); • glVertex2iv (p5); • glEnd ( );

  41. OpenGL • Glbegin ( GL_LINE_STRIP); • glVertex2iv (p1); • glVertex2iv (p2); • glVertex2iv (p3); • glVertex2iv (p4); • glVertex2iv (p5); • glEnd ( );

  42. OpenGL • Glbegin ( GL_LINE_LOOP); • glVertex2iv (p1); • glVertex2iv (p2); • glVertex2iv (p3); • glVertex2iv (p4); • glVertex2iv (p5); • glEnd ( );

More Related