再帰プログラム
5! = 5\times4\times3\times2\times1=120
階乗を求めるプログラム
void setup() {
print(f(5));
}
int f(int n) {
if (n<=0) {
return 1;
}
return n*f(n-1);
}
この再帰構造を参考にマンデルブロ集合を描く
void setup() {
size(512, 512);
noLoop();
double d = 2.5d/(double)width;
int xx=0,yy=0;
for (double y = -1.25; y < 1.25; y+=d) {
for (double x = -2.0; x < 0.5; x+=d) {
int c = mandel(0, 0, x, y, 255);
stroke(c);
point(xx,yy);
xx++;
}
xx=0;
yy++;
}
}
int mandel(double zr, double zi, double cr, double ci, int ite) {
double zr2 = zr*zr - zi*zi + cr;
double zi2 = 2*zr*zi + ci;
if(0==ite || zr2*zr2 + zi2*zi2>4){
return ite;
}
return mandel( zr2, zi2, cr, ci, ite-1);
}
式変形して
もう少しきれいに3Dで表示したい
z^3とかよくあるバリエーションについても