python - 仅使用线函数绘制分形树

标签 python recursion drawing processing fractals

我正在尝试找出如何在不使用任何几何变换的情况下绘制分形树。

我已经想出了这段代码,但它无法正确旋转以用于进一步的分支。

void setup() {
  size(1000,1000);
  background(50);
  stroke(255);
}

void draw() {
  branch(100, width/2, height, 10, PI/2);
}

float angle = PI/6;
void branch(float size, float cx, float cy, int noi, float alpha) {

  if(noi != 0) { //Number of increments - noi
    float rx = cx + (cos(alpha) * size);
    float lx = cx - (cos(alpha) * size);
    float y = cy - (sin(alpha) * size);

    line(cx, cy, rx, y);
    line(cx, cy, lx, y);

    branch(size/2, rx, y, noi-1, alpha - angle);
    branch(size/2, lx, y, noi-1, alpha - angle);

  } else {
    return;

  }

}

我使用基本的三角转换来找到下一个右点和左点。我认为我没有使用正确的 alpha 值进行转换。

Right now i get this

Trying to draw this

最佳答案

我解决了这个问题。

void setup() {
  size(1000,1000);
  background(50);
  stroke(255);
}

void draw() {
  branch(100, width/2, height/2, 10, PI/2);
}

float angle = PI/6;
void branch(float size, float cx, float cy, int noi, float alpha) {

  if(noi != 0) { //Number of increments - noi
    float rx = cx + (cos(alpha) * size);
    //float lx = cx - (cos(alpha) * size);
    float y = cy - (sin(alpha) * size);

    line(cx, cy, rx, y);
    //line(cx, cy, rx, y);

    branch(size*0.66, rx, y, noi-1, alpha - angle);
    branch(size*0.66, rx, y, noi-1, alpha + angle);

  } else {
    return;

  }

}

关于python - 仅使用线函数绘制分形树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56172369/

相关文章:

python - 如何使用 Visual Studio 的 Python 工具 (PTVS) 调试在本地 lighttpd 服务器上运行的 Python

python - scrapy重定向到127.0.0.1

javascript - 如何对嵌套数组中的所有元素求和?

java - void 方法递归错误

ios - drawRect - 绘制星形

javascript - 是否有用于使用 Canvas 的类似 Paint 的应用程序的 Javascript 库?

python - 当值为0时如何在轴上显示条形类别

javascript - 如何在 Spyder IDE 中使用 Javascript?

java - 寻路 - StackOverflowError

c++ - Direct2D 中的双缓冲?