Line connects two points. Hence to draw any line we need at least two points. Hence, we prefer one point as (X1, Y1) and second point is (X2, Y2). Following are some steps to perform DDA algorithm. Step 1: Get the input (X1, Y1) and (X2, Y2) Step 2: calculate the slope of the line using inputs. M= Y2-Y1/X2-X1 Step 3: according to slope of the line we need to decide the case which gives the next point of line. Following are three cases are used to solve the DDA algorithm. Case 1: m<1 Xn=X1+1 and Yn=Y1+m Case 2: m>1 Xn=X1+1/m and Yn=Y1+1 Case 3: m=1 Xn=X1+1 and Yn=Y1+1 Step 4: the process continues till we get the input point (X2, Y2). Then the line generates with the help of coordinates that we occur. Example: Inputs: (X1, Y1) = (0,0) and (X2, Y2) = (8,4) M= Y2-Y1/X2-X1 M=4-0/8-0 M = 0.5 Here M=0.5 Hence follow case 1 where M<1 then Xn=X1+1 and Yn=Y1+m |
X1 | Y1 | Xn | Yn |
0 | 0 | 0 | 0 |
0+1=1 | 0+0.5=0.5 | 1 | 1 |
1+1=2 | 0.5+0.5=1 | 2 | 1 |
2+1=3 | 1+0.5=1.5 | 3 | 2 |
3+1=4 | 1.5+0.5=2 | 4 | 2 |
4+1=5 | 2+0.5=2.5 | 5 | 3 |
5+1=6 | 2.5+0.5=3 | 6 | 3 |
6+1=7 | 3+0.5=3.5 | 7 | 4 |
7+1=8 | 3.5+0.5=4 | 8 | 4 |
It uses only integer calculations. Following are steps to perform bresenham’s algorithm. Step 1: Get the input (X1, Y1) and (X2, Y2) Step 2: Find decision parameter Pk and P0 Pk=2 ∆Y - ∆X Where ∆X=X2-X1 and ∆Y=Y2-Y1 Step 3: according to the value of Pk we need to decide the case which gives the next point of line. Following are three cases are used to solve the bresenham’s algorithm. Case 1: Pk>0, Pk+1=Pk + 2 ∆Y - 2 ∆X Where Xn=X1+1 and Yn=Y1+1 Case 2: Pk<0, Pk+1=Pk + 2 ∆Y Where Xn=X1+1 and Yn=Y1+1 Step 4: the process continues till we get the input point (X2, Y2). Then the line generates with the help of coordinates that we occur. Example: Step 1: Get the input (X1, Y1) and (X2, Y2) (X1, Y1) = (20,10) and (X2, Y2) = (30,18) Step 2: find decision parameter Pk and P0 Pk=2 ∆Y - ∆X Where ∆X=X2-X1=30-20=10 and ∆Y=Y2-Y1=18-10=8 Hence, Pk=2 ∆Y - ∆X = 2*8-10= 16-10=6
At last, we get the last point (X2, Y2) = (30,18) we stop. |
The algorithm calculates the starting point and by the value of E the value of x and y coordinates will be increases and decreases respt. The equation of circle is r2=X2+Y2 To calculate the value of E, We have formula, 2n-1<=r <=2n where r = radius of circle E=2^-n=1/2^n E is used to calculate the value of circle point which helps to draw a circle as X2=X1+E Y1 for x and Y2=Y1- E X2 for y Algorithm: Step 1: Read the radius r of the circle and calculate value of E Step 2: Start X=0 i.e. initial point and Start Y=r i.e. radius Step 3: X1=0 and Y1=r Step 4: Do { X2=X1+E Y1 Y2=Y1-E X2 //Here X2 represents Xn+1 and X1 represent Xn //Xn is from equation of circle Plot (int X2,int Y2) //here swapping points to calculate next closest pixel X1=X2 Y1=Y2 } While ((Y1-Y0)< E or (X0-X1)>E) //here while condition is to check the current position is starting point or not, if current point is not starting point then repeats. Step 5: stop Summary: In DDA circle drawing algorithm the radius and initial points are taken. Value of E is calculated by given radius. The value of X2 and Y2 are calculated by given formula and pot them. X2=X1+E Y1 Y2=Y1-E X2 The next points are then calculated by swapping points as X1 with X2 and Y1 with Y2. |
Following circle shows the 8 portions from which we will occur the coordinates of circle. Algorithm: Step 1: input of circle radius r, (Xc, Yc) Step 2: initial value X=0 and Y=r Step 3: plot pixel (X+Xc, Y+Yc) Step 4: decision parameter Pk=3-2r Step 5: start of loop If Case 1: If Pk<0 then Pk+1=Pk+4X+6 Where Xn=X+1 and Yn=Y Else Case 2: If Pk>0 then Pk+1=Pk+4(X-Y) +10 Xn=X+1 and Yn=Y-1 Step 6: X=Y stop Example: Given R=10 Hence Y=R If we want to draw circle in center then we have given the center points as (Xc, Yc) = (2,2) Decision parameters are Pk and P0. P0=3-2r Here, P0=3-2*10=3-20=-17 Hence Pk=P0= -17 Following are two cases that will be decided according to Pk’s value. Case 1: If Pk<0 then Pk+1=Pk+4X+6 Where Xn=X+1 and Yn=Y Case 2: If Pk>0 then Pk+1=Pk+4(X-Y) +10 Xn=X+1 and Yn=Y-1
Here X=Y then stop
|