210 likes | 679 Views
Line Algorithm. Pradondet Nilagupta Dept. of Computer Engineering Kasetsart University. Why Lines?. Lines most common 2D primitive - done 100s or 1000s of times each frame even 3D wireframes are eventually 2D lines!
E N D
Line Algorithm Pradondet Nilagupta Dept. of Computer Engineering Kasetsart University 204481 Foundation of Computer Graphics
Why Lines? Lines • most common 2D primitive - done 100s or 1000s of times each frame • even 3D wireframes are eventually 2D lines! • optimized algorithms contain numerous tricks/techniques that help in designing more advanced algorithms 204481 Foundation of Computer Graphics
Line Requirements • Must compute integer coordinates of pixels which lie on or near a line or circle. • Pixel level algorithms are invoked hundreds or thousands of times when an image is created or modified – must be fast! • Lines must create visually satisfactory images. • Lines should appear straight • Lines should terminate accurately • Lines should have constant density • Line algorithm should always be defined. 204481 Foundation of Computer Graphics
Cartesian Coordinate System 6 P = (X,Y) 5 4 P2 = (X2,Y2) 3 2 1 P1 = (X1,Y1) 5 1 2 3 4 6 RISE Y2-Y1 SLOPE = = RUN X2-X1 Basic Line Equation Given two points P1=(X1,Y1), P2=(X2, Y2) Consider a third point on the line: P = (X,Y) Y = [(Y2-Y1)/(X2-X1)]*(X-X1)+ Y1 or Y = mx + b 204481 Foundation of Computer Graphics
Other Helpful Formulas • Length of line segment between P1 and P2: • Midpoint of a line segment between P1 and P3: • Two lines are perpendicular iff 1) M1 = -1/M2 2) Cosine of the angle between them is 0. 204481 Foundation of Computer Graphics
Parametric Form Given points P1 = (X1, Y1) and P2 = (X2, Y2) X = X1 + t(X2-X1) Y = Y1 + t(Y2-Y1) We get all the other points on the line segment between (X1,Y1) and (X2,Y2) as 0 < t < 1 204481 Foundation of Computer Graphics
Simple DDA* Line Algorithm • The digital differential analyzer (DDA) is a scan-conversion line algorithm based on calculating either y or x where y = y2-y1 x = x1-x2 • For lines with a positive slope greater than 1 Xk+1 = Xk +1/m • For lines with a positive slope less than 1 Yk+1 = Yk + m 204481 Foundation of Computer Graphics
DDA Code #include “device.h” #define ROUND (a) ((int) (a+0.5)) void lineDDA( int xa, int ya, int xb, int yb) { int dx = xb-xa, dy = yb – ya, steps, k; float xinc, yinc, x = xa, y = ya; if (abs(dx) > abs(dy)) steps = abs(dx); else steps = abs(dy); xinc = dx/(float) steps; yinc = dy/(float) steps; setPixel(ROUND(x), ROUND(y)); for (k=0; k<steps; k++) { x += xinc; y+= yinc; setPixel(ROUND(x), ROUND(y)); } DDA create good lines but it is too time consuming due to the round function and long operation on real values 204481 Foundation of Computer Graphics
13 12 11 10 9 6 11 7 8 9 10 12 13 DDA Example Compute which pixels should be turned on to represent the line from (6,9) to (11,12). step = ? xinc = ? yinc = ? 204481 Foundation of Computer Graphics
DDA Example Line from (6,9) to (11,12). step := Max of (ABS(11-6), ABS(12-9)) = 5 Xinc := 1 Yinc := 0.6 Values computed are: (6,9), (7,9.6), (8,10.2), (9,10.8), (10,11.4), (11,12) 204481 Foundation of Computer Graphics
Bresenham’s Line Drawing Algorithm 1. Input the two line endpoints and store the left endpoint in (X0,Y0) 2. Load (X0,Y0) into the frame buffer; that is, plot the first point. 3. Calculate constants X, Y, 2Y, and 2Y - 2X, and obtain the starting value for the decision parameter as P0 = 2Y - X 4. 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 Pk+1 = Pk + 2Y Otherwise, the next point to plot is (Xk + 1, Yk + 1) and Pk+1 = Pk + 2Y - 2X 5. Repeat step 4, X times. 204481 Foundation of Computer Graphics
Bresenham’s Line Algorithm #include “device.h” void lineBres( int xa, int ya, int xb, int yb) { int dx = abs(xb-xa), dy = abs(yb – ya); int p = 2*dy-dx; int twoDy = 2*dy, twoDyDx = 2* (dy-dx); int x,y,xEnd; /* Determine which point to use as start, which as end */ if (xa > xb){ x = xb; y = yb; xEnd = xa; } else { x = xa; y = ya; xEnd = xb; } setPixel(x,y); while (x < xEnd){ x++; if (p < 0) p += twoDy; else { y++; p += twoDyDx;} setPixel(x,y); } } 204481 Foundation of Computer Graphics
Example We digitize the line with endpoints (20,10) and (30,18) X= 10, Y = 8, 2Y = 16, 2Y - 2X = -4 p0 = 2Y - X = 6 204481 Foundation of Computer Graphics
The aliasing problem • Aliasing is cause by finite addressability of the display. • Approximation of lines and circles with discrete points often gives a staircase appearance or "Jaggies". Aliased rendering of the line Desired line 204481 Foundation of Computer Graphics
Antialiasing - solutions • Aliasing can be smoothed out by using higher addressability. • If addressability is fixed but intensity is variable, use the intensity to control the address of a "virtual pixel". Two adjacent pixels can be be used to give the impression of a point part way between them. The perceived location of the point is dependent upon the ratio of the intensities used at each. The impression of a pixel located halfway between two addressable points can be given by having two adjacent pixels at half intensity. • An antialiased line has a series of virtual pixels each located at the proper address. 204481 Foundation of Computer Graphics
Aliasing / Antialiasing Examples 204481 Foundation of Computer Graphics