gpt4 book ai didi

for-loop - 使用角度处理

转载 作者:行者123 更新时间:2023-12-05 05:31:19 26 4
gpt4 key购买 nike

您好:我正在尝试在我的代码中使用角度,但不清楚如何将它们用作变量。我想制作这个看起来像树根的草图,需要最后一 block 来完成它。

错误发生在绘图函数中的这个for循环中。我想要在每一帧上改变角度,以及提供尺寸的颜色。这是 for 循环:

 for (int x=0; x<width; x++) {
angle=0;
pins (int p=>angle+=atan2(p.y-y, p.x-x));
color=map(sin(angle), -1, 1, 0, 256);
stroke(color);
line(int x, int y, int x, int y+color);
}

下面是我的完整代码。

float[] pins= new float[0];
float pins_num= 150;
float y, x;

void setup (){
size(1920, 1080);
float y=0;
for (int i=0; i<pins_num; i++ ) {
pins[i]=createVector(random(width), random(height));
}
}

void draw (){
if (y<height) {
for (int x=0; x<width; x++) {
angle=0;
pins (int p=>angle+=atan2(p.y-y, p.x-x));
color=map(sin(angle), -1, 1, 0, 256);
stroke(color);
line(int x, int y, int x, int y+color);
}
y++
}
}

最佳答案

您似乎混合了 p5.js (createVector , int p=>angle...) 语法和 Processing 语法。如果您要将 p5.js 草图移植到 Processing,您还应该在问题中包含原始 p5.js 代码。

一些突出的事情:

  • float[] pins= new float[0]; 看起来像一个存储单个浮点值的静态数组,但是 pins[i]=createVector(random(width), random( height)); 表示它应该是一个 PVector 数组,例如:PVector[] pins= new PVector[pins_num];
  • float pins_num= 150;pins 之后声明/初始化:我怀疑其目的是将其用作 pins 数组的大小它应该被添加到 pins 数组之前。
  • y,x 在顶部被声明为 float ,然而,在 draw(x++, y++) 中作为整数递增。
  • pins (int p=>angle+=atan2(p.y-y, p.x-x)); 看起来真的很困惑。也许意图是访问每个引脚(可能是 js 中的 pins.forEach)并随着每个随机 PVector 之间的旋转增加 anglesetup() 和当前 x,y 位置(从上到下)分配?类似于:for(PVector p : pins) angle += atan2(p.y-y, p.x-x); 在 Processing (Java) 中
  • color=map(sin(angle), -1, 1, 0, 256); 这看起来像 p5.js,在 Processing 中你需要同时使用变量类型和名称 (不仅仅是类型)(还有次要细节,颜色的范围是 0 到 255,而不是 0 到 256(因为总共有 256 个值,从 0 开始计数,而不是 1)。在处理中这可能看起来像:color mappedColor = color(map(sin(angle), -1.0, 1.0, 0, 255));
  • line(int x, int y, int x, int y+color); 看起来你试图添加变量类型(就像你在 Processing vs p5.js 中所做的那样),但是,你只需要在声明函数时指定类型,而不是在调用它时指定类型。这应该是 line(x, y, x, y + color);

这是一个有点疯狂的猜测,但仅从现有代码推断,您的代码的完全移植处理版本可能如下所示:

int numPins = 150;
PVector[] pins= new PVector[numPins];
int x, y;

void setup () {
size(1920, 1080);
for (int i = 0; i < numPins; i++ ) {
pins[i] = new PVector(random(width), random(height));
}
}

void draw () {
float angle = 0;
if (y < height) {
for (int x = 0; x < width; x++) {
for(PVector p : pins) angle += atan2(p.y-y, p.x-x);
color mappedColor = color(map(sin(angle), -1.0, 1.0, 0, 255));
stroke(mappedColor);
line(x, y, x, y + mappedColor);
}
y++;
}
}

关于for-loop - 使用角度处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74397347/

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