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.
