css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas id="canvas" width="200" height="200"></canvas>
            
          
!
            
              (function() {
	let canvas = document.getElementById("canvas");
	let context = canvas.getContext("2d");

	/*  Initialize variables necessary to run the script.  “allPixels” is an array of 50 randomly-placed 
	pixels on the canvas; “unvisited” is an array of unvisited pixels; “path” is the TSP tour.  */
	let allPixels = [],
		unvisited = [],
		path = [];

	//Returns the distance between two points
	let getDistance = function(p1, p2){
		var a = Math.pow(p2.x-p1.x,2);
		var b = Math.pow(p2.y-p1.y,2);
		return Math.sqrt((a+b));
	}

	/*  Returns the given pixel’s nearest neighbor.  Accepts a pixel and an array of unvisited pixels.  */
	let getNearestNeighbor = function(pixel, unvisited){
		var shortestDistance = Infinity;

		//initialize the nearest pixel (the return value) and the nearest index
		var nearest = unvisited[0];
		var nearestIndex = 0;

		for(var i=0; i<unvisited.length; i++){
			if((pixel.x != unvisited[i].x) 
				&& (pixel.y != unvisited[i].y)){	//make sure they're not the same pixel
				var d = getDistance(pixel, unvisited[i]);
	    		if(d <= shortestDistance){
	    			shortestDistance = d;
	    			nearest = unvisited[i];
	    			nearestIndex = i;
	    		}
			}
		}
		//remove this pixel from the “unvisited” array since it's now been visited
		unvisited.splice(nearestIndex,1);
		return nearest;
	}

	//Finds the TSP tour and assigns it to the "path" variable
	let getTSPTour = function() {
		var prevPixel = unvisited[0];
		path.push(unvisited[0]);
		unvisited.splice(0,1);

		 while(unvisited.length > 0){   	
			var closest = (getNearestNeighbor(prevPixel, unvisited)); 
			path.push(closest);
			prevPixel = closest;
	    }
	}

	//Draws the path using nearest neighbors heuristic
	let draw = function(){	
	    for(let i=0; i<path.length; i++){   	
	    	context.beginPath();
	    	context.moveTo(path[i].x, path[i].y);
			context.lineTo(path[(i+1)%path.length].x, path[(i+1)%path.length].y);
			context.stroke();
	    }
	}

	//Simple Pixel data structure
	function Pixel(x,y) {
		this.x = x;
		this.y = y;
	}

	//Place random points on canvas
	for(let i=0; i<50; i++){
		let x = Math.floor(Math.random() * canvas.width);
		let y = Math.floor(Math.random() * canvas.height);
		let pixel = new Pixel(x,y);
		allPixels.push(pixel);
		context.fillRect(x,y,1,1);
	}

	//Populate the array of unvisited pixels
	for(let i=0; i<allPixels.length; i++){
		unvisited.push(allPixels[i]);
	}

	getTSPTour();
	draw();
})();

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console