Posts Tagged ‘ book

B-day & xmas loot

Scored quite a few cool books this year for the birthday\Christmas season:

Books include:

In addition to the books, I got a Microrax starter set, which I built the above book-holder with.

Time to start reading!

openFrameworks + Code::Blocks Install

A while back I picked up the book Programming Interactivity, since it deals with Processing, Arduino, and openFrameworks.  I know a bit of Processing and Arduino, I mainly got it as a primer to help me wrap my head around c++ through openFrameworks.  As I quickly learned though, as a c++ \ openFrameworks noob, there were some serious holes in getting the install working.  I’m sure people that are more versed in the languages take this stuff for granted, but it took me a bit of searching.  I was also surprised that I could find no all-inclusive install tutorial for using openFrameworks with Code::Blocks, the IDE the book (and apparently openFrameworks) recommend using.  Took me a while to get it working, so I will log my steps here for future generations to follow.  I should point out this is being done on a WinXP system.

Install Code::Blocks

openFrameworks does have a install process for Code::Blocks here.  I followed that exactly after downloading the Code::Blocks binary release.

Download openFrameworks

I downloaded the ‘code blocks FAT’ file and unzipped it.  One of the key pieces that was missing was where to pit this data.  I had tried a variety of subdirs on my computer, but couldn’t ever get any of the openFrameworks projects to compile.  Finally after a lot of web searching, I learned I needed to put it in:

c:\openframeworks

So that’s exactly what I did, and things started working better.

Run Some Examples

In Code::Blocks, I opened the ‘workspace’:
C:\openframeworks\apps\examples\textureExample\textureExample.workspace
(or any example workspace for that matter) and promptly bonked the ‘Build and Run’ button.  But an error occurred:

error: 'exit' is not a member of 'std'|

After doing a bit of web searching, I ran across this post, which gives directions on how to modify the ‘ofConstants.h’ file (located C:\openframeworks\libs\openFrameworks\utils) to correct the issue.

After that, ‘Build and Run’ was appeased, and my openFrameworks career has officially started.

Two new books added to the library…

Got to make use of some 40% off coupons at Barnes & Nobel today:

  • Algorithms in a Nutshell : I’d been eying this book for a while now, finally took the plunge.  I’m particularly interested in their chapters about convex hull generation, and path-finding AI.  The problem-solving techniques in general I find quite fascinating.
  • Visualizing Data : By Ben Fry, one of the co-creators of Processing.  I find this interesting not only in the code-examples behind displaying data to the screen, but the processes behind finding and parsing the data in the first place.  Also serves as a good bridge between Processing and Java, with examples of running Processing in Eclipse (see my post here).

The Chaos Game

After getting the book “INTRODUCING Fractal Geometry“, I thought it’d make a good learning project to tackle “The Chaos Game“, as coined by the mathematician Michael Barnsley. Seemed simple enough to grasp, and would be fun to code up in Python\PyGame.

What attracted me to it was that while it was a seemingly simple problem, a few things outside my realm of knowledge were needed: Find a random point inside of a triangle. After a bit of web searching, I ran across the post on Wolfram Mathworld describing ‘Triangle Point Picking‘. It shows how to find points based on a quadrilateral (2x triangle). Using that code, and a Python specific post called “Deciding if a Point is Inside a Polygon“, I was able to combine them with the game rules to achieve success, the Sierpinski triangle!

While the end result is nothing special to look at, Sierpinski triangles have been around for a long time, it was the fun of actually making the whole thing work through Python that kept me at it on Sunday afternoon 😉

  • Python source code HERE.
    • Its expecting to use this Vec2D class.
  • .zip with Windows executable HERE.

Now has its own page on the blog.

Physics in PyGame and Python

I’m on week two of my seven week sabbatical (yay), and part of what I’ve been up to is learning more about PyGame (see previous posts).  I have nothing major to show yet:  I started a ‘PyGame’ wiki (not yet online) and have a “tank” game where you can drive them around, bump into each other and obstacles, and I just got the ability to add independently transforming “turrets” to them (my son has had fun making the sprite graphics for them in Gimp).  My goal is to have them all powered via pseudo-accurate 2d (top down) physics.  I started developing this based on web-examples… but I quickly figured out a couple things:

  • I don’t know anything about physics, really.
  • Online examples only take you so far.

To help remedy this, and since I’m a sucker for paper in my hand, and have purchased these informative volumes:

Before I start in earnest though, it’s always good to see what other people have done, and I tracked down these two 2d physics engines for Python\PyGame:

Since I like the name “munk” more than “box”, I’ve recently installed PyMunk, and the examples were up and running in no time.  I look forward to strapping this into my PyGame projects.

