Lecture04-LineDrawing

# Lecture04-LineDrawing - CS 455 – Computer Graphics Line...

This preview shows pages 1–10. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 455 – Computer Graphics Line Drawing Line Drawing • Scan-conversion or rasterization : Determining which pixels to illuminate - due to the scanning nature of raster displays . • Algorithms are fundamental to both 2-D and 3-D computer graphics. • Transforming the continuous into the discrete (sampling) . • Line drawing was easy for vector displays. • Most incremental line-drawing (and other scan-conversion) algorithms were first developed for pen-plotters. Most attributed to Jack Bresenham. Screen Coordinates We will use the following 2-D screen coordinate API: Pixel centers are at integer coordinates. The y-axis points up, x-axis 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 y-intercept. 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 y-intercept. dx = (x1 > x0) ? 1 : -1; // Set x increment. while (x0 != x1) { // Loop: x0 += dx; // Increment x ....
View Full Document

{[ snackBarMessage ]}

### Page1 / 51

Lecture04-LineDrawing - CS 455 – Computer Graphics Line...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online