C++ port of codes from http://www.metanetsoftware.com/technique/tutorialA.html & http://www.metanetsoftware.com/technique/tutorialB.html