Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="canvas" width="200" height="200"></canvas>
              
            
!

CSS

              
                
              
            
!

JS

              
                (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

Console