Posts Tagged ‘ math

The Matrix

Since I never ‘went to school’ for any kind of programming, I’ve had to pick it up as needed throughout my career.  One of the ‘great mysteries’ has been matrix math.  I’m a visual person, and that includes learning math.  Last night, while reading chapter 9 in my PyGame book ‘Beginning Game Development with Python and PyGame: From Novice to Professional‘, the whole ‘matrix thing’ finally made sense in my brain.  So much in fact, I couldn’t fall asleep I was mulling it over so much.  Waking up, I decided to dive into it, and made some psuedo-code showing how matrix multiplication happens.  But it was still really hard to physically visualize what was going on.  So, working in GIMP, I made a graphical layout of what is going on.  It is a work of beauty :)

Couple things are presumed:  It is a description of multiplying two 4×4 matrices, with their translation along the bottom row, not the right column.  In each of the sixteen blocks, the sub-matrices (‘matrixA’ on the left, ‘matrixB’ on the right) are multiplied left to right, and added top to bottom to get the final result of each block.  Time for a t-shirt!

visualMatrixMult02Click through (twice) for a larger version.
You can find it on flickr here as well.

SohCahToa!

Time to get back to the basics.  I use trig at work, and at home in PyGame\Processing, but I like understanding the fundamentals of how it works.  So I sat down this afternoon and made up a “SohCahToa” PyGame program that really illustrates (to me at least)  what the values behind sine, cosine and tangent mean.  They are after all, ratios of the sides of a triangle.

So the mnemonic device is “Soh-Cah-Toa”, which means:

  • Sine = Opposite / Hypotenuse
  • Cosine = Adjacent / Hypotenuse
  • Tangent = Opposite / Adjacent

I wanted a visual way to see this in action, and that’s what this little program does:

sohCahToa01

Click through (twice) to larger image...

It plots a triangle defined by the opposite, adjacent, and hypotenuse sides.  As time goes by ‘degrees \ radians \ pi’ values increase, and the triangle changes.  The lengths of each side are plotted, and at the bottom, the math behind the sin, cos, and tan are shown in real-time.

The source code is online here, feel free to grab it:
http://www.akeric.com/python/sohCahToa01.py

Also, with a lot of help from this post (and several followup emails from its author), I was able to (finally) turn my PyGame apps into Windows executable.  Find the zipped version here:
http://www.akeric.com/python/sohCahToa01.zip

I built it with Python 2.6.2 and PyGame 1.9.1, but nothing fancy is going on with either, so it should work with older versions.