Please see my comment to the question.
One of the way to perform necessary calculations is using
, more exactly:
There are some code samples on the pages of each of these methods, just to understand how to use them. Most important thing here is not to use
as those method would work prohibitively slowly for your purpose.