Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const DistToLineEx = (p, sA, sB) => {
- let dx, dy, d
- let f
- let result = { nearest: {}, result: 0 }
- result.nearest.x = sA.x
- result.nearest.y = sA.y
- dx = sB.x - sA.x
- dy = sB.y - sA.y
- d = dx * dx + dy * dy
- if (d === 0) {
- result.result = Math.hypot(p.x - sA.x, p.y - sA.y)
- return result
- }
- f = ((p.x - sA.x) * dx + (p.y - sA.y) * dy) / d
- if (f < 0) {
- result.result = Math.hypot(p.x - sA.x, p.y - sA.y)
- } else if (f > 1) {
- result.nearest = sB
- result.result = Math.hypot(p.x - sB.x, p.y - sB.y)
- }
- result.nearest.x = Math.round(sA.x + f * dx)
- result.nearest.y = Math.round(sA.y + f * dy)
- result.result = Math.hypot(p.x - result.nearest.x, p.y - result.nearest.y)
- return result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement