Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I got reminded of a rather cute snippet of code I had a part in.

One needed to find the coordinates of the bisector of an angle subtended by an arc of a unit circle. They (x,y) coordinates of the 'arms' were available. The existing implementation was a mass of trigonometry - going from the x,y coordinates to polar (r,θ), then making sure that the θ computed was in the correct quadrant, halving the θ and converting back to x,y coordinates. All in all, many calls to trigonometric functions and inverse functions.

This was in Python and thus we had first class access to complex numbers. So all that was needed was to define two complex numbers. z1 from (x1,y1) z2 from (x2,y2) and then take the geometric mean of the product: √(z1*z2). Done.

No explicit trigonometry and no explicit conversion and back in the new code.



Reminds me of this article which I'm often drawn back to.

https://fgiesen.wordpress.com/2010/10/21/finish-your-derivat...


That was a good read, thanks. Bookmarked




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: