如何获取2D平面中中心点(0,0)周边距离≤5的所有点?代码优化
Hey there! Let's break down your questions and fix up that code so it works exactly as you need.
问题1:如何在2D平面中找到中心点周围的所有点?
First, we need to clarify what "周围的点" means—usually this refers to all integer coordinate points that lie within or equal to a specified distance from the center point. The core approach here is:
- Define the range of possible x and y values (any point outside
[centerX - distance, centerX + distance]or[centerY - distance, centerY + distance]will definitely be too far) - Iterate through every combination of x and y in that range
- Calculate the distance from each point to the center, and keep only the points that meet your distance requirement
问题2:中心点(0,0),获取所有距离不超过5的点?
Let's first spot the issues in your current code:
- Your loop uses
i < distance, which means it stops ati = 4when distance is 5—you're missing all points exactly 5 units away - You're only adding points along the axes (up/down/left/right) and the positive/negative diagonals. This misses tons of valid points like (1,2), which has a distance of ~2.23 from (0,0) (well within 5)
- There's no actual distance check—you're just assuming points at offset
iare valid, which isn't accurate for non-axis/diagonal points
Fixed Code (Euclidean Distance)
This version covers all valid points, including the center itself, and correctly checks the distance:
function getPoints(startX, startY, distance) { const res = []; // Iterate over all possible x values in the valid range for (let x = startX - distance; x <= startX + distance; x++) { // Iterate over all possible y values in the valid range for (let y = startY - distance; y <= startY + distance; y++) { // Calculate squared distance to avoid floating-point sqrt (faster + avoids precision issues) const distanceSquared = Math.pow(x - startX, 2) + Math.pow(y - startY, 2); // Compare squared distance to squared threshold (equivalent to checking actual distance) if (distanceSquared <= Math.pow(distance, 2)) { res.push({ x, y }); } } } console.log(res); console.log(`Total valid points: ${res.length}`); return res; } // Test with your requirements: center (0,0), max distance 5 getPoints(0, 0, 5);
Key Details:
- Squared Distance Check: We avoid calculating the square root because it's unnecessary for comparisons and can introduce tiny floating-point errors. Squaring both sides of the inequality
distance ≤ thresholdgives the same result. - Full Range Coverage: By iterating from
startX - distancetostartX + distance, we make sure no valid point is missed. - Includes Center Point: If you don't want the center (0,0) in your results, just add a check like
if (x !== startX || y !== startY)before pushing to the array.
If You Want Manhattan Distance Instead:
If "distance" here refers to Manhattan distance (sum of horizontal and vertical offsets: |x| + |y| ≤ 5), just modify the condition:
const manhattanDistance = Math.abs(x - startX) + Math.abs(y - startY); if (manhattanDistance <= distance) { res.push({ x, y }); }
内容的提问来源于stack exchange,提问作者chosirai
相关产品推荐
相关产品推荐

