Download presentation
Presentation is loading. Please wait.
Published byLesley Chambers Modified over 9 years ago
1
Computer Graphics Drawing Line
2
Lines and Polylines Convex: For every pair of points in the polygon, the line between them is fully contained in the polygon. Convex vs. Concave Polygons Concave: Not convex: some two points in the polygon are joined by a line not fully contained in the polygon. 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 2D Object
3
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 r 2 = x 2 + y 2 trianglesquare rectangle P0P0 P1P1 r 0 y x r Special polygons 2D Object
4
Circle as polygon A circle can be approximated by a polygon with many sides (>15) 0 1 1 2 2 3 4 5 6 7 8 9 10 3 4 5 6 0 1 1 2 2 3 4 5 6 7 8 9 3 4 5 6 (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 2D Object
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
Triangles and tri-meshes Parametric polynomials, like the aforementioned splines used to define surface patches. Building 3D Primitives
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-x 0 ) = 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
Line Drawing A classic part of the computer graphics curriculum Input: –Line segment definition (x 1, y 1 ), (x 2, y 2 ) Output: –List of pixels (x 1, y 1 ) (x 2, y 2 )
15
How Do They Look? So now we know how to draw lines But they don’t look very good:
16
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
Line Drawing Algorithm 0 1 2 3 4 5 6 7 8 9 10 11 12 8765432187654321 Line: (3,2) -> (9,6) ? Which intermediate pixels to turn on?
20
Line Drawing Algorithm Slope-intercept line equation –y = mx + b –Given two end points (x0,y0), (x1, y1), how to compute m and b? (x0,y0) (x1,y1) dx dy
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
Basic Algorithm Find equation of line that connects two points P and Q Starting with leftmost point P, increment x i by 1 to calculate y i = m*x i + B where m = slope, B = y intercept Draw pixel at (x i, Round(y i )) where Round (y i ) = Floor (0.5 + y i ) Incremental Algorithm: Each iteration requires a floating-point multiplication –Modify algorithm to use deltas –(y i+1 – y i ) = m*(x i+1 - x i ) + B – B –y i+1 = y i + m*(x i+1 – x i ) –If x = 1, then y i+1 = y i + m At each step, we make incremental calculations based on preceding step to find next y value Incremental Algorithm
24
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) { intx, y; floatdy = y1 – y0; floatdx = x1 – x0; floatm = 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. Previous Pixel (x p,y p ) Choices for Current pixel Choices for Next pixel Evaluate F at point M Referred to as decision variable M NE E
31
Mid-Point Algorithm Evaluate d for next pixel, Depends on whether E or NE Is chosen : If E chosen : But recall : So : M E NE Previous Pixel (x p,y p ) Choices for Current pixel Choices for Next pixel
32
Decision variable. If NE was chosen : So : M E NE Previous Pixel (x p,y p ) Choices for Current pixel Choices for Next 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 (x 1, y 1 ). Need to calculate the initial value for d
34
Initial value of d. 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. Start point is (x 1, y 1 )
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
Bresenham Exercise kpkpk (x k+1,y k+1 ) 012345678012345678
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.