I have canvas drawing tab and want lineWidth to be based on distance between two last mousemove coordinate updates. I will make translation of distance to width myself, I just need to know how to get distance between those points (I already have coordinates of those pointes).
7 Answers
You can do it with pythagoras theorem
If you have two points (x1, y1) and (x2, y2) then you can calculate the difference in x and difference in y, lets call them a and b.
var a = x1 - x2;
var b = y1 - y2;
var c = Math.sqrt( a*a + b*b );
// c is the distance

- 17,638
- 8
- 69
- 66

- 3,481
- 1
- 19
- 21
-
35you can shorten var c = Math.sqrt( a*a + b*b ); to var c = Math.hypot(a,b); – evgpisarchik Oct 12 '17 at 12:38
-
2a^2 + b^2 = c^2 Hypotenus equation – Kad Sep 20 '18 at 03:02
-
Is it any difference if you go `x1 - x2, y1 - y2` or `x2 - x1, y2 - y1`? – Rami Chasygov Jan 15 '20 at 08:55
-
6@RamzanChasygov There is no difference in this case because each value is squared! So whether the order was like `7 - 5 = 2` or `5 - 7 = -2` won't matter. `-2 * -2 = 4` `2 * 2 = 4` – rdmurphy Apr 28 '20 at 19:30
Note that Math.hypot
is part of the ES2015 standard. There's also a good polyfill on the MDN doc for this feature.
So getting the distance becomes as easy as Math.hypot(x2-x1, y2-y1)
.

- 1,961
- 1
- 11
- 2
-
3I won't recommend using `Math.hypot()` to calculate the distance when performances matters. It is about 100 times slower. – Markus Zeller Jan 02 '21 at 14:44
-
1Here is an explanation of why the Math.hypot is slower, basically because it is going to be more accurate. That may or may not matter to you. https://stackoverflow.com/questions/3764978/why-hypot-function-is-so-slow – RayB Apr 04 '21 at 19:32
http://en.wikipedia.org/wiki/Euclidean_distance
If you have the coordinates, use the formula to calculate the distance:
var dist = Math.sqrt( Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2) );
If your platform supports the **
operator, you can instead use that:
const dist = Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2);

- 743
- 11
- 21

- 900
- 1
- 12
- 28
To find the distance between 2 points, you need to find the length of the hypotenuse in a right angle triangle with a width and height equal to the vertical and horizontal distance:
Math.hypot(endX - startX, endY - startY)

- 7,738
- 4
- 38
- 58
Here's a quick one-liner to find the distance between (x1, y1)
and (x2, y2)
const distance = (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1);
Here is a short runnable demo:
const distance = (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1);
var x1 = 1
var y1 = 5
var x2 = 4
var y2 = 5
var d = distance(x1, y1, x2, y2)
console.log(`The distance between (${x1}, ${y1}) and (${x2}, ${y2}) is ${d}`)

- 1,044
- 7
- 20
i tend to use this calculation a lot in things i make, so i like to add it to the Math object:
Math.dist=function(x1,y1,x2,y2){
if(!x2) x2=0;
if(!y2) y2=0;
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
Math.dist(0,0, 3,4); //the output will be 5
Math.dist(1,1, 4,5); //the output will be 5
Math.dist(3,4); //the output will be 5
Update:
this approach is especially happy making when you end up in situations something akin to this (i often do):
varName.dist=Math.sqrt( ( (varName.paramX-varX)/2-cx )*( (varName.paramX-varX)/2-cx ) + ( (varName.paramY-varY)/2-cy )*( (varName.paramY-varY)/2-cy ) );
that horrid thing becomes the much more manageable:
varName.dist=Math.dist((varName.paramX-varX)/2, (varName.paramY-varY)/2, cx, cy);

- 693
- 5
- 10
The distance between two coordinates x and y! x1 and y1 is the first point/position, x2 and y2 is the second point/position!
function diff (num1, num2) {
if (num1 > num2) {
return (num1 - num2);
} else {
return (num2 - num1);
}
};
function dist (x1, y1, x2, y2) {
var deltaX = diff(x1, x2);
var deltaY = diff(y1, y2);
var dist = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
return (dist);
};

- 73
- 5
-
-
6You don't need to use `diff` as squaring a number will always result in a positive number. If `x1 - y1` is negative, `(x1 - y1) ^ 2` is positive still. – Radvylf Programs Dec 17 '18 at 01:32