← Back to index

Distance between two points

The straight-line distance between two points. Move the mouse over the canvas.

See also: Point-in-circle — the hit test is the same calculation, skipping the square root.

Pseudocode

dx = a.x - b.x
dy = a.y - b.y
distance = sqrt(dx * dx + dy * dy)

Source

const originX = 150
const originY = 150

const dx = originX - mouseX
const dy = originY - mouseY
const distance = Math.floor(Math.sqrt(dx * dx + dy * dy))