Distance & Midpoint


When dealing with lines and points on a plane it is often necessary to find the distance of lines and between points and the midpoint of them. Give this script the cordinates of your points and it will calculate both distance and midpoint.

In Action

(,)
(,)

Code

function calculate() {
	var y1 = parseInt(document.getElementById('y1').value);
	var y2 = parseInt(document.getElementById('y2').value);
	var x1 = parseInt(document.getElementById('x1').value);
	var x2 = parseInt(document.getElementById('x2').value);
				
	var inSq = ((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1));
	var decSq = Math.sqrt(((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1)));
	var midpointX = (x1 + x2) / 2;
	var midpointY = (y1 + y2) / 2;
	
	document.getElementById('value').innerHTML = 
	  "distance = square root of " 
	  	+ inSq + " = " + "<b>" + decSq + "</b>"
		+ "<br/> midpoint = <b>(" + midpointX + "," 
		+ midpointY + ")</b>";
}
Like some of the other scripts. This one is short on commentation. I'll get on that soon

Javascript File

All content here is on the MIT License and you are (mostly) free to use it
Novis Design | Feedback