Lecture02-LineDrawing 计算机图形学ppt课件
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
b = x0 – m * y0;
// Compute x-intercept.
...
// Etc.
}
}
Basic Algorithm (cont.)
• Basic algorithm now correct But slow
▪ Slope-intercept equation (floating point multiply) inside the loop
// 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?
// Start at (x0, y0). // Absolute slope is less than 1?
double m = (double)dy / (double)dx, // Compute the slope.
b = y0 – m * x0;
// Compute y-intercept.
▪ Function call (floor) inside the loop
• Most line drawing code has the general form:
void line(int x0, int y0, int x1, int y1, Color c) { // 1. General setup. // 2. Slope case setup.
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); if (dx != 0) {
// Start at (x0, y0).
while (/* Not done */) { // 3. Inner loop.
} }
// Loop.
• Can we move some computation outside the loop?
▪ The remaining discussion will assume that m 1
Incremental/DDA Algorithm
x = x0 + 0.5; ... } }
// Start at (x0, y0). // Absolute slope is less than 1? // Compute the slope. // Initialize rounded y value. // Set x increment. // Set y increment. // Loop: // Increment x. // Compute y incrementally. // Draw point (truncating y).
Basic Algorithm (cont.)
Problem: Doesn’t work if slope > 1
Creates gaps since x gets incremented by 1 each iteration
(x1,y1)
(x0,y0)
“Ideal” line
Basic Algorithm (cont.)
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
(x(2),y(2)) = (x(2),y(1)+m)
(5,0) y
DDA Code
void lineDDA(int x0, int y0, int x1, int y1, Color c) { int dx = x1 – x0, dy = y1 – y0;
drawPoint(x0, y0, c); if (abs(dx) > abs(dy)) {
(x0,y0)
“Ideal” line
(x1,y1)
Basic Algorithm
Use implicit (slope intercept) form of a line:
y = mx + b Given two endpoints (x0, y0) and (x1, y1)
m = Dy/Dx 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
xy 13 2 2.25 (rounded to 2) 3 1.5 (rounded to 2) 4 0.75 (rounded to 1) 50
x
(x(0),y(0)) = (x0, y0) (1,3)
(x(1),y(1)) (x(2),Round(y(2) ))
(x(1),Round(y(1)))
• Most incremental line-drawing (and other scan-conversion) algorithms were first developed for pen-plotters.
▪ Most attributed to Jack Bresenham.
Screen Coordinates
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.
(x0,y0)
Which pixels do I illuminate?
“Ideal” line (x1,y1)
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)
• What about if x0 > x1?
▪ Reverse the roles of (x0, y0) and (x1, y1).
Case 3
Standard Case
DDA Example
Line: (1,3) (5,0)
dx = 4 dy = -3
xincr = 1 yincr = m = -¾
• 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.
We will use the following 2-D screen coordinate API:
▪ Pixel centers are at
integer coordinates.
y
▪ The y-axis points up,
x-axis points right
(3,2)
(0,1)
x (0,0) (1,0) (2,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) {
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.
drawPoint(x0, y0, c);
// Draw point.
}
}
Why check if dy == 0?
else if (dy != 0) {
// |dy| > |dx| exchange x and y.
double m = (double)dx / (double)dy, // Compute the slope.
• Reduces several computations
Incremental/DDA Algorithm
• What about –1 m 0 (case 2)
▪ m will be negative, so it will be subtracted from the previous y
Case 4
• What about 1 m ? (case 3)
▪ Want to increment y, then compute x. Reverse the roles of x and y.
Case 2
• What about - m -1? (case 4)
▪ Want to increment y, then compute x. M will be negative so it will be subtracted from the previous x
• DDA (Digital Differential Algorithm) uses an incremental approach to compute y.
▪ Compute m ▪ Draw (x0, y0) ▪ For each iteration:
- Increment x - Add m to y - Draw (x, (round)y)
double m = (double)dy / (double)dx, y = y0 + 0.5;
dx = (x1 > x0) ? 1 : -1; m *= dx; while (x0 != x1) {
x0 += dx; y += m; drawPoint(x0, (int)y, c); } } else if (dy != 0) { double m = (double)dx / (double)dy,
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); if (abs(dx) > abs(dy)) {
• In General:
▪ At x(k+1): y(k+1) = m(x0+k+1) + b = mx0 + km + m + b = y0 + km = yk + m
▪ Also: - xk = x(k) - yk = round(y(k))
Incremental/DDA Algorithm
• What is y at x(0) = x0?
▪ At x(0): y(0) = mx0 + b
• At x(1) = x0+1?
▪ At x(1): y(1) = m(x0+1) + b = mx0 + m + b = y0 + m
• At x(2) = x0+2?
▪ At x(2): y(2) = m(x0+2) + b = mx0 + 2m + b = y0 + 2m = y1 + m