微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/js/429241.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