This is possibly the best script in the world. Used for fitting templates to maps.
Template fitting code for fitting templates