Draw a Circle Without Floating Point Arithmetic C

next → ← prev

Bresenham's Line Algorithm

This algorithm is used for browse converting a line. It was developed by Bresenham. It is an efficient method considering information technology involves simply integer addition, subtractions, and multiplication operations. These operations can be performed very quickly so lines tin be generated quickly.

In this method, next pixel selected is that i who has the least altitude from true line.

The method works every bit follows:

Assume a pixel Pi'(x1',y1'),then select subsequent pixels as we work our may to the dark, one pixel position at a time in the horizontal direction toward P2'(x2',y2').

Once a pixel in choose at whatever step

The next pixel is

  1. Either the one to its correct (lower-spring for the line)
  2. One acme its correct and upwardly (upper-bound for the line)

The line is best approximated past those pixels that fall the least distance from the path between Pane',Pii'.

Bresenham's Line Algorithm

To chooses the adjacent one between the bottom pixel S and height pixel T.
If S is chosen
Nosotros have xi+i=10i+1       and       yi+one=yi
If T is chosen
Nosotros have xi+1=teni+1       and       yi+1=yi+i

The bodily y coordinates of the line at x = xi+1is
y=mxi+1+b

Bresenham's Line Algorithm

The altitude from S to the bodily line in y direction
s = y-yi

The distance from T to the actual line in y management
t = (yi+i)-y

At present consider the difference betwixt these 2 distance values
s - t

When (south-t) <0 ⟹ s < t

The closest pixel is S

When (southward-t) ≥0 ⟹ s < t

The closest pixel is T

This deviation is
southward-t = (y-yi)-[(yi+i)-y]
= 2y - 2yi -1

Bresenham's Line Algorithm

Substituting yard by Bresenham's Line Algorithm and introducing decision variable
di=△x (s-t)
di=△10 (2 Bresenham's Line Algorithm (xi+i)+2b-2yi-1)
=2△xyi-2△y-1△x.2b-2yi△x-△x
di=2△y.teni-2△x.yi+c

Where c= two△y+△x (2b-1)

We can write the decision variable di+1 for the side by side slip on
di+i=2△y.xi+1-ii△10.yi+1+c
di+i-di=two△y.(teni+1-10i)- 2△x(yi+1-yi)

Since x_(i+1)=teni+1,we accept
di+1+di=2△y.(xi+1-xi)- 2△ten(yi+ane-yi)

Special Cases

If called pixel is at the top pixel T (i.e., di≥0)⟹ yi+i=yi+1
di+ane=di+2△y-2△x

If chosen pixel is at the bottom pixel T (i.due east., di<0)⟹ yi+1=yi
di+1=di+2△y

Finally, we calculate d1
d1=△x[2m(x1+one)+2b-2yane-1]
done=△x[2(mx1+b-yi)+2m-1]

Since mxone+b-yi=0 and m = Bresenham's Line Algorithm, we accept
d1=ii△y-△10

Advantage:

i. It involves only integer arithmetic, then information technology is simple.

ii. It avoids the generation of indistinguishable points.

3. Information technology tin be implemented using hardware because information technology does not use multiplication and partition.

4. It is faster as compared to DDA (Digital Differential Analyzer) because it does not involve floating point calculations like DDA Algorithm.

Disadvantage:

1. This algorithm is meant for basic line drawing only Initializing is not a function of Bresenham'south line algorithm. So to describe smooth lines, you lot should want to look into a different algorithm.

Bresenham'due south Line Algorithm:

Step1: Commencement Algorithm

Step2: Declare variable x1,x2,yane,yii,d,i1,itwo,dx,dy

Step3: Enter value of x1,yone,x2,y2
Where xi,y1are coordinates of starting point
And x2,y2 are coordinates of Ending betoken

Step4: Calculate dx = x2-xane
Calculate dy = y2-yone
Calculate i1=2*dy
Summate i2=ii*(dy-dx)
Calculate d=ione-dx

Step5: Consider (ten, y) every bit starting point and xendas maximum possible value of x.
If dx < 0
Then x = xii
y = y2
xfinish=ten1
If dx > 0
And then x = x1
y = yone
xterminate=102

Step6: Generate point at (ten,y)coordinates.

Step7: Check if whole line is generated.
If x > = xend
Stop.

Step8: Calculate co-ordinates of the next pixel
If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increase y = y + 1

Step9: Increment x = x + 1

Step10: Describe a point of latest (x, y) coordinates

Step11: Get to step 7

Step12: Cease of Algorithm

Example: Starting and Catastrophe position of the line are (i, 1) and (8, 5). Find intermediate points.

Solution: x1=1
y1=ane
xtwo=8
y2=5
dx= x2-x1=8-1=7
dy=y2-y1=5-1=4
Iane=2* ∆y=two*four=8
I2=2*(∆y-∆ten)=2*(four-7)=-6
d = I1-∆x=eight-vii=i

x y d=d+Ii or I2
1 1 d+I2=1+(-half dozen)=-5
2 ii d+I1=-v+8=iii
3 2 d+I2=3+(-6)=-three
4 iii d+Iane=-3+eight=5
5 3 d+I2=5+(-vi)=-i
6 4 d+I1=-i+viii=7
7 4 d+Itwo=7+(-six)=1
8 five
Bresenham's Line Algorithm

Programme to implement Bresenham'due south Line Cartoon Algorithm:

Output:

Bresenham's Line Algorithm

Differentiate betwixt DDA Algorithm and Bresenham's Line Algorithm:

DDA Algorithm Bresenham's Line Algorithm
1. DDA Algorithm use floating point, i.e., Real Arithmetic. one. Bresenham's Line Algorithm employ stock-still point, i.east., Integer Arithmetic
2. DDA Algorithms uses multiplication & division its operation 2.Bresenham'southward Line Algorithm uses simply subtraction and add-on its functioning
3. DDA Algorithm is slowly than Bresenham's Line Algorithm in line drawing because it uses real arithmetic (Floating Betoken operation) 3. Bresenham's Algorithm is faster than DDA Algorithm in line because information technology involves simply addition & subtraction in its adding and uses but integer arithmetic.
4. DDA Algorithm is non accurate and efficient as Bresenham's Line Algorithm. 4. Bresenham's Line Algorithm is more accurate and efficient at DDA Algorithm.
5.DDA Algorithm tin can draw circle and curves but are non accurate as Bresenham's Line Algorithm 5. Bresenham's Line Algorithm tin can draw circle and curves with more accurate than DDA Algorithm.

Adjacent Topic Defining a Circle

← prev next →

lambertyoullot.blogspot.com

Source: https://www.javatpoint.com/computer-graphics-bresenhams-line-algorithm

0 Response to "Draw a Circle Without Floating Point Arithmetic C"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel