Wow, WordPress 2.7′s self-update really works! It also “updated” my customisations on the theme, so the banner is nuked. I have a backup but have been meaning to make a new one anyway. Hopefully I can pull something together over the next day or so.
11
2009
Embarrassing myself…
In 2006 I wrote some code to work out the coordinates of a point on the circumference of a circle based on an angle. This was so I could draw this nice clock in PHP:
It should display the current time in Ireland (though my server’s clock seems about 4 minutes behind).
The following code calculates the point at angle $angle on the circumference of the circle of radius $r centred on (0,0).
function findposc($angle, $r) { if ($angle > 180) $ang = $angle - 180; $ang = 90-$angle; $m = tan(deg2rad($ang)); $x = sqrt(($r*$r) / (1+($m*$m))); $y = $m*$x; if ($angle>180) { $x = $x*(-1); $y = $y*(-1); } return $res; }
Look at it! It’s embarrassing! I don’t even know what “$m” is supposed to be! I remember sitting down and working it out on paper, but I don’t know how my understanding of trigonometry was so horrible considering I had just done the leaving cert. 90-$angle? Why?? $y ends up being this (pseudo-code, and $angle is in radians):
$y = tan($angle) * (sqrt(($radius^2) / (1+(tan^2($angle)))
Looks badass, I know, but what Y really should be is:
$y = $radius * cos($angle)
I honestly couldn’t sleep the night I saw this code. I went to bed and after going over and over the trig in my head for about 20 minutes, I had to get up and rewrite it. Here’s the proper version in Java:
Point2D.Double calcPointOnCircle( double angle, double radius ) { double angleAsRad = angle / (180/Math.PI); Double x = radius * Math.sin( angleAsRad ); Double y = -radius * Math.cos( angleAsRad ); return new Point2D.Double( x, y ); }
… and then I could sleep.
11
2009
FYP Development: “(?) is totally broken and I can’t fix it!”
There have been a few occasions where something is totally, unexplainably broken. I usually spend a few hours working on it, stepping through the entire execution of what is invariably multithreaded code, and just cannot find what’s wrong.
A few days ago, my job queue just wasn’t working nicely with collision callbacks. You can attach “collision listeners” to rigid bodies in Havok, so when they collide, your implementation of the collision listener gets called. The problem I was seeing was that one callback in the listener seemed to cause one or two other callbacks to get fired when it was processed. So every time a worker thread processed a job, the queue would stay the same size.
After a while, I just decided I’d comment it out, ignore it, and hope that it would get fixed automatigically at some stage later. After doing some work on areas of code related to the listener today, I uncommented the buggy code and it worked! The job queue never gets more than one entry in it now!
This has happened a few times, and I’m a big supporter of ignoring buggy code after a certain amount of effort doesn’t fix it. I’ll just write a note about it somewhere, leave it alone, and when I check in on the broken code a few days later something else has fixed it.
I believe this is thanks to all the refactoring I do. Cleaning up how objects interact with each other usually ends up working wonders for fixing “weird” behaviour.
