Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0)...

7
Example : (7, 9) (12, 0) mple 2: nt1 V:(7,9 ) C:( 0,255,0) nt2 V:(12,0) C:(0,255,0) (0, 0) (18, 0) (0, 9) What are the problems with this method? Slope>1

description

Optimization (DDA Algorithm) Since we increment y by the same amount, we can also inline rounding: New cost: one floating point addition, one integer addition, one cast. DrawLine (Point point1, Point point2){ m=(point2.y-point1.y) / (point2.x-point2.x); j=point1.y + (-point1.x) * m + 0.5; for i=point1.x to point2.x SetPixel(i, (int)j+=m)); }

Transcript of Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0)...

Page 1: Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0)…

Example:

(7,9)

(12,0)

Example 2:

Point1 V:(7,9 ) C:( 0,255,0)

Point2 V:(12,0) C:(0,255,0)

(0,0) (18,0)

(0,9)

What are the problems with this method? Slope>1

Page 2: Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0)…

Revised Slope InterceptDrawLine (Point point1, Point point2){m=(point2.y-point1.y) / (point2.x-point2.x);b=point1.y + (-point1.x) * m;if (m>1){

for i = point1.x to < point2.x {SetPixel(i , round(i*m+b));

}}else{

for i = point1.y to point2.y {SetPixel(round(i-b/m), i);

}}

Which one should we use if m=1? What is the cost per pixel?

Page 3: Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0)…

Optimization (DDA Algorithm)

• Since we increment y by the same amount, we can also inline rounding:

• New cost: one floating point addition, one integer addition, one cast.

DrawLine (Point point1, Point point2){

m=(point2.y-point1.y) / (point2.x-point2.x);

j=point1.y + (-point1.x) * m + 0.5;

for i=point1.x to point2.x

SetPixel(i , (int)j+=m));

}

Page 4: Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0)…

Optimizations

• In general processing unit:– Addition and Subtraction are faster than Multiplication

which is faster than Division– Integer calculations are faster than Floating and double

calculations – Logical operation is lighter the math operations– Bitwise operation is the lightest

Page 5: Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0)…

Bresenham Line Drawing Algorithm

BdyydxxdyyxF

Bxdxdyy

Bmxy

),(

Let us consider a line equation y = mx + b could be writtenas F (x, y) =ax + by + c

F(x,y) is zero on the line, positive above the, and negative below the line

M

E

NE

)5.0,1( pp yxFd

if ( d > 0) then we choose NEIf ( d < 0 ) then we choose EIf ( d == 0) we choose any of them

Page 6: Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0)…

Bresenham Line Drawing Algorithm

dyaadd

cybxad

cybxa

yxFd

E

oldnew

ppold

pp

ppnew

)5.0()1(

)5.0()2(

)5.0,2(

What happens to next grid point after xp+2, that depends on whether we select E or NE.If E was selected then

M

E

NE

dxdybabadd

cybxa

yxFd

NE

oldnew

bp

ppnew

)5.1()2(

)5.1,2(If NE was selected then

Since the first point (x0,y0)We can directly calculate d to choose NE or E

Page 7: Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0)…

public void DrawLine(Point point1, Point point2){

int dy = point2.y - point1.y; int dx = point2.x - point1.x; dy <<= 1; // dy is now 2*dy dx <<= 1; // dx is now 2*dx int fraction = dy - (dx >> 1); // same as 2*dy - dx j=point1.y;for (i=point1.x; i<point2.x; i++) {

if (fraction >= 0) {

j++; fraction -= dx; // same as fraction -= 2*dx

}fraction += dy; // same as fraction -= 2*dySetPixel(i, j);

}