Closed clawsoon closed 3 years ago
And to make it even more useful as an import:
def main(width, height):
if width >= height:
for x, y in gilbert2d(0, 0, width, 0, 0, height):
yield(x, y)
else:
for x, y in gilbert2d(0, 0, 0, height, width, 0):
yield(x, y)
if __name__ == "__main__":
width = int(sys.argv[1])
height = int(sys.argv[2])
for x, y in main(width, height):
print(x, y)
Then I'm able to, for example, write my own script like this to put spaces between the lines, since I just need to give it width and height and it gives me back a nice generator that loops over all the values:
#!/usr/bin/env python3
import sys
import gilbert2d
def main(width, height):
(old_x, old_y) = (0, 0)
for x, y in gilbert2d.main(width, height):
print((x+old_x), (y+old_y))
print(2*x, 2*y)
(old_x, old_y) = (x, y)
if __name__ == "__main__":
width = int(sys.argv[1])
height = int(sys.argv[2])
main(width, height)
Hi @clawsoon, I like the idea of using yield
and making the module readily usable, at the same time I would like the code to stay as simple and clean as possible (for me this is basically a simple way to publish the algorithm itself, close to pseudocode).
Please do post a PR. I am no Python expert so I have a few questions:
main
also use yield from
without the need to "repackage" the result?main
could be renamed to gilbert2d
, and the recursive function to gilbert2d_impl
or similar (not sure how internal functions are usually done in Python)for x, y in gilbert2d(width, height)
?Thanks, Jakub
Hi @jakubcerveny. Those all sound like sensible ideas. I'll see if I can put a pull request together in the next couple of days. As I play around with it I might come up with a few more ideas. :-)
The usual convention for a private function in Python is to prefix the function name with an underscore, so _gilbert2d. It's not really private, of course, since no functions in Python are private. Details here, with some extra stuff about double underscore prefixes that I haven't taken the time to fully understand myself:
https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references
Yep, the last code I posted is just an example. Adding the two coordinates together in the example gives me the in-between step when I double the size and space things out. So if the algorithm gives me (0,0), (0,1), (1,1), I'm converting that to (0,0)[original], (0,1)[inbetween], (0,2)[original], (1,2)[inbetween], (2,2)[original]. That allows me to directly draw the pattern as pixels with spaces between the lines. Not mathematically useful, just visually useful. :-)
Pull request submitted.
Hi! This is a great little algorithm. I made my version a little friendlier by turning it into a generator, so that I could use it in other functions. Not sure if you'd be interested in doing that. The diff looks like this for the 2d script; same basic idea for the 3d script. I could put in a formal pull request if you're interested in the idea and prefer that way of working.