cs-321 dr. mark l. hornick 1 line drawing algorithms
TRANSCRIPT
CS-321Dr. Mark L. Hornick
1
Line Drawing Algorithms
CS-321Dr. Mark L. Hornick
2
Line Drawing Algorithms
(x0, y0)
(x1, y1)
Which pixels should be set to represent the line?
CS-321Dr. Mark L. Hornick
3
Line Equations
(x0, y0)
(x1, y1)
1 0
1 0
0 0
0 0
y mx b
y ym
x x
y y m x x
y m x x y
CS-321Dr. Mark L. Hornick
4
Simple Algorithm
Start x at origin of line Evaluate y value Set nearest pixel (x,y) Move x one pixel
toward other endpoint Repeat until done
0 0y m x x y
CS-321Dr. Mark L. Hornick
5
Implementation
How does this work?
// SimplePixelCalc - elementary pixel calculation using// variation of y=mx+bvoid SimplePixelCalc( int x0, int xn, int y0, int yn ){
for( int ix=x0; ix<=xn; ix++ ){
float fy = y0 + float(yn-y0)/float(xn-x0)*ix; // round to nearest whole integerint iy = (int)(fy+0.5);SetPixel(ix, iy, 1); // write pixel value
}}
How well does this
work?
CS-321Dr. Mark L. Hornick
6
Use the lab1 program on the following coordinates
Example 1 (x0, y0) = (0,1) (x1, y1) = (10,4)
Example 2 (x0, y0) = (0,8) (x1, y1) = (9,0)
Example 3 (x0, y0) = (0,1) (x1, y1) = (2,7)
CS-321Dr. Mark L. Hornick
7
Example 1 Results?
0
1
2
3
4
5
6
7
8
0 1 2 3 4 5 6 7 8 9 10 11
m=0.3
CS-321Dr. Mark L. Hornick
8
Example 2 Results?
0
1
2
3
4
5
6
7
8
0 1 2 3 4 5 6 7 8 9 10 11
m=-0.89
CS-321Dr. Mark L. Hornick
9
Example 3 Results?
0
1
2
3
4
5
6
7
8
0 1 2 3 4 5 6 7 8 9 10 11
m=3
CS-321Dr. Mark L. Hornick
10
Simple Algorithm Adjustment for m
If |m|<1
If |m|>1
If |m|=1x = y
0 0y m x x y
0 0(1/ )x m y y x
11
Simple Algorithm Summary
Evaluate y = f(x) at each x position Requires floating multiplication for each
evaluation of y
CS-321Dr. Mark L. Hornick
12
Digital Differential Analyzer(DDA) Algorithm
Step through either x or y based on slope Build the line parametrically
If |m|<1 xk+1 = xk + 1 yk+1 = yk + m
If |m|>1 xk+1 = xk + 1/m yk+1 = yk + 1
If |m|=1 xk+1 = xk + 1 yk+1 = yk + 1
CS-321Dr. Mark L. Hornick
13
DDA Implementationvoid dda( int x0, int xn, int y0, int yn ){
float dx = float(xn - x0); // total span in xfloat dy = float(yn - y0); // total span in yfloat y = float(y0); float x = float(x0);
float Dx, Dy; // incremental steps in x & y// determine if slope m GT or LT 1if( dx > dy ){ Dx = 1; Dy = dy/dx;// floating division, but only done once per line}else{ Dx = dx/dy; Dy = 1;}int ix, iy; // pixel coordsfor( int k=0; k<=((dx>dy)? dx:dy); k++ ){
ix = int(x + 0.5); // round to nearest pixel coordinateiy = int(y + 0.5);x += Dx; // floating point calculationsy += Dy;
}}
CS-321Dr. Mark L. Hornick
14
DDA Results?
0
1
2
3
4
5
6
7
8
0 1 2 3 4 5 6 7 8 9 10 11
CS-321Dr. Mark L. Hornick
15
DDA Algorithm Highlights
Reduction in strength Replaces multiplication with addition
Still some limitations Round-off error accumulates Floating point addition (e.g. not on PPC)
16
Line-Drawing Algorithms
Simple algorithm Evaluate y = f(x) at each x position Floating multiplication
DDA algorithm Floating addition only
Can we do better? Tomorrow: Bresenham’s algorithm!