starmap.js
// create connections between all the planets
create connections between all the planets
create connections between all the planets
function generateConnections() {
	setConnectedToStart(0);
	var distance;
	for (var i = 0; i < planets.length; i++)
	{
		if (planets[i].numConnections == 0)
		{
			distance = (columns + rows) * CELL_SIZE;
			var closest = -1;
			for (var p = 0; p < planets.length; p++)
			{
				if (p != i) 
				{
					var d = distanceBetweenPoints(planets[i].x, planets[i].y, planets[p].x, planets[p].y);
					if (d < distance)
					{
						distance = d;
						closest = p;
					}
				}
			}
			var link = {point1: {x: planets[i].x,
								y: planets[i].y},
						point2: {x: planets[closest].x,
								y: planets[closest].y},
						pass: 1
						};
			planets[i].numConnections ++;
			planets[i].planetsConnected.push(closest)
			planets[closest].numConnections ++;
			planets[closest].planetsConnected.push(i);
			links.push(link);
			if (planets[i].connectedToStart)
				setConnectedToStart(closest);
			else if (planets[closest].connectedToStart)
				setConnectedToStart(i);
		}
	}
	var non_connected = getConnectedPlanets(false); 
	while (non_connected != null && non_connected.length > 0)
	{
		var connected = getConnectedPlanets(true);
		distance = (columns + rows) * CELL_SIZE;
		var closest_pair = {connected: -1, non_connected: -1}; 
		for (var nc = 0; nc < non_connected.length; nc++)
		{
			for (var c = 0; c < connected.length; c++)
			{
				var d = distanceBetweenPoints(non_connected[nc].x,
												non_connected[nc].y,
												connected[c].x,
												connected[c].y);
				if (d < distance)
				{
					distance = d;
					closest_pair.connected = connected[c].index;
					closest_pair.non_connected = non_connected[nc].index;
				}
			}
		}
		var planet_connected = planets[closest_pair.connected];
		var planet_non_connected = planets[closest_pair.non_connected];
		var link = {point1: {x: planet_connected.x,
							y: planet_connected.y},
					point2: {x: planet_non_connected.x,
							y: planet_non_connected.y},
					pass: 2
					};
		planet_connected.numConnections ++;
		planet_connected.planetsConnected.push(closest_pair.non_connected)
		planet_non_connected.numConnections ++;
		planet_non_connected.planetsConnected.push(closest_pair.connected);
		links.push(link);
		setConnectedToStart(closest_pair.non_connected);
		non_connected = getConnectedPlanets(false);
	}
	var planets_to_check = planets.slice(0); 
	var max_distance = 3 * CELL_SIZE;
	var added = false;
	for (var i = planets_to_check.length - 1; i >= 0; i--)
	{
		added = false;
		for (var p = planets_to_check.length - 1; p >= 0; p--)
		{
			if (added) break;
			if (p != i && planets_to_check[i].planetsConnected.indexOf(planets_to_check[p].index) == -1)
			{
				distance = distanceBetweenPoints(planets_to_check[i].x,
													planets_to_check[i].y,
													planets_to_check[p].x,
													planets_to_check[p].y);
				if (distance < max_distance)
				{
					var link = {point1: {x: planets_to_check[i].x,
										y: planets_to_check[i].y},
								point2: {x: planets_to_check[p].x,
										y: planets_to_check[p].y},
								pass: 3
								};
					planets_to_check[i].numConnections ++;
					planets_to_check[i].planetsConnected.push(p)
					planets_to_check[p].numConnections ++;
					planets_to_check[p].planetsConnected.push(i);
					links.push(link);
					planets_to_check.splice(i, 1);
					added = true;
				}
			}
		}
	}
}
function generateConnections ( ) { setConnectedToStart ( 0 ) ; var distance ; for ( var i = 0 ; i < planets . length ; i ++ ) { if ( planets [ i ] . numConnections == 0 ) { distance = ( columns + rows ) * CELL_SIZE ; var closest = - 1 ; for ( var p = 0 ; p < planets . length ; p ++ ) { if ( p != i ) { var d = distanceBetweenPoints ( planets [ i ] . x , planets [ i ] . y , planets [ p ] . x , planets [ p ] . y ) ; if ( d < distance ) { distance = d ; closest = p ; } } } var link = { point1 : { x : planets [ i ] . x , y : planets [ i ] . y } , point2 : { x : planets [ closest ] . x , y : planets [ closest ] . y } , pass : 1 } ; planets [ i ] . numConnections ++ ; planets [ i ] . planetsConnected . push ( closest ) planets [ closest ] . numConnections ++ ; planets [ closest ] . planetsConnected . push ( i ) ; links . push ( link ) ; if ( planets [ i ] . connectedToStart ) setConnectedToStart ( closest ) ; else if ( planets [ closest ] . connectedToStart ) setConnectedToStart ( i ) ; } } var non_connected = getConnectedPlanets ( false ) ; while ( non_connected != null && non_connected . length > 0 ) { var connected = getConnectedPlanets ( true ) ; distance = ( columns + rows ) * CELL_SIZE ; var closest_pair = { connected : - 1 , non_connected : - 1 } ; for ( var nc = 0 ; nc < non_connected . length ; nc ++ ) { for ( var c = 0 ; c < connected . length ; c ++ ) { var d = distanceBetweenPoints ( non_connected [ nc ] . x , non_connected [ nc ] . y , connected [ c ] . x , connected [ c ] . y ) ; if ( d < distance ) { distance = d ; closest_pair . connected = connected [ c ] . index ; closest_pair . non_connected = non_connected [ nc ] . index ; } } } var planet_connected = planets [ closest_pair . connected ] ; var planet_non_connected = planets [ closest_pair . non_connected ] ; var link = { point1 : { x : planet_connected . x , y : planet_connected . y } , point2 : { x : planet_non_connected . x , y : planet_non_connected . y } , pass : 2 } ; planet_connected . numConnections ++ ; planet_connected . planetsConnected . push ( closest_pair . non_connected ) planet_non_connected . numConnections ++ ; planet_non_connected . planetsConnected . push ( closest_pair . connected ) ; links . push ( link ) ; setConnectedToStart ( closest_pair . non_connected ) ; non_connected = getConnectedPlanets ( false ) ; } var planets_to_check = planets . slice ( 0 ) ; var max_distance = 3 * CELL_SIZE ; var added = false ; for ( var i = planets_to_check . length - 1 ; i >= 0 ; i -- ) { added = false ; for ( var p = planets_to_check . length - 1 ; p >= 0 ; p -- ) { if ( added ) break ; if ( p != i && planets_to_check [ i ] . planetsConnected . indexOf ( planets_to_check [ p ] . index ) == - 1 ) { distance = distanceBetweenPoints ( planets_to_check [ i ] . x , planets_to_check [ i ] . y , planets_to_check [ p ] . x , planets_to_check [ p ] . y ) ; if ( distance < max_distance ) { var link = { point1 : { x : planets_to_check [ i ] . x , y : planets_to_check [ i ] . y } , point2 : { x : planets_to_check [ p ] . x , y : planets_to_check [ p ] . y } , pass : 3 } ; planets_to_check [ i ] . numConnections ++ ; planets_to_check [ i ] . planetsConnected . push ( p ) planets_to_check [ p ] . numConnections ++ ; planets_to_check [ p ] . planetsConnected . push ( i ) ; links . push ( link ) ; planets_to_check . splice ( i , 1 ) ; added = true ; } } } } }
create connections between all the planets
create connections between all the planets
// the start planet is connected, obviously // 1. link each planet to its closest neighbor // if this is not the current planet  // add a new link // update the connectedToStart bool // 2. while we have some non_connected planets, find the closest pair //    of connected/non_connected and link them // returns a list of planets // indices of planets // add a new link // 3. try adding an extra connection to each planet, in case there's //    something nearby // clones the array