D
D
Dmitry Demidov2014-01-13 12:39:35
Python
Dmitry Demidov, 2014-01-13 12:39:35

What is the problem with using Bresenham's algorithm?

Hello!
There is a task: pixel by pixel to connect the points given by coordinates.
To solve this, I use the above algorithm like this:

def print_bres(a, b):
  x1, x2, y1, y2 = a[0], b[0], a[1], b[1]
  delta_x = abs(x2 - x1)
  delta_y = abs(y2 - y1)

  sign_x = 1 if x1 < x2 else -1
  sign_y = 1 if y1 < y2 else -1	

  error = delta_x - delta_y	

  print ("%d %d" %(x2, y2))

  while (x1 != x2) or (y1 != y2):
    print ("%d %d" %(x1, y1))
    error2 = error*2
    if error2 > -delta_y:
      error -= delta_y
      x1 += sign_x
    
    if error2 < delta_x:
      error += delta_x
      y1 += sign_y

Then I apply this function to the points given by the list. Like this:
counter = 0
while counter != len(v) - 1:
  for i in range(counter, len(v)-1):
    print_bres(v[counter], v[i])
  counter += 1

I put the results into a text file, which I load into Octave to see the picture.
In the picture, it turns out that each line is drawn twice, and in different ways ... I
broke my whole head, what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2014-01-13
@ptitca_zu

And in Octave separate points or lines between points are drawn? Your implementation of the algorithm, for the segment (0, 0) - (2,3) outputs:

2 3
0 0
1 1
1 2

That is, if drawing segments is set, then first the segment (2, 3) - (0, 0), then (0, 0) - (1, 1), etc.
For normal output, move the line to the end of the program
print ("%d %d" %(x2, y2))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question