Question

In: Computer Science

Compute the pixel co-ordinates for lines 1 and 2 given below using i) Bresenham algorithm Line...

Compute the pixel co-ordinates for lines 1 and 2 given below using i) Bresenham algorithm Line 2: (xp = 0, yp = 8) to (xq = 5, yq = 1) Show all the steps involved and mark the computed pixels on the blank pixel grid (attached) for each case.

Solutions

Expert Solution

Bresenham’s Line Generation Algorithm:-

Given coordinate of two points A(x1, y1) and B(x2, y2). The task to find all the intermediate points required for drawing line AB on the computer screen of pixels. Note that every pixel has integer coordinates.

Examples:

 

Below are some assumptions to keep algorithm simple.

  1. We draw line from left to right.
  2. x1 < x2 and y1< y2
  3. Slope of the line is between 0 and 1. We draw a line from lower left to upper right.

Let us understand the process by considering the naive way first.

 

Above algorithm works, but it is slow. The idea of Bresenham’s algorithm is to avoid floating point multiplication and addition to compute mx + c, and then computing round value of (mx + c) in every step. In Bresenham’s algorithm, we move across the x-axis in unit intervals.

  1. We always increase x by 1, and we choose about next y, whether we need to go to y+1 or remain on y. In other w
  2. words, from any position (Xk, Yk) we need to choose between (Xk + 1, Yk) and (Xk + 1, Yk + 1).
  3. We would like to pick the y value (among Yk + 1 and Yk) corresponding to a point that is closer to the original line.
  4. We need to a decision parameter to decide whether to pick Yk + 1 or Yk as next point. The idea is to keep track of slope error from previous increment to y. If the slope error becomes greater than 0.5, we know that the line has moved upwards one pixel, and that we must increment our y coordinate and readjust the error to represent the distance from the top of the new pixel – which is done by subtracting one from error.

     

    How to avoid floating point arithmetic
    The above algorithm still includes floating point arithmetic. To avoid floating point arithmetic, consider the value below value m.

    m = (y2 – y1)/(x2 – x1)

    We multiply both sides by (x2 – x1)

    We also change slope_error to slope_error * (x2 – x1). To avoid comparison with 0.5, we further change it to slope_error * (x2 – x1) * 2.

    Also, it is generally preferred to compare with 0 than 1.

     

    The initial value of slope_error_new is 2*(y2 – y1) – (x2 – x1). Refer this for proof of this value

    Below is the implementation of above algorithms.

  5. Here is the implementation of Bresenham’s Line Generation Algorithm using python:-

  6. # Python 3 program for Bresenham’s Line Generation
    # Assumptions :
    # 1) Line is drawn from left to right.
    # 2) x1 < x2 and y1 < y2
    # 3) Slope of the line is between 0 and 1.
    # We draw a line from lower left to upper
    # right.


    # function for line generation
    def bresenham(x1,y1,x2, y2):

    m_new = 2 * (y2 - y1)
    slope_error_new = m_new - (x2 - x1)

    y=y1
    for x in range(x1,x2+1):

    print("(",x ,",",y ,")\n")

    # Add slope to increment angle formed
    slope_error_new =slope_error_new + m_new

    # Slope error reached limit, time to
    # increment y and update slope error.
    if (slope_error_new >= 0):
    y=y+1
    slope_error_new =slope_error_new - 2 * (x2 - x1)


    # driver function
    if __name__=='__main__':
    x1 = 0
    y1 = 8
    x2 = 5
    y2 = 1

  7. output:-

  8. ( 0 , 8 )

    ( 1 , 8 )

    ( 2 , 8 )

    ( 3 , 8 )

    ( 4 , 8 )

    ( 5 , 8 )


Related Solutions

Using Green’s theorem, compute the line integral of the vector field below, along the curve x^2...
Using Green’s theorem, compute the line integral of the vector field below, along the curve x^2 - 2x + y^2 = 0 , with the counterclockwise orientation. Don’t compute the FINAL TRIG integral. F(x,y) = < (-y^3 / 3) - cos(x^7) , cos(y^9 + y^5) + (x^3 / 3) > .
Using Green’s theorem, compute the line integral of the vector field below, along the curve x^2-2x+...
Using Green’s theorem, compute the line integral of the vector field below, along the curve x^2-2x+ y^2=0 , with the counterclockwise orientation. Don’t compute the FINAL TRIG integral. F(x,y)=<- y^3/3-cos⁡(x^7 ) ,cos(y^9+y^5 )+ x^3/3> .
The ordinates of a 6- hr unit hydrograph are given below. A storm had three successive...
The ordinates of a 6- hr unit hydrograph are given below. A storm had three successive 6 – hr intervals of rainfall magnitude of 3, 5, and 4 cm respectively. Assuming a Φ- index of 0.2 cm/hr and a base flow of 30 m3/s, determine the resulting hydrograph of flow (total flow). time, hr Ordinate of 6-h UH 0 0 6 250 12 600 18 800 24 700 30 600 36 450 42 320 48 200 54 100 60 50...
C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2....
C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2. Print 5 asterisk on 1 line. Endl at the end of the line. 3. Ask for a positive # and print that many asterik on the next line. 4. Using 2 for loops one inside of another that only prints 1 asterik, print a hill shaped triangle: Ex( Input a number a: 4 * ** *** **** 5. Change the for statements to print...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
Using Python, I am trying to integrate 'iloc' into the line of code below? This line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line is replacing all the '1' values across my .csv file and is throwing off my data aggregation. How would I implement 'iloc' so that this line of code only changes the '1's integers in my 'RIC' column? patient_data_df= patient_data_df.replace(to_replace=[1], value ="Stroke") Thank you:)
Using the method of characteristics, compute and graph to scale, the contour (using solid lines) and...
Using the method of characteristics, compute and graph to scale, the contour (using solid lines) and the characteristics (using dashed lines) of a two-dimensional minimum-length nozzle for the expansion of air to a design exit Mach number of 3. Assume the specific heat ratio is 1.4. For clarity, lay out the design using a schematic, number the intersection points for the characteristics, develop the calculations using a spread sheet and show the spread sheet results for each point for K-,...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Give a recursive algorithm to compute a list of all permutations of a given set S....
Give a recursive algorithm to compute a list of all permutations of a given set S. (That is, compute a list of all possible orderings of the elements of S. For example, permutations({1, 2, 3}) should return {〈1, 2, 3〉, 〈1, 3, 2〉, 〈2, 1, 3〉, 〈2, 3, 1〉, 〈3, 1, 2〉, 〈3, 2, 1〉}, in some order.) Prove your algorithm correct by induction.
Calculate the integrals given below using partial fractions: a) [int] 1/(x^2 - a^2) dx a is...
Calculate the integrals given below using partial fractions: a) [int] 1/(x^2 - a^2) dx a is a fixed positive real number b) [int] 1/(x^4 - 1) dx c) [int] 1/(x^2 + x) dx d) [int] 1/(x^2 - 4x - 5) dx e) [int] 1/(x^2 - 3x - 5) dx f) [int] exp(x) / (exp(4x) - 5exp(2x) + 4) dx
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT