There's so many ways to do it and it involves quite a bit of code. Besides, it appears to be a large part of the assignment.

How would you do this on paper? Write out the steps, I gave a real quick summary of one method in that post. Once the steps make sense on paper, then you should try to move it to code.