Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know why you're using an xstep variable. You don't really need one with the algorithm you're using.</p> <blockquote> <p>@Gabe: xstep is needed because without it, if x0 > x1, then the for loop will terminate immediately, as the default step for a Python for loop is 1.</p> </blockquote> <p>The reason you don't need an xstep variable is because, if it's going backwards, the coordinates were already switched (in the <code>if backward:</code> conditional at the beginning) so that the end-point is now the start-point and vice-versa, such that we now are <em>still</em> going left-to-right.</p> <p>You just need this:</p> <pre><code>result = [] for x in range(x0, x1): if steep: result.append((y, x)) else: result.append((x, y)) error -= dy if error &lt; 0: y += ystep error += dx return result </code></pre> <p>If you want the list of coordinates in order from start to end-point, then you can do the check at the end:</p> <pre><code>if backward: return result.reverse() else: return result </code></pre> <p><strong>EDIT</strong>: The problem is that the <code>backward</code> boolean is being evaluated <em>before</em> it needs to be. If the <code>steep</code> conditional executes, then the values change, but by then your <code>backward</code> conditional is different. To fix this, instead of using a <code>backward</code> boolean, make it an explicit expression:</p> <pre><code>if x0 &gt; x1: # swapping here </code></pre> <p>Then again, since you end up using the boolean later on, you could just define it before the conditional:</p> <pre><code>backward = x0 &gt; x1 if backward: </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload