Sunday, August 21, 2016

Pickover's OR Game

In his book Mazes for the Mind: Computers and the Unexpected, Clifford Pickover explores tons of algorithms that produce surprising results, including this one: take all the points on an x-y grid and convert the coordinates to binary. Taken digit by digit, if there's a 1 in either x OR y, the result will have a 1 in that digit. Color that x-y location according to the base 10 value of that number, mod 255 of course. (Since RGB values typically have a range of 0 - 255).

For example, for the point at (10, 45) you'd convert the x and y coordinates to binary. 10 is 1010 in binary, and 45 is 101101. Comparing the digits and filling in 1's if there's a 1 in that place in either number, you get 101111 or 47. You'd color that pixel 47, on the darker end of the 0-255 grayscale.

I have a binary converter program in my book Hacking Math Class with Python:

def convToBinary(num):
    '''converts decimal number to binary'''
    exponent = 0
    binary_number = 0
    while num >= 2 ** exponent: #Find the lowest power of 2
        exponent += 1           #the number is less than
    exponent -= 1
    for i in range(exponent + 1):
        #if num contains that power of 2
        if num - 2**exponent > -1: 
            #add that power of 10
            binary_number += 10**exponent 
            num -= 2**exponent
        exponent -= 1
    return binary_number

I created an "addZeros" function to make sure each binary number was 8 digits.

def addZeros(x):
    '''converts a binary number to
    8-digit form'''
    x = str(convToBinary(x))
    length = len(x)
    x = (8-length)*'0'+ x
    return x

In the next step I'll want to convert the number back to decimal so I wrote this:

def convToDec(num):
    '''converts num to decimal'''
    decnum = 0
    num = str(num)
    num = num[::-1] #reverse digits
    for i,v in enumerate(num):
        if v == '1':
            decnum += 2 ** i
    return decnum

Now we can make the comparison I described above, and return the decimal number that we'll give to the color argument.

def compareOr(a,b):
    '''converts 2 numbers to binary and
    compares their digits, returns a 1 in
    a place if it's 1 in a OR b'''
    result = 0
    a,b = addZeros(a),addZeros(b)
    for i in range(8):
        if a[-(1+i)] == '1' or b[-(1+i)] == '1':
            result += 10**i
    return convToDec(result)

Finally we'll draw a tiny 2x2 pixel rectangle of the calculated color. I used the bluescale:

def displayOr(x,y):
    '''displays the result of the "Or" comparison'''
    pygame.draw.rect(screen,compareOr(x,y) %254,[x,y,2,2])

I first did this in Pygame. All the code so far will run in the Python mode of Processing except the very last line. In Processing it needs to be replaced by these two lines.

    fill(0,0,compareOr(x,y) %254)
    rect(x,y,2,2)

Run the displayOr function using this nested loop for every other pixel on the screen:

for x in range(0,WIDTH,2):
    for y in range(0,HEIGHT,2):
        displayOr(x,y)

Guess what it makes?


If you guessed the Sierpinski Triangle you're right about a ton of seemingly unrelated afternoons spent playing around with math, Python, Pygame, Processing and Pickover. Pete out!

Saturday, August 20, 2016

The A is in the mAth

In my position as co-founder of the Professional Development company Make It STEM, I've been asked, "What about the A for Art?" I agree "Make it STEAM" would be a kickass company name, but as a math fanatic I feel the A is in the math.

Before you groan, let me explain.

Think about Art, and what it means to you. I'm sure you're conjuring up images of liberation, creativity and fun. Everybody does art in their own way, and that's cool. There are no rules, only tools: paint, crayons, yarn, beads, clay, marble, metal, lights, LEGO, anything goes!

That's exactly the feeling I get when I think of Math. It's all about beautiful curves and symmetry and mysterious symbols that can do magic if you know how to use them. Add to that the idea that my "Art" can be used to help understand science and lots of other fields, and it just adds to its ridiculous usefulness. Yes, some people like to promote rigid, "hard" math, but it's OK, anything goes. I realize not everybody shares my opinion.

Picture somebody saying to you, "I hate Art." You'd think they were crazy! You might ask, "What do you mean, you hate Art?" They might tell you, "Ugh, it's all about brushes, and buying brushes, and it has to be the right brushes, and you gotta wash the brushes.... I flunked a painting class because of the damned brushes. Then I took a drawing class and it was all about having this pencil or that stump and where's your sketchbook? I couldn't draw a horse that looked like a horse so I flunked that class. That meant I couldn't be a forest ranger because I couldn't pass the Art requirement. I hate Art. It makes me feel stupid."

Hopefully you'd think there's something wrong with the way we teach Art!

I think there's something wrong with the way we teach Math, so I promote a very Artsy approach to it. Math teachers want their students to be able to visualize the topics they're presenting, so I train teachers how to use computer programming to graph functions and draw geometric shapes. The Astroid is one of my favorites:

To create this design, you have to know your x-y coordinates, and how to draw lines in whatever graphics package you're using (I love Python but this time I used p5.js). Loops make the job much easier. And once you can make one astroid, you save it to an astroid function and then you can make any number of astroids, anywhere on the screen and make them rotate:



See? It's Art! And it's dynamic and interactive: move the slider on the top left of the applet to change the number of lines in each astroid. That's what happens when you use variables: you can vary things: location, size, color, number of lines...

Get creative with all the classic "math" graphics and you'll learn a lot. It's not easy, but the payoff is immense!