Download presentation
Presentation is loading. Please wait.
Published byIra James Modified over 9 years ago
1
1 Line Drawing Version B: Semi-Formal Methods Derivation ©Anthony Steed 1999-2003
2
2 Overview n Line drawing is hard! n Ideal lines and drawing them n Bresenham’s algorithm Stages of optimisation n Going Faster n Going Faster Still
3
3 Ideal Lines n From the equation of a line n Find a discretisation
4
4 Octants n We will choose one case (1 st octant) Line gradient is > 0 and is < 1 n There are eight variations of the following algorithms 4 iterate over X 4 iterate over Y 1 st octant (x2,y2) (x1,y1)
5
5 In the 1 st Octant n We know that there is more than one pixel on every row (i.e. X increases faster than y) n Also Y increase as X increases n Note that horizontal, vertical and 45 degree lines usually treated as special cases
6
6 Naïve Algorithm dx = x 2 -x 1 dy = y 2 - y 1 for (x=x 1 ;x<=x 2 ;x++) { y = round(y1+(dy/dx)*(x-x 1 )) setPixel(x,y) } n Problems one divide, one round, two adds, one multiply per pixel
7
7 What have we got n Only integer arithmetic n One if, 1/2 adds per pixel One hidden if in the loop n Can we get faster than that Well we can get rid of one loop if we are not afraid of programming assembler and using the jmp statement!
8
8 Step 0 dx = x 2 -x 1 dy = y 2 - y 1 for (x=x 1 ;x<=x 2 ;x++) { y = round(y1+(dy/dx)* (x-x 1 )) setPixel(x,y) } Step 1: Convert to incremental algorithm dx = x 2 -x 1 dy = y 2 - y 1 y = y 1 for (x=x 1 ;x<=x 2 ;x++) { y += dy/dx setPixel(x,round(y)) }
9
9 Step 1Step 2: Replace round dx = x 2 -x 1 dy = y 2 - y 1 y = y 1 for (x=x 1 ;x<=x 2 ;x++) { y += dy/dx setPixel(x,(int)(y+0.5)) } dx = x 2 -x 1 dy = y 2 - y 1 y = y 1 for (x=x 1 ;x<=x 2 ;x++) { y += dy/dx setPixel(x,round(y)) }
10
10 Step 2 Step 3: Split y into an integer and fraction part dx = x 2 -x 1 dy = y 2 - y 1 y = y 1 for (x=x 1 ;x<=x 2 ;x++) { y += dy/dx setPixel(x,(int)(y+0.5)) } dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = 0.0 for (x=x 1 ;x<=x 2 ;x++) { y f += dy/dx if (y f > 0.5) { y i ++ y f -- } setPixel(x,y i ) } Note y f is always in range –0.5 to 0.5
11
11 Step 3Step 4: Shift y f by 0.5 dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = 0.0 for (x=x 1 ;x<=x 2 ;x++) { y f += dy/dx if (y f > 0.5) { y i ++ y f -- } setPixel(x,y i ) } dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = -0.5 for (x=x 1 ;x<=x 2 ;x++) { y f += dy/dx if (y f > 0.0) { y i ++ y f -- } setPixel(x,y i ) }
12
12 Step 4 Step 5: Multiply y f through by 2dx dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = 0.0 for (x=x 1 ;x<=x 2 ;x++) { y f += dy/dx if (y f > 0.0) { y i ++ y f -- } setPixel(x,y i ) } dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = -dx for (x=x 1 ;x<=x 2 ;x++) { y f += 2dy if (y f > 0.0) { y i ++ y f += -2dx } setPixel(x,y i ) } We now have all integer arithmetic
13
13 Step 5Step 6: Re-arrange if dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = -dx for (x=x 1 ;x<=x 2 ;x++) { if (y i > 0.0) { y i ++ y f += 2dy -2dx } else { y f += 2dy } setPixel(x,y i ) } This has one less add statement dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = -dx for (x=x 1 ;x<=x 2 ;x++) { y f += 2dy if (y f > 0.0) { y i ++ y f += -2dx } setPixel(x,y i ) }
14
14 Step 6 Step 7: Make all constants dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = -dx e = 2dy – 2dx f = 2dy for (x=x 1 ;x<=x 2 ;x++) { if (y i > 0.0) { y i ++ y f +=e } else { y f += f } setPixel(x,y i ) } Reduced to one if, 1 or 2 adds dx = x 2 -x 1 dy = y 2 - y 1 y i = y 1 y f = -dx for (x=x 1 ;x<=x 2 ;x++) { if (y i > 0.0) { y i ++ y f += 2dy -2dx } else { y f += 2dy } setPixel(x,y i ) }
15
15 What have we got n Only integer arithmetic n One if, 1/2 adds per pixel One hidden if in the loop n Can we get faster than that Well we can get rid of one loop if we are not afraid of programming assembler and using the jmp statement!
16
16 Faster - Mid-Point Drawing n Note that lines are symmetric n (a,b) to (c,d) should generate the same pixels. Thus the lines are symmetric about the mid-point n Implies... draw outwards from mid-point in both directions n Is almost twice as fast (one extra add gives an extra pixel)
17
17 Faster - Two-Step n Note we considered whether to choose P or Q n What if we choose between the four cases n Almost twice the speed again
18
18 Summary n Bresenham’s algorithm uses only integer arithmetic and 2/3 ops per pixel Ideal for hardware implementation n Can be improved almost four-fold in speed, with added complexity Good for software implementations Pixel scanning is usually not the bottleneck these days so wouldn’t be cast into hardware
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.