Sunday, September 23, 2012

It Doesn't Take A Genius

Let's talk about smarts.  I recently upgraded an open source 3D addon in Python that generates step pyramids.  It involved linear algebra and a functional programming idea.  Some time ago, I would have looked at that combination and have had a formula like this go through my head...

 Open Source + 3D  Math + Python + Functional Programming = Smart Hacker

To be honest with you, I never fit the "smart hacker" profile before and I don't think I fit it now.  The good news is you don't NEED to be in order to pull off stuff like that.  Here's how I managed it.

I Played
My script required working with quaternions and vectors.  These are linear algebra concepts that are way beyond what I took in college.  Rather than diving into the math, I decided to pull pieces out of other people's open source code and play with parameters.  In the end, all I learned was that vectors and quaternions can be used to make points that circle around an axis.  Conceptually, it's no weirder than spokes coming out from the center of a bicycle wheel.

I Used List Comprehensions
List comprehensions are a neat little spin on the functional programming ideas of maps and filters.  Put simply, it's nice syntactic sugar for turning one list into another list.  I like them because they allow me to easily make these baby loops to work my way towards a final result.  Here’s what it looks like to go from a set of angles into the corner points of a pyramid step.

What I like about this approach is how it lets me easily see the flow of the entire list from one form to another to another.  An imperative for-loop flow would have encouraged me to make bigger loops that did more.  I probably would have also been more inclined to see things on a point by point basis instead of seeing the state of the list as a whole.  This isn't to say "normal" for loops don't have their place.  I just think that list comprehensions are less brain hurt than the alternative would have been in this function.

It Doesn’t Take a Wizard/Rockstar/Whatever
So there you have it. I made a 3D art tool with the help of a simple functional programming trick and copied code.  It didn’t come from being a genius.  It came from poking around, playing, and learning from people who are smarter than I am.  It’s the basis of most of what I’ve done and it’s an all around fun way to learn and build stuff.  I'd encourage anyone to take this approach.  Never back down from a problem just because it looks too advanced.  Do it anyways.

No comments:

Post a Comment