0
배열을 반복하고 각 항목을 다른 모든 항목에 할당하려고합니다. 현재 console.log (i, j)에 실제로 수행되지만 내 코드는 다르게 작동하지 않습니다. 콘솔에서는 볼 수 있습니다 :각 항목에 대한 모든 배열 항목을 반복합니다.
- 1,1
- 1, 2
- 1,3
- ... 등 ...
- 19,18
- (19, 19)
I 믿어 문제는 anchorConnectors를 [내가 J를 *] 인덱스에서 배열을 만드는 생성된다는 것이다 연속적이지 않은, 나는 생각하지 않는다. 어쩌면 나는 다른 것을 놓치고 있을지 모른다. 사전에 도움을 주신 모든 분들께 감사드립니다. 진심으로 감사드립니다.
var anchorPoints = [];
var anchorConnectors = [];
var anchorPointAmount = 20;
var xvals = [];
var yvals = [];
const cenX = window.innerWidth/2;
const cenY = window.innerHeight/2;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
frameRate(60);
for (i = 0; i < anchorPointAmount; i++) {
xvals.push(random(0, window.innerWidth));
yvals.push(random(0, window.innerHeight));
anchorPoints[i] = new AnchorPoint(xvals[i], yvals[i]);
for (j = 0; j < anchorPointAmount; j++) {
console.log(i, j)
anchorConnectors[j * i] = new AnchorConnector(xvals[i], yvals[i], xvals[j * 2], yvals[j * 2]);
}
}
console.log(xvals);
console.log(yvals);
}
u r anchorConnectors에 임의의 인덱스를 할당합니다. 따라서 0 * 1 (j x i) = 0과 0 * 2 = 0입니다. 따라서 두 번째 반복에서 0 번째 인덱스 값을 덮어 씁니다. 아마 당신이 2 차원 배열 - anchorConnectors [i] [j]가 아니라 anchorConnectors [i * j]가 필요하다고 생각합니다. – user1428716