This is a translation of the code that is used for measuring distances in Roll20. I've restructured it a bit and set it up to work with the API (I didn't bother with the hex portion of the code): var getDistanceInScale = function(startPoint, endPoint, page) {
'use strict';
var measuredLength,
lengthInGridUnits = 0,
diagonalType = page.get("diagonaltype"),
scaleNumber = parseFloat(page.get("scale_number")),
distance,
lengthX = endPoint.x - startPoint.x,
lengthY = endPoint.y - startPoint.y;
switch(diagonalType){
case 'foure':
lengthX = Math.abs(lengthX);
lengthY = Math.abs(lengthY);
distance = Math.max(lengthX, lengthY);
break;
case "threefive":
lengthX = Math.abs(lengthX);
lengthY = Math.abs(lengthY);
var gridSize = Math.round(
parseFloat(
page.get("snapping_increment") || 1
) * 70
);
lengthInGridUnits = Math.floor(Math.min(lengthX, lengthY) / gridSize);
distance = Math.floor(lengthInGridUnits / 2) * gridSize + Math.max(lengthX, lengthY);
break;
default:
distance = "manhattan" == diagonalType ? Math.abs(lengthX) + Math.abs(lengthY) : Math.sqrt(lengthX * lengthX + lengthY * lengthY);
break;
}
measuredLength = distance / 70;
return Math.round(measuredLength * scaleNumber * 10) / 10;
};
The return is the distance in Units between the two points using the settings of the provided page.