Processing系列 三角分形

2015-08-086856
AI 快速预览详细 收起
本文介绍了如何使用Processing语言绘制经典的三角分形图。首先设置画布大小和背景颜色,然后定义三个顶点,并通过随机选择顶点并计算中点的方式,逐步生成分形图。代码示例详细展示了每一步的操作,适合编程初学者和STEM教育场景。

经典分形图,基本学过计算机图形学的都写过这个作业
中级难度吧

  1. void setup() {
  2.   size(300, 300);
  3.   translate(150, 175);
  4.   rotate(-HALF_PI);
  5.   background(55);
  6.   stroke(255);
  7.   Point[] points = {
  8.     new Point(cos(0)*140, sin(0)*140),
  9.     new Point(cos(TWO_PI/3)*140, sin(TWO_PI/3)*140),
  10.     new Point(cos(TWO_PI/3*2)*140, sin(TWO_PI/3*2)*140),
  11.   };
  12.   Point point = new Point(points[0]);
  13.   for (int i=0; i<10000; i++) {
  14.     int index = int(random(3));
  15.     point.x = (point.x + points[index].x) * .5;
  16.     point.y = (point.y + points[index].y) * .5;
  17.     point(point.x, point.y);
  18.   }
  19. }
  20. class Point {
  21.   float x, y;
  22.   Point(float x, float y) {
  23.     this.x = x;
  24.     this.y = y;
  25.   }
  26.   Point(Point point) {
  27.     this.x = point.x;
  28.     this.y = point.y;
  29.   }
  30. }
复制代码

授权转载自 任远媒体实验室

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -