

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Computer Graphics Raster Graphics: Drawing Lines
2D Graphics Pipeline
Object World Coordinates
Object subset
window to viewport mapping
Object Display Rasterization Screen coordinates
Applying world window
Clipping
Simple 2D Drawing Pipeline
n A fundamental computer graphics function
n Determine the pixels’ colors, illuminations, textures, etc.
n Implemented by graphics hardware
n Rasterization algorithms
n Lines n Circles n Triangles n Polygons
n Drawing lines on the screen
n Manipulating pixel maps (pixmaps): copying, scaling, rotating, etc
n Compositing images, defining and modifying regions
n Drawing and filling polygons
n Previously glBegin(GL_POLYGON), etc
n Aliasing and antialiasing methods
Line: (3,2) -> (9,6)
pixels to turn on?
Line Drawing Algorithm
n Slope-intercept line equation
n y = mx + b n Given two end points (x0,y0), (x1, y1), how to compute m and b?
(x0,y0)
(x1,y1)
dx
dy
1 0
1 0 x x
y y dx
dy m −
− = = b^ = y^0 − m * x^0
(x0,y0)
(x1,y1)
dx
dy
ßWalk through the line, starting at (x0,y0) ßConstrain x, y increments to values in [0,1] range ßCase a: x is incrementing faster (m < 1) ßStep in x=1 increments, compute and round y ßCase b: y is incrementing faster (m > 1) ßStep in y=1 increments, compute and round x
m<
m> m=
(x0, y0)
x = x0 + 1 y = y0 + 1 * m
Illuminate pixel (x, round(y))
x = x + 1 y = y + 1 * m
Illuminate pixel (x, round(y))
…
Until x == x
(x1,y1)
x = x0 y = y
Illuminate pixel (x, round(y))
y (^) k + 1 = yk + m
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);
}
n Note: setPixel(x, y) writes current color into pixel in column x and row y in frame buffer
n DDA is the simplest line drawing algorithm n Not very efficient n Round operation is expensive n Optimized algorithms typically used. n Integer DDA n E.g.Bresenham algorithm (Hill, 10.4.1) n Bresenham algorithm n Incremental algorithm: current value uses previous value n Integers only: avoid floating point arithmetic n Several versions of algorithm: we’ll describe midpoint version of algorithm
n Based on assumptions: n W, H are +ve n H < W n As x steps in +1 increments, y incr/decr by <= +/– n y value sometimes stays same, sometimes increases by 1 n Midpoint algorithm determines which happens
(x0, y0)
M = (x0 + 1, Y0 + ½)
Build equation of line through and compare to midpoint
…
(x1,y1) What Pixels to turn on or off? Consider pixel midpoint M(Mx, My)
M(Mx,My)
If midpoint is above line, y stays same If midpoint is below line, y increases + 1
n So, F(x,y) = -2W(y – Ay) + 2H(x – Ax) n Algorithm, If: n F(x, y) < 0, (x, y) above line n F(x, y) > 0, (x, y) below line n Hint: F(x, y) = 0 is on line n Increase y keeping x constant, F(x, y) becomes more negative
n Example: to find line segment between (3, 7) and (9, 11)
F(x,y) = -2W(y – Ay) + 2H(x – Ax) = (-12)(y – 7) + (8)(x – 3)
n For points on line. E.g. (7, 29/3), F(x, y) = 0 n A = (4, 4) lies below line since F = 44 n B = (5, 9) lies above line since F = -