Docsity

Drawing Lines - Computer Graphics - Lecture Slides, Slides of Computer Graphics

Partial preview of the text

Download Drawing Lines - Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!

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

Rasterization

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

Rasterization Operations

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 Drawing Algorithm

Line: (3,2) -> (9,6)

? Which intermediate

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

Digital Differential Analyzer (DDA): Line Drawing

Algorithm

(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=

DDA Line Drawing Algorithm (Case a: m < 1)

(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

DDA 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);

}

n Note: setPixel(x, y) writes current color into pixel in column x and row y in frame buffer

Line Drawing Algorithm Drawbacks

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

Bresenham’s Line-Drawing 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

Bresenham’s Line-Drawing Algorithm

(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

Bresenham’s Line-Drawing Algorithm

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

Bresenham’s Line-Drawing Algorithm

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