/*jslint plusplus: true */
/*globals self, importScripts, utils */

(function () {
	'use strict';
	
	importScripts('utils.js');
	
	function getRandomPoint(w, h) {
		return [utils.getRandomArbitary(0, 1) * w, utils.getRandomArbitary(0, 1) * h];
	}
	
	self.addEventListener('message', function (e) {
		var points = [], vertices = e.data[2], size = e.data[1], point, pointSkip = 100,
			i = parseInt(e.data[0], 10) + pointSkip, j, length = vertices.length,
			frac = e.data[3], cvert, numvertex, correction;
		
		correction = 1 / frac - 1; // make points fit in the container
		point = getRandomPoint(size[0], size[1]);
		
		do {
			numvertex = utils.getRandomInt(0, vertices.length - 1);
			cvert = vertices[numvertex];
			point = [(cvert[0] + point[0]) * (frac), (cvert[1] + point[1]) * (frac)];
			
			if (i > pointSkip) { // skip the first few points
				points.push([
					[
						point[0] * correction,
						point[1] * correction
					], numvertex
				]);
			}
		} while (i--);
		
		self.postMessage(points);
	}, false);
}());