Tomas Vesely Tomas Vesely - 4 years ago 100
Javascript Question

x y plane coordinates - find is how many points in the list above are located a distance less than or equal to the radius

Hello I am new to JavaScript, and I have a little project for school that I struggle with. We have a data array set such like [{x:-33,y:83},{x:81,y:-99},{x:-13,y:-89},{x:13,y:-22},{x:-17,y:55},{x:78,y:-96},
{x:77,y:99},{x:-81,y:27}]. I have to use this [formula][1]

[1]: https://i.stack.imgur.com/kaSkf.png to compute it and what we need to find is how many points in the list above are located a distance less than or equal to the radius from the point the user provides. Any help is appreciated! Thank you

Answer Source

I'll show you a decent way to go about doing this problem, but you need to implement the logic yourself

var pts = [{x:-33,y:83},{x:81,y:-99},{x:-13,y:-89},{x:13,y:-22},{x:-17,y:55},{x:78,y:-96}, {x:77,y:99},{x:-81,y:27}];

var allowedDistance = 40;
var pt = {x: 0, y: 0}; //the radius point the user provides

let validPts = pts.filter(function (currPt){
    let distance = //use formula here to calculate distance between pt and currPt
    return //return true if the distance is within your allowed distance otherwise false
});

console.log(validPts); //or validPts.length if you just want to know how many points are within allowedDistance
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download