The alogorithim for this isn't that simplistic. I have done something similar using the XNA framework but can't remember the exact alogoritihim.
that link is an XNA resource, ignore all of the Draw and Update code and focus on the calculations that are preformed about half way down through the source.
Read this if you want to know how to get a correct reply for your question:
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
Technical Editor for: Professional Search Engine Optimization with ASP.NET