Concurrent with that, I’ve just started reading the first two of my physics books, and to help me learn the actual nature of physics, I’ve been authoring some Python classes along with them.  Below are a few examples of what I’ve been up to.  And for me, it’s been both a good learning experience with simple physics, and for making Python classes themselves.  Especially the operator overloading stuff.

In the below example, I make several objects to represent different “units” of measurement:

  • Distance
  • Mass (omitted from below example)
  • Weight  (omitted from below example)
  • Time
  • more to come… (volume, etc…)

All of these objects have a superclass called ‘MeasureBase’, which defines their behaviors (mainly the operator overloading stuff).  Each class stores its data, no matter what type it is, in an attribute called ‘units’.  This allows me to multiply distance by time, for example.  Each class stores its units as a certain kind of units:  Distance stores everything as meters, no matter if you pass in inches, centimeters, miles, or kilometers.  Time stores everything as seconds, even if you pass in hours or days.

Finally, I have a class that actually does something interesting (hopefully the first of many) with all these units, called ‘DistanceFallen’: You pass in how long something has fallen, and it will return back how far it has fallen.  See code below (WordPress seems to be messing up the indentation… :-( )

I’ll conclude with:  This is just a start, for fun, on a Saturday afternoon.  But I think it’ll be a good learning tool moving forward, both for programming PyGame, and for physics concepts in general.

# units.py
import math

# gravitational constant: 9.8m/sec2
GRAVITY = 9.8

class MeasureBase(object):
    # Superclass inherited by measurement types.  Defines some default behavior.
    # self.units & .unitType is defined by subclasses

    inchToMeter = 39.3700787
    footToMeter = 3.2808399
    yardToMeter = 0.9144
    mileToMeter = 609.344
    ounceToKilogram = 0.0283495231
    poundToKilogram = 0.45359237    

    def __str__(self):
        return "%s %s" %(self.units, self.unitType)
    def __repr__(self):
        return str(self.units)
    def __float__(self):
        return self.units
    def __int__(self):
        return int(self.units)

    def __add__(self, dist):
        return self.__class__(self.units + float(dist))

    def __sub__(self, dist):
        return self.__class__(self.units - float(dist))

    def __mul__(self, dist):
        return self.__class__(self.units * float(dist))

    def __div__(self, dist):
        return self.__class__(self.units / float(dist))

    def __pow__(self, val):
        return math.pow(self.units, float(val))

    def __abs__(self):
        return self.__class__(abs(self.units))

class Distance(MeasureBase):
    # Internal measurements are stored as meters

    def __init__(self, units=0.0):
        MeasureBase.__init__(self)
        self.unitType = "meters"
        self.units = float(units)

    def apply_mm(self, mm):
        self.units += mm/1000.0

    def apply_cm(self, cm):
        self.units += cm/100.0        

    def apply_dm(self, dm):
        self.units += dm/10.0        

    def apply_m(self, m):
        self.units += m

    def apply_km(self, km):
        self.units += km*1000.0

    def apply_inch(self, inch):
        self.units += inch / MeasureBase.inchToMeter

    def apply_foot(self, foot):
        self.units += foot / MeasureBase.footToMeter

    def apply_yard(self, yard):
        self.units ++ yard / MeasureBase.yardToMeter

    def apply_mile(self, mile):
        self.units += mile / MeasureBase.mileToMeter

class Time(MeasureBase):
    # Internal measurements are stored as seconds

    def __init__(self, units=1.0):
        MeasureBase.__init__(self)
        self.unitType = "seconds"
        self.units = float(units)

    def apply_miliseconds(self, ms):
        self.units += ms*.001

    def apply_seconds(self, s):
        self.units += s

    def apply_minutes(self, m):
        self.units += m*60

    def apply_hours(self, h):
        self.units += h*3600

    def apply_days(self, d):
        self.units += d*86400

    def apply_years(self, y):
        self.units += y*31104000

class DistanceFallen(object):

    def __init__(self, time, grav=GRAVITY):
        # time is either in seconds, or a Time object
        self.time = Time(time)
        self.grav = Distance(grav)
        self.calcDistance()

    def __str__(self):
        return "Fallen %s in %s" %(self.distance, self.time)

    def set_time(self, time):
        # time is either in seconds, or a Time object
        self.time = Time(time)

    def set_gravity(self, grav):
        self.grav= Distance(grav)

    def calcDistance(self):
        self.distance = (self.grav * math.pow(float(self.time), 2)) * .5

    def getDistance(self):    
        self.calcDistance()
        return self.distance

Based on that, here’s a simple example of it in action:

time = Time(4)
print time
fallen = DistanceFallen(time)
print fallen

The result printed is:

4.0 seconds
Fallen 78.4 meters in 4.0 seconds
self,