There are probably 2 steps to your challenge.
The first would be the actual math of it, something which is probably covered in many places, like here.
I could give you straight up a built project, but I don't feel neither me nor you would gain anything from it in the end. Maybe I'm wrong, sorry if I am.
I'd be glad to answer any specific question you have while you build it though.