You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从地块坐标数组中提取地产边界地块的x、y坐标

How to Filter Boundary Plots from Your Land Coordinate Array

Hey there! Let's figure out how to pick out those boundary plots (the ones marked with red lines in your diagram) from your landArray. First, let's clarify what makes a plot a boundary one: a plot is on the boundary if at least one of its four adjacent plots (up, down, left, right) isn't present in your land array. If all four neighbors exist, it's an inner plot and we can skip it.

Step 1: Convert the Array to a Set for Fast Lookups

First, we'll turn your array of string coordinates into a set. This lets us check if a coordinate exists in O(1) time, which makes the whole process way more efficient.

const landArray = ["-4,2", "-3,2", "-2,2", "-1,2", "-1,1", "-1,0", "1,0", "0,0", "2,0", "3,0", "4,0", "4,-1", "4,-2", "4,-3", "4,-4", "-4,1", "-4,-1", "-4,0", "-4,-2", "-4,-3", "-4,-4", "-2,-4", "-3,-4", "-1,-4", "0,-4", "1,-4", "2,-4", "3,-4", "-3,1", "-2,1", "-2,0", "-3,0", "-3,-1", "-2,-1", "0,-1", "-1,-1", "1,-1", "3,-1", "2,-1", "3,-2", "3,-3", "2,-2", "2,-3", "1,-3", "1,-2", "0,-2", "0,-3", "-1,-3", "-1,-2", "-2,-2", "-3,-2", "-3,-3"];
// Convert to a set so we can quickly check if a coordinate exists
const landSet = new Set(landArray);

Step 2: Check Each Plot to See if It's a Boundary

Next, we'll loop through every coordinate in your original array. For each one, we'll split out the x and y values, then check all four adjacent coordinates. If any neighbor is missing from the set, this plot is a boundary one.

const boundaryPlots = [];

for (const coord of landArray) {
  // Split the string into x and y numbers
  const [xStr, yStr] = coord.split(',');
  const x = parseInt(xStr);
  const y = parseInt(yStr);
  
  // List all four adjacent coordinates (right, left, up, down)
  const adjacentCoords = [
    `${x + 1},${y}`, // Right neighbor
    `${x - 1},${y}`, // Left neighbor
    `${x},${y + 1}`, // Top neighbor
    `${x},${y - 1}`  // Bottom neighbor
  ];
  
  // If any adjacent coordinate isn't in our land set, this is a boundary plot
  const isBoundary = adjacentCoords.some(adjacent => !landSet.has(adjacent));
  
  if (isBoundary) {
    boundaryPlots.push(coord);
  }
}

// Log the result to see all boundary plots
console.log(boundaryPlots);

Verify the Result

When you run this code, the boundaryPlots array will hold exactly the coordinates of the red-lined plots from your diagram. You can cross-reference this with your original array to make sure it matches what you expect.

Quick Note on Edge Cases

If you ever need to consider diagonal neighbors as part of the boundary check, you can just add those four diagonal coordinates to the adjacentCoords array. But based on your description of the red lines, the four cardinal directions are all we need here.


内容的提问来源于stack exchange,提问作者Genie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 16:39:06