所以我试图用彩色线条制作毯子,但我似乎无法得到下面显示的结果......
我的代码首先放置一条水平线并应用数组中的颜色(颜色正确),然后放置一条垂直线并应用数组中的颜色。
但我似乎无法得到这个结果..
这是我的代码。有人开导我吗?
float[][] vclrs = { //Array with color values for the vertical lines
{220,31,24}, //Red
{128,100,172}, //Purple
{39,52,142}, //Blue
{225,104,0}, //Beige
{77,137,11}, //Green
{83,188,188}, //Light Blue
};
float[][] hclrs = { //Array with color values for the horizontal lines
{220,31,24}, //Red
{187,167,212}, //Purple
{39,52,142}, //Blue
{244,198,149}, //Beige
{77,137,11}, //Green
{183,207,209}, //Light Blue
};
int controller = 8; //Width of the line and distance between the lines
int hlinedistance = controller; //Horizontal distance to place the next line
int vlinedistance = controller; //Vertical distance to place the next line
void setup() {
size(576,480);
background(255);
strokeWeight(controller); //Stroke of the controller
}
void draw() {
for(int i=0;i<6;i=i+1) {
stroke(vclrs[i][0], vclrs[i][1], vclrs[i][2]);
line(vlinedistance, 0, vlinedistance, displayHeight);
vlinedistance = vlinedistance + (controller + controller);
line(0, hlinedistance, displayWidth, hlinedistance);
stroke(hclrs[i][0], hclrs[i][1], hclrs[i][2]);
hlinedistance = hlinedistance + (controller + controller);
}
}
答案 0 :(得分:1)
很抱歉没有清理详细信息(现在已经很晚了,但现在更好了)我并不想以任何方式冒犯。 与每种编程语言一样,代码从上到下进行查看,因此行从上到下进行布局。
stroke(255, 0, 0); //red
line(x1, y1, x2, y2);
stroke(0, 255, 0); //green
line(x1, y1, x2, y2);
在此代码段中,首先绘制红线,然后将绿线放在红线上。所以你最终只能查看绿线。在您的示例中,您有4层线。
这4组中的每一组都有不同的颜色存储在其中。在我的解决方案中,您有不同的笔画如下:
我们也使用strokeCap(PROJECT);使线条前卫。要增加线宽,我们使用strokeWeight(width);
在绘图部分,我们现在编写这4个for循环,它们使用modulo 3 - &gt;相应地切换颜色。因此,每组有3种不同的颜色。 Modulo是可供选择的颜色数组。 所有这四个for循环都有一点不同,因为它们涉及不同的位置,我确信你可以理解那里的细节。
以下是您修改的代码以匹配您的结果。 我希望这会有所帮助:
float[][] vclrs = { //Array with color values for the vertical lines
{220,31,24}, //Red
{128,100,172}, //Purple
{39,52,142}, //Blue
};
float[][] vclrs2 =
{
{225,104,0}, //Beige
{77,137,11}, //Green
{83,188,188}, //Light Blue
};
float[][] hclrs = { //Array with color values for the horizontal lines
{220,31,24}, //Red
{187,167,212}, //Purple
{39,52,142}, //Blue
};
float[][] hclrs2 =
{
{244,198,149}, //Beige
{77,137,11}, //Green
{183,207,209}, //Light Blue
};
int controller = 30; //Width of the line and distance between the lines
void setup() {
size(576,480);
background(255);
strokeCap(PROJECT);
strokeWeight(controller / 2); //Stroke of the controller
}
void draw() {
background(255);
pushMatrix();
translate(controller / 4, controller / 4);
for(int i = 0; i < displayHeight / (3 * controller); i++) {
stroke(vclrs[i % 3][0], vclrs[i % 3][1], vclrs[i % 3][2]);
line((i * 2 * controller), 0, (i * 2 * controller), displayHeight);
}
for(int i = 0; i < displayWidth / (3 * controller); i++) {
stroke(hclrs[i % 3][0], hclrs[i % 3][1], hclrs[i % 3][2]);
line(0, (i * 2 * controller), displayWidth, (i * 2 * controller));
}
for(int i = 0; i < displayHeight / (3 * controller); i++)
{
stroke(vclrs2[i % 3][0], vclrs2[i % 3][1], vclrs2[i % 3][2]);
line((i * 2 * controller + controller), 0, (i * 2 * controller + controller), displayHeight);
}
for(int i = 0; i < displayWidth / (3 * controller); i++)
{
stroke(hclrs2[i % 3][0], hclrs2[i % 3][1], hclrs2[i % 3][2]);
line(0, (i * 2 * controller + controller), displayWidth, (i * 2 * controller + controller));
}
popMatrix();
}