This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 455 – Computer Graphics Line Drawing Line Drawing • Scanconversion or rasterization : Determining which pixels to illuminate  due to the scanning nature of raster displays . • Algorithms are fundamental to both 2D and 3D computer graphics. • Transforming the continuous into the discrete (sampling) . • Line drawing was easy for vector displays. • Most incremental linedrawing (and other scanconversion) algorithms were first developed for penplotters. Most attributed to Jack Bresenham. Screen Coordinates We will use the following 2D screen coordinate API: Pixel centers are at integer coordinates. The yaxis points up, xaxis points right x y (0,0) (1,0) (2,0) (0,1) (3,2) Goal • Given integer descriptions of the endpoints of a line, produce a rasterized line • The line may not fall on the discretized pixels Usually won’t So, we must choose the nearest pixels to illuminate Which pixels do I illuminate? “Ideal” line ( x , y ) ( x 1 , y 1 ) The Ideal Line • Important qualities for a line: Continuous appearance (no breaks – diagonal “steps” OK) Uniform thickness and brightness Accuracy (illuminate pixels nearest the ideal line) Speed (how fast is the line drawn) Consistency (drawn the same in both directions) “Ideal” line ( x , y ) ( x 1 , y 1 ) Basic Algorithm Use implicit (slope intercept) form of a line: y = mx + b Given two endpoints (x , y ) and (x 1 , y 1 ) m = ∆ y/ ∆ x We will step through the line, incrementing x each iteration Then let xinc = 1 (step 1 in x each iteration, then compute y), With y = mx + b Basic Algorithm void lineBasic(int x0, int y0, int x1, int y1, Color c) { int dx = x1 – x0, dy = y1 – y0; drawPoint(x0, y0, c); // Start at ( x , y ). if (dx != 0) { double m = (double)dy / (double)dx, // Compute the slope. b = y0 – m * x0; // Compute yintercept. dx = (x1 > x0) ? 1 : 1; // Set x increment. while (x0 != x1) { // Loop: x0 += dx; // Increment x . y0 = (int)floor(m * x0 + b + 0.5); // Compute y and // round. drawPoint(x0, y0, c); // Draw point. } } } • Does it work? • How about speed? Basic Algorithm (cont.) Problem: Doesn’t work if slope > 1 Creates gaps since x gets incremented by 1 each iteration “Ideal” line ( x , y ) ( x 1 , y 1 ) Basic Algorithm (cont.) Solution: check slope void lineBasic(int x0, int y0, int x1, int y1, Color c) { int dx = x1 – x0, dy = y1 – y0; drawPoint(x0, y0, c); // Start at ( x , y ). if (abs(dx) > abs(dy)) { // Absolute slope is less than 1? double m = (double)dy / (double)dx, // Compute the slope. b = y0 – m * x0; // Compute yintercept. dx = (x1 > x0) ? 1 : 1; // Set x increment. while (x0 != x1) { // Loop: x0 += dx; // Increment x ....
View
Full Document
 Winter '08
 Jones,M
 dy, Einc

Click to edit the document details