Skip to content

Instantly share code, notes, and snippets.

@8q
Last active December 11, 2016 18:29
Show Gist options
  • Save 8q/4840bfe6d04881d1cae999c11d2af2bc to your computer and use it in GitHub Desktop.
Save 8q/4840bfe6d04881d1cae999c11d2af2bc to your computer and use it in GitHub Desktop.
processingで再帰やフラクタル色々
void setup()
{
size(700,700);
translate(width/2,height/2);
circlesR(0, 0, 350, 8);
}
void draw()
{
}
void circlesR(float x, float y, float r, float n) //nは繰り返し回数
{
ellipse(x, y, r*2, r*2);
if(n<= 1) return;
float newR = r/2;
circlesR(x+newR, y, newR, n-1); //右側の円
circlesR(x-newR, y, newR, n-1); //左側の円
circlesR(x, y+newR, newR, n-1); //下
circlesR(x, y-newR, newR, n-1); //上
}
void setup()
{
size(700,700);
translate(width/2,height/2);
circlesR(0, 0, 350, 7);
}
void draw()
{
}
void circlesR(float x, float y, float r, float n) //nは繰り返し回数
{
ellipse(x, y, r*2, r*2);
if(n<= 0) return;
float newR = r/2;
float radian = radians(random(360.0));
circlesR(x+newR*cos(radian), y+newR*sin(radian), newR, n-1);
circlesR(x-newR*cos(radian), y-newR*sin(radian), newR, n-1);
}
void setup()
{
colorMode(HSB, 360,100,100);
noFill();
strokeWeight(5);
background(0);
size(800,600);
translate(0, height);
float a = 300;
float b = 400;
stroke(random(15)%16*24, 100, 100);
ellipse(2*sqrt(a*b), -b, b*2, b*2);
circlesR(0, a, a, b, 20);
//save("~/Desktop/recursion" + random(1000) + ".png");
}
void draw()
{
}
void circlesR(float x, float y, float a, float b, float n)
{
stroke(random(15)%16*24, 100, 100);
ellipse(x, -y, a*2, a*2);
if(n <= 0) return;
float r = (a*b) / pow((sqrt(a) + sqrt(b)), 2);
float newX = x + 2*sqrt(a*r);
circlesR(newX, r, r, b, n-1);
}
void setup()
{
size(700, 700);
translate(width/2, height/2);
rect(-250, -200, 500, 400);
linesR(-250, -200, 500, 400, 10);
}
void draw()
{
}
void linesR(float x, float y, float a,float b, float n)
{
line(x, y, x+a, y);
line(x, y, x, y+b);
line(x+a, y, x+a, y+b);
line(x, y+b, x+a, y+b);
if(n <= 0) return;
if(a >= b)
{
float r = random(a);
line(x + r, y, x + r, y + b);
linesR(x, y, r, b, n-1);
linesR(x + r, y, a - r ,b, n-1);
}
else
{
float r = random(b);
line(x, y + r, x + a, y + r);
linesR(x, y, a, r, n-1);
linesR(x, y + r, a, b - r, n-1);
}
}
void setup(){
size(700,700);
colorMode(RGB, 255);
background(30,30,30);
stroke(160, 240, 238);
translate(width/2, height/2);
kochSnow(-300, 300*sqrt(3)/3, 600/3, 0, 5);
kochSnow(300, 300*sqrt(3)/3, 600/3, 240, 5);
kochSnow(0, -300*sqrt(3)*2/3, 600/3, 120, 5);
//translate(0, height/2);
//kochSnow(700, 100, 1000/3, 180, 6);
}
void draw(){
}
void kochSnow(float x, float y, float r, int degree, int n) //左はじの座標,基本形の1辺の長さ,基本形の回転角度(60の倍数、座標軸は回転させない),繰り返し回数
{
float x2 = x + r*cos(radians(degree));
float y2 = y + r*sin(radians(degree));
float x3 = x2 + r*cos(radians(degree+60));
float y3 = y2 + r*sin(radians(degree+60));
float x4 = x3 + r*cos(radians(degree-60));
float y4 = y3 + r*sin(radians(degree-60));
float x5 = x4 + r*cos(radians(degree));
float y5 = y4 + r*sin(radians(degree));
if(n<=1)
{
line(x, y, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x4, y4);
line(x4, y4, x5, y5);
}
else
{
kochSnow(x, y, r/3, degree, n-1);
kochSnow(x2, y2, r/3, degree+60, n-1);
kochSnow(x3, y3, r/3, degree-60, n-1);
kochSnow(x4, y4, r/3, degree, n-1);
}
}
void setup(){
size(700,700);
colorMode(RGB, 255);
background(0);
stroke(75,150,0);
branch(0, 350, 700, 3, 0, 5);
}
void draw(){
}
void branch(float x, float y, float r, float weight, int degree, int n){
strokeWeight(weight);
float dx = r*cos(radians(degree));
float dy = r*sin(radians(degree));
line(x, y, x+dx, y+dy);
float dx2 = (r/3)*cos(radians(degree+45));
float dy2 = (r/3)*sin(radians(degree+45));
line(x + dx/7, y + dy/7, x + dx/7 + dx2,y + dy/7 + dy2);
line(x + dx*3/7, y + dy*3/7, x + dx*3/7 + dx2,y + dy*3/7 + dy2);
line(x + dx*5/7, y + dy*5/7, x + dx*5/7 + dx2,y + dy*5/7 + dy2);
float dx3 = (r/3)*cos(radians(degree-45));
float dy3 = (r/3)*sin(radians(degree-45));
line(x + dx*2/7, y + dy*2/7, x + dx*2/7 + dx3,y + dy*2/7 + dy3);
line(x + dx*4/7, y + dy*4/7, x + dx*4/7 + dx3,y + dy*4/7 + dy3);
line(x + dx*6/7, y + dy*6/7, x + dx*6/7 + dx3,y + dy*6/7 + dy3);
if(n <=1) return;
branch(x + dx/7, y + dy/7, r/3, weight*0.7, degree+45, n-1);
branch(x + dx*3/7, y + dy*3/7, r/3, weight*0.7, degree+45, n-1);
branch(x + dx*5/7, y + dy*5/7, r/3, weight*0.7, degree+45, n-1);
branch(x + dx*2/7, y + dy*2/7, r/3, weight*0.7, degree-45, n-1);
branch(x + dx*4/7, y + dy*4/7, r/3, weight*0.7, degree-45, n-1);
branch(x + dx*6/7, y + dy*6/7, r/3, weight*0.7, degree-45, n-1);
}
void setup(){
size(700,700);
background(255,255,255);
sidaplot(100000);
}
void draw(){
}
void sidaplot(long n){
translate(width/2, 0);
noStroke();
fill(0,127,0);
float scale = height /10.0;
float x = 0.0, y = 0.0f;
for(long i = 0; i < n; i++){
double d = random(100.0);
float x_ = 0.0, y_ = 0.0;
if(d < 1){
x_ = 0;
y_ = 0.16*y;
} else if(1 <= d && d < 8){
x_ = 0.2 * x-0.26 * y;
y_ = 0.23 * x+ 0.22 * y + 1.6;
} else if(8 <= d && d < 15){
x_ = -0.15 * x + 0.28 * y;
y_ = 0.26 * x + 0.24 * y +0.44;
} else {
x_ = 0.85 * x + 0.04 * y;
y_ = -0.04 * x + 0.85 * y + 1.6;
}
x = x_;
y = y_;
rect(scale * x, scale * y, 1,1);
}
}
void setup(){
size(800,600);
}
void draw(){
if (mousePressed) {
drawTree(mouseX, mouseY, 30, 90, 3, 6);
}
}
void drawTree(float x, float y, float l, float deg, float thickness, int n){ //x,y,length,degree,thickness,n
noFill();
stroke(91,79,63);
strokeWeight(thickness);
line(x, y, x + l*cos(radians(deg)), y - l*sin(radians(deg)));
float r = random(40);
x += l*cos(radians(deg));
y -= l*sin(radians(deg));
noStroke();
fill(252,238,235);
ellipse(x,y,4,4);
if(n <= 1) return;
drawTree(x, y, l , deg + r, thickness-0.5, n-1);
drawTree(x, y, l , deg - r, thickness-0.5, n-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment