Skip to content

Instantly share code, notes, and snippets.

@fguillen
Created March 3, 2012 12:22
Show Gist options
  • Save fguillen/1965850 to your computer and use it in GitHub Desktop.
Save fguillen/1965850 to your computer and use it in GitHub Desktop.
Calculation of the coordinates or a point in the middle of two points
function distancePoints( xA, yA, xB, yB ){
var xDistance = Math.abs( xA - xB );
var yDistance = Math.abs( yA - yB );
return Math.sqrt( Math.pow( xDistance, 2 ) + Math.pow( yDistance, 2 ) );
}
function coordinatesMediumPoint( xA, yA, xB, yB, distanceAC ){
var distanceAB = distancePoints( xA, yA, xB, yB );
var angleAB = Math.atan2( ( yB - yA ), ( xB - xA ) );
var deltaXAC = distanceAC * Math.cos( angleAB );
var deltaYAC = distanceAC * Math.sin( angleAB );
var xC = xA + deltaXAC;
var yC = yA + deltaYAC;
return { x: xC, y: yC };
}
@ereyesgt
Copy link

hello can you tell me what unit of measurement is the distance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment