[英]Can someone help? I'm trying to make a programs that loops through numbers -1to10 and makes circles with the radius squared but it doesn't work
I want the program to loop trough each number in the for loop and create a circle with the radius of that number squared but it doesn't seem to work. 我希望程序循环遍历for循环中的每个数字,并创建一个半径为该数字平方的圆,但是它似乎不起作用。 I only creates a circle with the radius of the last number in the for loop.
我只在for循环中以最后一个数字的半径创建一个圆。
I've tried creating a separate function and return the value and putting into my draw function, but that didn't seem to work. 我尝试创建一个单独的函数并返回值并将其放入我的draw函数中,但这似乎没有用。
function setup() {
createCanvas(600, 600);
}
function draw() {
for (i = -1; i <= 10; i++) {
newI = i ** 2;
};
background(250);
circle(300, 300, newI);
//ignore the lines those are just the axis//
line(300, 0, 300, 600);
line(0, 300, 600, 300);
}
I want each new radius of each new circle to be that of the number in the for loop squared. 我希望每个新圆的每个新半径都等于for循环中平方的数字。
As mentioned by @Tim Biegeleisen - the circle() and line() should be in the loop, but another approach is to have them separate. 正如@Tim Biegeleisen提到的那样,circle()和line()应该在循环中,但是另一种方法是将它们分开。
Rather than putting the logic into the loop - have the loop iteration call a separate named frunction and pass the new i value as the argument. 与其将逻辑放入循环中,还不如让循环迭代调用一个单独的名为frunction的函数,并将新的i值作为参数传递。
EDIT: loop changed to decrement in order to reverse the order of radius. 编辑:循环更改为递减,以反转半径的顺序。
function setup() {
createCanvas(600, 600);
}
function draw() {
for (i = 10; i >= -1; i--) {
createCircle(i ** 2)
};
}
function createCircle(newI){
background(250);
circle(300, 300, newI);
line(300, 0, 300, 600);
line(0, 300, 600, 300);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.