javascript – 在由不同颜色的立方体组成的网格中,如何找到匹配的聚类?

定义集群

任何一组相同颜色的立方体,触摸面平面,而不是它们的角落.

簇将形成实心几何形状.

帮助可视化问题

让我们假设这些乐高积木中的每一个都是1×1单位大.

legos

一个简化的代码示例中 – 让我们看一下由1x1x1立方体组成的2x2x2网格:

var mesh = [ 

  // First layer   ( x,y,z )
  new THREE.Vector3( 0,0 ),new THREE.Vector3( 0,1 ),new THREE.Vector3( 1,1 )

  //Second layer   ( x,1,1 )
];

enter image description here

网格中的每个立方体都有一个颜色:

//Indexes of mesh array sorted by color
var colors = {
  red: [0,4,6],green: [2,3,5,7]
}
最佳答案
这可以通过flood fill解决.维基百科页面对于两个维度是有益的:

Flood-fill (node,target-color,replacement-color):
 1. If target-color is equal to replacement-color,return.
 2. If the color of node is not equal to target-color,return.
 3. Set the color of node to replacement-color.
 4. Perform Flood-fill (one step to the south of node,replacement-color).
    Perform Flood-fill (one step to the north of node,replacement-color).
    Perform Flood-fill (one step to the west of node,replacement-color).
    Perform Flood-fill (one step to the east of node,replacement-color).
 5. Return.

如果它们的距离为1,则可以通过观察两个单元格是相邻的,或者更简单地如果它们在一个维度上相差一个,则可以将其扩展为三维,因此您可以迭代所有六个邻居而不是两个维度中的四个.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...