src/inputjs/get-distance.js
raw| 1 | import { PROPS_XY } from './input-consts'; |
| 2 | |
| 3 | /** |
| 4 | * @private |
| 5 | * calculate the absolute distance between two points |
| 6 | * @param {Object} p1 {x, y} |
| 7 | * @param {Object} p2 {x, y} |
| 8 | * @param {Array} [props] containing x and y keys |
| 9 | * @return {Number} distance |
| 10 | */ |
| 11 | export default function getDistance(p1, p2, props) { |
| 12 | if (!props) { |
| 13 | props = PROPS_XY; |
| 14 | } |
| 15 | let x = p2[props[0]] - p1[props[0]]; |
| 16 | let y = p2[props[1]] - p1[props[1]]; |
| 17 | |
| 18 | return Math.sqrt((x * x) + (y * y)); |
| 19 | } |