我想创建一个包含三个变量的数组,这些变量可以是true或false(即8种可能的组合)。
我正在尝试创建此图像左上角的立方体:
因此,输出应该类似于:
points = [
// first square
{
id: '000',
truths: [false, false, false]
position: [0, 0]
},
{
id: '100',
truths: [true, false, false]
position: [5, 0]
},
{
id: '010',
truths: [false, true, false]
position: [0, 5]
},
{
id: '110',
truths: [true, true, false]
position: [5, 5]
},
// second square
{
id: '001',
truths: [false, false, true]
position: [2.5, 2.5]
},
{
id: '101',
truths: [true, false, true]
position: [7.5, 2.5]
},
{
id: '011',
truths: [false, true, true]
position: [2.5, 7.5]
},
{
id: '111',
truths: [true, true, true]
position: [7.5, 7.5]
},
];
lines = [
{ from: '000', to: '100' },
{ from: '000', to: '010' },
{ from: '000', to: '001' },
{ from: '100', to: '101' },
{ from: '100', to: '110' },
{ from: '001', to: '101' },
{ from: '001', to: '011' },
{ from: '101', to: '001' },
{ from: '101', to: '111' },
...
]
我不知道如何遍历所有可能的真值并创建这些点。
一种方法是使用for循环。
for (var i=0; i<Math.pow(2, 3); i++) {
...
}
但这并不能帮助我分配可能的真值。
0...2^n
的位。truths
中的n个值将是整数的位。 - plasmacel