Created
April 25, 2010 15:50
-
-
Save VoQn/378495 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
FizzBuzz Program Visualization | |
Author by VoQn mail: voqn.tyrantist(AT)gmail.com | |
Inspirated by "Don't Overthink FizzBuzz" | |
http://weblog.raganwald.com/2007/01/dont-overthink-fizzbuzz.html | |
>>> fizzbuzz(1) | |
1 | |
>>> fizzbuzz(3) | |
Fizz | |
>>> fizzbuzz(5) | |
Buzz | |
>>> fizzbuzz(15) | |
FizzBuzz | |
This program can running "NodeBox" http://nodebox.net/code/index.php/Home | |
Generated graphics is this -> http://twitpic.com/1iembg | |
""" | |
class FizzBuzzCircle: | |
def __init__(self, base): | |
self.base = base | |
self.num = 1 | |
def setNumber(self, num): | |
self.num = num | |
def fizzbuzz(self, i): | |
r = 'radius' | |
s = 'stroke' | |
if i % 15 == 0: | |
return { r:15, s:0.6 } | |
if i % 5 == 0: | |
return { r:5, s: 0.7 } | |
if i % 3 == 0: | |
return { r:3, s: 0.8 } | |
return { r: 1, s: 0.9 } | |
def justified(self, l, r): | |
return l - r / 2 | |
def draw(self, x0, y0): | |
fzbz = self.fizzbuzz(self.num) | |
radius = self.base * fzbz['radius'] | |
j = lambda l: self.justified(l, radius) | |
stroke(fzbz['stroke']) | |
oval(j(x0), j(y0), radius, radius) | |
class FizzBuzzDrawer: | |
def __init__(self, x, y, w, h, r): | |
self.r = r | |
self.x = x | |
self.y = y | |
self.w = w | |
self.h = h | |
def draw(self): | |
tx = self.x | |
ty = self.y | |
i = 0 | |
c = FizzBuzzCircle(self.r) | |
while ty < (self.y + self.h): | |
i += 1 | |
c.setNumber(i) | |
c.draw(tx, ty) | |
tx += self.r | |
if tx > (self.x + self.w): | |
tx = self.x | |
ty += self.r | |
size(400, 600) | |
var("X", NUMBER, 0, 0, WIDTH) | |
var("Y", NUMBER, 0, 0, HEIGHT) | |
var("Width", NUMBER, WIDTH, 0, WIDTH) | |
var("Height", NUMBER, HEIGHT, 0, HEIGHT) | |
var("Radius", NUMBER, 10, 5, 40) | |
drawer = FizzBuzzDrawer(int(X), | |
int(Y), | |
int(Width), | |
int(Height), | |
float(Radius)) | |
nofill() | |
drawer.draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment