Paths in python
<p>Consider a rectangular grid.</p>

<p>I want a short and elegant way of generating a straight path from <code>[x0,y0]</code> to <code>[x1,y1]</code>, where either <code>x0=x1</code> or <code>y0 = y1</code>.</p>

<p>For example, on input <code>[1,3], [3,3]</code> the output <code>[[1,3],[2,3],[3,3]</code> should be generated. Likewise if the input is <code>[3,3], [1,3]</code>.</p>

<p>I have tried <code>[[i,j] for i in range(self.origin[0],self.end[0]+1) for j in range(self.origin[1], self.end[1]+1)]</code>, but it only works for the case where the input is ordered.</p>

<p>Add the <strong>step</strong> argument to your range, deriving the sign of the start &amp; end difference:</p>

<pre><code>x_dir = copysign(1, self.end[0] - self.origin[0])
... for i in range(self.origin[0], self.end[0]+1, x_dir) ...
</code></pre>

<p>Do likewise for the y direction.</p>

python python-3.x