Monday, September 22, 2008

A Source For Cleanly Implemented Code

From what I can tell, Flex's API doesn't include a Polygon class. And on this non-existant class is the non-existant method contains(p:Point) which I also needed.

After a bit of refreshing my middle school geometry (yes boys and girls, you will need this when you grow up, so pay attention), I learned that calculating whether or not a point is inside a polygon is relatively simple.

But still, converting it to code was going to be a bit of hassle. So, I opted for an approach I've used in the past.

I checked out the Java API docs, and sure enough, there's a Polygon. And even more fortunately, there's a contains(...) method.

I then downloaded the JDK and installed it. Sitting in C:/Program Files/Java/jdk1.6.0_10/ was src.zip. I unpacked this zip file, and voila!, I had a clean implementation of the Polygon contains method.

I've used this approach in the past. Unlike a lot of code out there, the JDK code tends to be clean, efficient and well organized. Of course, knowing the algorithm for how the contains method worked was critical, as I didn't want to simply be copying code down blindly.

Next time you get hung up on an algorithm, see of our buddies at SUN have already solved it.

No comments:

Post a Comment