I have one array of objects which have pair of numbers and i have one given pair
let points = [{x:20,y:30}, {x:34,y:40}, {x:45,y:30}, {x:55,y:30}]
let givenNumber= {x:19,y:25}
How i can find nearest pair from array which is near to my given pair number
Anyone can help?
You could get the distance and then reduce the array by taking the smaler distance of two points of the array.
const distance = (p1, p2) => Math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2);
let points = [{ x: 20, y: 30 }, { x: 34, y: 40 }, { x: 45, y: 30 }, { x: 55, y: 30 }],
point = { x: 19, y: 25} ,
result = points.reduce((a, b) => distance(a, point) < distance(b, point) ? a : b);
console.log(result);
Related
I need to dynamically create array, knowing only how long I want it to be. So for example I need array to be 3 long so I need array = [-10, 0, 10], if I need array 9 long it will be [-40, -30, -20, -10, 0, 10, 20, 30, 40] etc. How can I do it automatically?
You can simply get the result using Array with fill
Since you only want 0 at the center that input should be odd.
const end = 9;
let start = Math.floor(end / 2);
const result = [
...Array(start).fill(0).map((_, i) => start * -10 + i * 10),
0,
...Array(start).fill(0).map((_, i) => (i + 1) * 10)
]
console.log(result)
Another solution with Array.from:
const createArray = (length) => Array.from({length}, (el, i) => Math.round(i - length / 2) * 10);
console.log(createArray(1))
console.log(createArray(3))
console.log(createArray(9))
I have hero coordinates, target coordinate and range.
Let's say my hero is at x: 1, y: 1;
Target coordinates are: x: 4, y: 4;
I'm getting every coordinate from target within range. So if range is 1 then I create array of objects like this:
[
{x: 3, y: 3},
{x: 4, y: 3},
{x: 5, y: 3},
{x: 3, y: 4},
{x: 5, y: 4},
{x: 3, y: 5},
{x: 4, y: 5},
{x: 5, y: 5}
]
just 1 sqm - 8 coordinates around target. I'm getting that with simple algorithm
const hero = {x: 1, y: 1};
const closest = {x: 4, y: 4};
const range = 1;
const coordsAround = [];
for (let i = 0; i <= range * 2; i++) {
for (let j = 0; j <= range * 2; j++) {
let newCoord = { x: closest.x - range + j, y: closest.y - range + i };
//here note
if(!((newCoord.x === 3 && newCoord.y === 3) || (newCoord.x === 4 && newCoord.y === 3))) {
coordsAround.push(newCoord);
}
}
}
but additionally before push to coordsAround I'm executing some function which check collisions. Here in example I just added if statement to simplify it a bit. And with this if I excluded 3,3 and 4,3.
So now my dashboard is like this:
( I accidently made this dashboard so it's in y,x pattern instead of x,y srr)
where pink is hero (1,1) red are collisions, gold is the target (4,4) and greens are around the target (gained from above code snippet)
Now it's pretty simple to say which green is closest if it would be without red tiles (collisions):
const realClosest = {
x: hero.x > closest.x
? closest.x + 1
: closest.x - 1,
y: hero.y > closest.y
? closest.y + 1
: closest.y - 1
};
console.log('real closest is:', realClosest, 'but it is not within coordsAournd:', coordsAround,
'so next closest coord is 3,4 or 4,3 but 3,4 is not within coordsAournd as well' +
'so closest coord is 4,3');
but as far as I have this red tiles I don't know how to tell which is second best, third best and so on..
Sort the tiles with a custom function so that coordsAround[0] is the tile that is the closest to the hero, coordsAround[coordsAround.length - 1] is the tile that is thefarthest:
function dist2(a, b) {
let dx = a.x - b.x;
let dy = a.y - b.y;
return dx*dx + dy*dy;
}
coordsAround.sort(function (a, b) {
let da = dist2(a, hero);
let db = dist2(b, hero);
if (da < db) return -1;
if (da > db) return 1;
return 0;
})
The auxiliary function dist2 calculates the square of the distance. The sorting rder will be the same, because sqrt(x) < sqrt(y) when x < y (and both values are non--negative). Given that your range is reates a square, not a circly, you could also use dist = Math.max(Math.abs(dx), Math.abs(dy)).
I don't know how to explain this but i will try my best, so sorry in advance
For example this numbers
i would like to get
5 => 1, 12 => 10, 128 => 120, 1493 => 1400, 13301 => 13000
I have came up with Math.pow(10, Math.floor(Math.log10(x)));
which returns me 1, 10, 100, 1000, 10000
You could get the exponent and a factor and check if the value is smaller than 100, then take just the decimal with the exponent or the adjusted value.
function format(v) {
var e = Math.floor(Math.log10(v)),
f = 10 ** (e - 1);
return v < 100
? 10 ** e
: Math.floor(v / f) * f;
}
console.log(...[5, 12, 128, 1493, 13301, 239584].map(format));
// 1, 10, 120, 1400, 13000, 230000
This question already has answers here:
How to find the coordinate that is closest to the point of origin?
(2 answers)
Closed 4 years ago.
I am wondering how can I calculate my coordinates to an array of coordinates and find out which one is closest?
Lets say:
var player = {
x: 10,
y: 20
}
var box = [
{x: 17, y: 30},
{x: 41, y: 14},
{x: 20, y: 30}
];
So my player stays on 10, 20. And i have 3 different boxed, and i need to find out which one is closest. Is there any easy way to caluculate that?
You could calculate the distance and reduce the array by checking each distance and return the object with the shortest.
function getDistance(p1, p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2))
}
var player = { x: 10, y: 20 },
box = [{ x: 17, y: 30 }, { x: 41, y: 14 }, { x: 20, y: 30 }];
console.log(box.reduce((a, b) => getDistance(a, player) < getDistance(b, player) ? a : b));
To find the least distance (the closest point) just find the least difference in the sum of the squares of their x and y offsets. You don't need to take the square root to find the actual distance as per Pythagorous's theorom. If you have trouble writing the code don't forget to include what you have tried.
You can use 2 points canvas formla and sort it based on distance
let positions = [
{x: 17, y: 30},
{x: 41, y: 14},
{x: 20, y: 30}
];
let x=10;
let y= 20;
let positions_close=positions.sort(p=>Math.hypot(p.x-x, p.y-y));
console.log(positions_close[0]);
//check the distance
let distances=positions.map(p=>Math.hypot(p.x-x, p.y-y));
console.log(distances);
I know there are many many questions about sorting javascript arrays by multiple values, but none of the answers solved my problem.
I have an array of coordinates like:
x | y
--------
10 20
12 18
20 30
5 40
100 2
How can I get the coordinate that is closest to the point of origin?
Calculate the distance of each point using
Math.sqrt( Math.pow(x, 2) + Math.pow(y, 2) );
Take the result that's the lowest
var points = [
{x: 10, y: 20},
{x: 12, y: 18},
{x: 20, y: 30},
{x: 5, y: 40},
{x: 100, y: 2}
];
function d(point) {
return Math.pow(point.x, 2) + Math.pow(point.y, 2);
}
var closest = points.slice(1).reduce(function(min, p) {
if (d(p) < min.d) min.point = p;
return min;
}, {point: points[0], d:d(points[0])}).point;
closest;
// {x: 12, y:18}
You'll notice that we're skipping the Math.sqrt step here. As Mark Setchell points out, calculating the square root is a sort of "lowest common denominator" operation; We can still determine the closest point by getting the smallest x^2 + y^2 value.
For each x,y pair, square x, square y and add together. Smallest number is nearest to the origin.