This isn't particularly difficult from an algorithm perspective, but you've made the rookie mistake of giving us no examples of what you want for input settings or output data and then expecting us to magically divine the answer. I'm just going to +1 the decent answers here and move on.