Bresenham's line algorithm to generate a rectangle defined by its diagonal end points coordinates
Answers
Explanation:
only because the line is defined to start and end on integer coordinates (though it is entirely reasonable to want to draw a line with non-integer end points).
Bresenham's line algorithm :
Explanation:
- This algorithm is employed for scan changing a line.
- it had been developed by Bresenham. it's Associate in Nursing economical methodology as a result of it involves solely number addition, subtractions, and multiplication operations. These operations will be performed terribly apace thus lines will be generated quickly.
- In this method, the next pixel selected is that the one that has the least distance from the true line.
- The method works as follows:
- Assume a pixel P1'(x1',y1'), then select subsequent pixels as we work our way to the night, one-pixel position at a time in the horizontal direction toward P2'(x2',y2').
- Once a component is chosen at any step
- The next pixel is:
- The one to its right sides (lower-bound for the line)
- On top, it's right and up (upper-bound for the line)
- The line is best approximated by those pixels that fall the smallest amount distance from the path between P1', P2'.
- To chooses the successive one between the bottom pixel S and top pixel T.
- If S is chosen
We have xi+1=xi+1 and yi+1=yi
2. If T is chosen
We have xi+1=xi+1 and yi+1=yi+1
- The actual y coordinates of the line at x = xi+1 is given by
y=mxi+1+b
- Bresenham's Line Algorithm, the distance from S to the actual line in the y-direction.
s = y-yi
- The distance from T to the actual line in the y-direction is given by
t = (yi+1)-y
- Now consider the distinction between these 2 distance values
s - t
- When (s-t) <0 ⟹ s < t
The closest pixel is S
When (s-t) ≥0 ⟹ s < t
The closest pixel is T
- This difference is
s-t = (y-yi)-[(yi+1)-y]
= 2y - 2yi -1
- Bresenham's Line Algorithm
by putting m by Bresenham's Line Algorithm and introducing decision variable
di=△x (s-t)
di=△x (2 Bresenham's Line Algorithm (xi+1)+2b-2yi-1)
=2△xyi-2△y-1△x.2b-2yi△x-△x
di=2△y.xi-2△x.yi+c
Where c= 2△y+△x (2b-1)
- We can write the decision variable di+1 for the next slip on
di+1=2△y.xi+1-2△x.yi+1+c
di+1-di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
- Since x_(i+1)=xi+1,we have
di+1+di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Special Cases:
- If the chosen pixel is at the top pixel T (i.e., di≥0)⟹ yi+1=yi+1
di+1=di+2△y-2△x
- If the chosen pixel is at the bottom pixel T (i.e., di<0)⟹ yi+1=yi
di+1=di+2△y
- Finally, we calculate d1
d1=△x[2m(x1+1)+2b-2y1-1]
d1=△x[2(mx1+b-y1)+2m-1]
- Since mx1+b-yi=0 and m = Bresenham's Line Algorithm, we have
d1=2△y-△x
Refer to the following attachment: