问题描述
这是一个学校作业。 我是处理软件的新手,我想创建一个与下图(中间)完全一样的彩虹填充窗口。
左边的程序是我现在拥有的程序。 中间的程序就是我想要的样子。 右边是我正在使用的代码。我会复制粘贴到这里。
void setup() {
size(255,255);
}
void draw() {
nostroke();
colorMode(RGB,255,255);
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
stroke(j,i,128);
point(i,j);
}
}
}
对代码的任何帮助、建议和调整将不胜感激。提前致谢。
解决方法
你会受益于一些伪代码。永远不要低估伪代码的力量。
在这张图片中,你需要做的一切都写得很简单:
因为我们在 RGB 中工作,并且图像告诉您如何处理红色、绿色和蓝色,所以您已经是金色的,但为了使事情更透明,我们将稍微更改代码。让我们暂时忘记循环。这是图片告诉您的操作:
R -> vertical slider,the closer to the bottom the more red you have
G -> horizontal slider,left is less and right is more
B -> vertical slider,the opposite to the red slider
现在,知道您的值在 [0-255] 范围内,并且您的图像也是一个 256 像素宽的正方形,您只需要使用循环的索引来获取您的 RGB 值:
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
int r = j; // up == more red
int g = i; // right == more green
int b = 255 - j; // down == less blue
stroke(color(r,g,b));
point(i,j);
}
}
另外,只是为了踢球,因为这是一个静态图像而不是动画,您可以将此代码放在 setup()
方法中,它会得到相同的结果:
void setup() {
size(255,255);
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
int r = j; // up == more red
int g = i; // right == more green
int b = 255 - j; // down == less blue
stroke(color(r,b));
point(i,j);
}
}
}
void draw() {} // you still need this method even if it's empty
结果如下:
玩得开心!