DEV Community


Discussion on: What’s your alternative solution? Challenge #46

miketalbot profile image
Mike Talbot

My first thought is that - hmm, that's as good as it gets. Then as a game programmer I wonder "Why do you want to know?" Because if the question was - given 2 points see if they are less than 40 apart - a much faster solution presents itself. Square roots are very expensive... multiplies not so much.

    function withinRange(x1, y1, x2, y2, range) {
        var dx = x2 - x1
        var dy = y2 - y1
        return (dx * dx + dy * dy) < range * range
easrng profile image


let getDistance=(x1, y1, x2, y2, r)=>((x2-x1)**2+(y2-y1)**2) < r**2;