gpt4 book ai didi

javascript - 帮助查找包含模运算符的 if 语句中的模式

转载 作者:行者123 更新时间:2023-12-01 01:37:16 24 4
gpt4 key购买 nike

我正在从事一个个人项目,我发现自己重复了很多代码:

// this code is in a function with params k & dir

// where i equals a number from 0-2 inclusive
// j = (i + 1) % 2
// k = (j + 1) % 2
// The objective is to change a.x, a.y, b.x, & b.y



1. if(k === 0 && dir === -1) { a.x = dir * array[k].x; a.y = dir * array[k].y; b.x = dir * -array[i].x; b.y = dir * -array[i].y; }
2. if(k === 0 && dir === 1) { a.x = dir * array[i].x; a.y = dir * array[i].y; b.x = dir * -array[k].x; b.y = dir * -array[k].y; }
3.
4. if(k === 1 && dir === -1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; b.x = dir * -array[i].x; b.y = dir * -array[i].y; }
5. if(k === 1 && dir === 1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; b.x = dir * -array[i].x; b.y = dir * -array[i].y; }
6.
7. if(k === 2 && dir === -1) { a.x = dir * -array[j].x; a.y = dir * array[j].y; b.x = dir * array[i].x; b.y = dir * -array[i].y; }
8. if(k === 2 && dir === 1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; b.x = dir * array[j].x; b.y = dir * -array[j].y; }

我两天来一直在尝试减少代码,但没有成功,坦率地说,我相信一定有办法不使用所有这些 if 语句。

我只是想在正确的方向上引起轰动,尽管非常欢迎完整的解决方案。

到目前为止,我尝试再次使用模运算符之类的操作,但问题出在第 78 行等部分,因为数组索引不同,具体取决于判断您的对象是 a 还是 b。所以我相信必须至少有两个 if 语句,但我只是迷失了。

再说一遍,这最重要的是成为一个谜题,所以我希望它至少对编码专家来说是有趣的:)

编辑

代码已经可以工作,只是重复,我希望找到一个更简洁的解决方案。所有值都是数字,ab 都是包含 { x: undefined, y: undefined } 的对象。

为了清楚起见,分为两个对象:

    // for a
if(k === 0 && dir === -1) { a.x = dir * array[k].x; a.y = dir * array[k].y; } // (0, -1) => (0)
if(k === 0 && dir === 1) { a.x = dir * array[i].x; a.y = dir * array[i].y; } // (0, 1) => (1)

if(k === 1 && dir === -1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; } // (1, -1) => (2)
if(k === 1 && dir === 1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; } // (1, 1) => (2)

if(k === 2 && dir === -1) { a.x = dir * -array[j].x; a.y = dir * array[j].y; } // (2, -1) => (1)
if(k === 2 && dir === 1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; } // (2, 1) => (0)


// for b
if(k === 0 && dir === -1) { b.x = dir * -array[i].x; b.y = dir * -array[i].y; } // (0, -1) => (1)
if(k === 0 && dir === 1) { b.x = dir * -array[k].x; b.y = dir * -array[k].y; } // (0, 1) => (0)

if(k === 1 && dir === -1) { b.x = dir * -array[i].x; b.y = dir * -array[i].y; } // (1, -1) => (2)
if(k === 1 && dir === 1) { b.x = dir * -array[i].x; b.y = dir * -array[i].y; } // (1, 1) => (2)

if(k === 2 && dir === -1) { b.x = dir * array[i].x; b.y = dir * -array[i].y; } // (2, -1) => (0)
if(k === 2 && dir === 1) { b.x = dir * array[j].x; b.y = dir * -array[j].y; } // (2, 1) => (1)

每行末尾的注释显示输入 => 输出,其中 (k, dir) => 输出,输出值是赋予 array 的索引>.

array 是函数范围之外的数组。

编辑2

    for(let i = 0; i < 3; i++) {
// list has arrays inside containing coordinates in a 2d plane, following a triangular grid. Each value looks like this [0, 2, -1]
// j = next modulus value in base 3 given i
// k = next modulus value in base 3 given j
let j = (i + 1) % 3;
let k = (i + 2) % 3;

list[i] = [...old_list].sort((a, b) => { // sort list
// This provides 3 sorted lists which are sorted by xy, yz, and zx
if(a[i] === b[i]) {
if( a[j] === b[j] && Math.abs(a[k] - b[k]) ) {
my_function(a, b, i, j, k);
}
return a[j] - b[j];
}
return a[i] - b[i];
});
}

my_function(a, b, i, j, k) {
let modules = universe.modules;
let dir = b[k] - a[k];

a = {x: undefined, y: undefined};
b = {x: undefined, y: undefined};

if(k === 0 && dir === -1) { a.x = dir * module.constraints[k].x; a.y = dir * module.constraints[k].y; } // (0, -1) => (0)
if(k === 0 && dir === 1) { a.x = dir * module.constraints[i].x; a.y = dir * module.constraints[i].y; } // (0, 1) => (1)

if(k === 1 && dir === -1) { a.x = dir * -module.constraints[i].x; a.y = dir * module.constraints[i].y; } // (1, -1) => (2)
if(k === 1 && dir === 1) { a.x = dir * -module.constraints[i].x; a.y = dir * module.constraints[i].y; } // (1, 1) => (2)

if(k === 2 && dir === -1) { a.x = dir * -module.constraints[j].x; a.y = dir * module.constraints[j].y; } // (2, -1) => (1)
if(k === 2 && dir === 1) { a.x = dir * -module.constraints[i].x; a.y = dir * module.constraints[i].y; } // (2, 1) => (0)



if(k === 0 && dir === -1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (0, -1) => (1)
if(k === 0 && dir === 1) { b.x = dir * -module.constraints[k].x; b.y = dir * -module.constraints[k].y; } // (0, 1) => (0)

if(k === 1 && dir === -1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (1, -1) => (2)
if(k === 1 && dir === 1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (1, 1) => (2)

if(k === 2 && dir === -1) { b.x = dir * module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (2, -1) => (0)
if(k === 2 && dir === 1) { b.x = dir * module.constraints[j].x; b.y = dir * -module.constraints[j].y; } // (2, 1) => (1)

// doing some stuff with Matter.js(physics library) to decide where to add constraints between two bodies
}

最佳答案

这段代码可能有问题。自从我开始写这个问题以来,这个问题已经改变了,而且我可能没有跟上。但我认为这至少可以展示一种避免重复的技术:

const rules = [[0, -1, 'k', 'k', 'i', 'i'], [0, 1, 'i', 'i', 'k', 'k'], /* ... */]

const foo = (i, j, k, dir, array) => {
const idxs = {i, j, k}
const a = {}, b = {}
const [,, ax, ay, bx, by] = rules.find(([rk, rd, ..._]) => rk === k && rd === dir)
a.x = dir * array[idxs[ax]].x
a.y = dir * array[idxs[ay]].y
b.x = dir * -array[idxs[bx]].x
b.y = dir * -array[idxs[by]].y
return {a, b}
}

console.log(foo(0, 1, 0, -1, [{x: 1, y: 2}]))

重要的部分是规则列表,它实际上捕获您的逻辑,以及 idxs 对象,它允许将这些规则转换为实际值。可能有一种方法可以更进一步,对 a、b、x 和 y 进行类似的操作。但我认为这达到了可读性的平衡。

关于javascript - 帮助查找包含模运算符的 if 语句中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com