ビルドしている簡単なコードにバグがあります。コードはノードから線を伸ばします。各ノードは2つの点とその2つを結ぶ線です。各ノードは1回成長でき、成長は成長関数で処理されますループ内のすべてのベクトルがこの処理コード内で回転するのはなぜですか?
ノードクラスの成長関数の一部には、ノードの方向を回転させる条件があります(何らかの理由ですべてのノードはコードを書いたやり方で回転します。誰かが私が作った間違いなく単純なエラーを指摘できますか?
コード:
Node[] ns;
int nodeCount;
void setup() {
size(1024, 1024, P3D);
background(255);
stroke(0);
frameRate(60);
fill(0);
nodeCount = int(10000);
ns = new Node[nodeCount];
for(int i = 0; i < nodeCount; i++){
ns[i] = new Node(i, 0, 0, 0, 0, 0, new PVector(0, 0), false, false, false);
}
for(int i = 0; i < 1; i++){
ns[i] = new Node(i, random(float(width)), random(float(height)), 5, 0, random(100), new PVector(10, 10).rotate(random(-PI/2, PI/2)), true, false, true);
}
}
void draw() {
background(255);
//print("frame ");
for (Node n: ns){
if (n.Active) {
//print(n.index, " ");
n.display();
if (n.Growth){
n.grow();
}
}
if(n.Init){
n.init();
}
}
}
class Node {
int index;
float xi, yi, size, theta, noiseoff;
PVector dir;
Boolean Active, Growth, Init;
Node(int ind, float x, float y, float s, float t, float n, PVector d, Boolean A, Boolean G, Boolean I) {
index = ind;
xi = x;
yi = y;
size = s;
theta = t;
noiseoff = n;
dir = d;
Active = A;
Growth = G;
Init = I;
}
void display(){
strokeWeight(0);
ellipse(xi, yi, size, size);
ellipse(xi + dir.x, yi + dir.y, size, size);
strokeWeight(size);
line(xi, yi, xi + dir.x, yi + dir.y);
//xi = xi + dir.x;
//yi = yi + dir.y;
}
void grow(){
boolean done = false;
int i = index + 1;
PVector tempDir = new PVector(0, 0);
tempDir = dir;
while (!done & i < nodeCount & Growth) {
if (ns[i].Active == false) {
if(random(100) > 85){
noiseoff += 0.01;
theta = map(noise(noiseoff), 0, 1, -PI/12, PI/12);
tempDir.rotate(theta);
}
ns[i] = new Node(i, xi + tempDir.x, yi + tempDir.y, size, theta, noiseoff, tempDir, true, false, true);
Growth = false;
done = true;
if (i == nodeCount - 1) {
done = true;
}
}
i++;
if (i == nodeCount) {
done = true;
}
}
i++;
}
void init(){
Growth = true;
Init = false;
}
}
ノードクラス内のノードの配列をループを削除するcreateNode()メソッドを紹介する別のバージョン(まだバグがあります):
Node[] ns;
int nodeCount;
void setup() {
size(1024, 1024, P3D);
background(255);
stroke(0);
frameRate(60);
fill(0);
nodeCount = int(10000);
ns = new Node[nodeCount];
// Initialize array with blank nodes
for(int i = 0; i < nodeCount; i++){
ns[i] = new Node(i, 0, 0, 0, 0, 0, new PVector(0, 0), false, false, false);
}
// Create Root node
createNode(random(float(width)), random(float(height)), 5, 0, random(100), new PVector(2.5, 2.5).rotate(random(-PI/2, PI/2)), true, false, true);
}
void draw() {
background(255);
for (Node n: ns){
if (n.Active) {
// Draw active nodes
n.display();
// Grow nodes which haven't grown before
if (n.Growth){
n.grow();
}
}
// Initialize nodes which were grown last time step (so that they can grown in the subsequent timestep)
if(n.Init){
n.init();
}
}
}
// Wrapper function that looks for a free row in the node array to create a new node
void createNode(float x, float y, float s, float t, float n, PVector d, Boolean A, Boolean G, Boolean I){
float xi, yi, size, theta, noiseoff;
PVector dir;
Boolean Active, Growth, Init, done;
int i;
xi = x;
yi = y;
size = s;
theta = t;
noiseoff = n;
dir = d;
Active = A;
Growth = G;
Init = I;
i = 0;
done = false;
while (!done) {
if (ns[i].Active == false) {
ns[i] = new Node(i, xi + dir.x, yi + dir.y, size, theta, noiseoff, dir, true, false, true);
done = true;
if (i == nodeCount - 1) {
done = true;
}
}
i++;
if (i == nodeCount) {
done = true;
}
}
}
// Node class
class Node {
int index;
float xi, yi, size, theta, noiseoff;
PVector dir;
Boolean Active, Growth, Init;
Node(int ind, float x, float y, float s, float t, float n, PVector d, Boolean A, Boolean G, Boolean I) {
index = ind;
xi = x;
yi = y;
size = s;
theta = t;
noiseoff = n;
dir = d;
Active = A;
Growth = G;
Init = I;
}
void display(){
// Draw node as two circles and a line
strokeWeight(0);
ellipse(xi, yi, size, size);
ellipse(xi + dir.x, yi + dir.y, size, size);
strokeWeight(size);
line(xi, yi, xi + dir.x, yi + dir.y);
}
// Grow method
void grow(){
int i = index + 1;
PVector tempDir = dir;
// Small chance of angle deviation
if(random(100) > 85){
noiseoff += 0.01;
theta = map(noise(noiseoff), 0, 1, -PI/12, PI/12);
tempDir.rotate(theta);
}
// Create new node appended onto the last
createNode(xi + tempDir.x, yi + tempDir.y, size, theta, noiseoff, tempDir, true, false, true);
// Set growth to false to prevent this node growning again
Growth = false;
}
// Initialize previously grown nodes for growth
void init(){
Growth = true;
Init = false;
}
}
これらのノードの動作を正確に把握できますか?彼らはどのように接続する必要がありますか? –
確かに、ノードは大きさ(この段階では定数)と方向(この段階ではランダム)で「生まれ」ます。ノードは2つの円として表示され、その2つをベクトルの方向/ magに結ぶ線が表示されます。 ノードは一度「成長」することができます。元のノードにわずかにランダムなオフセット角度を持つ新しいノードを追加する必要があります。ノードが大きくなると、それは決してそのプロパティを変更するべきではありません – niklz