2025年11月9日 星期日

week 11. drawing mandala


 
float radius;
int segment = 12;
float centerX;
float centerY;

void setup(){
 size(600,600);
 background(0);
 stroke(255);
 centerX = height/2;
 centerY = width/2;
 noLoop();
}

void draw(){
  background(0);
  //radius = dist(centerX, centerY, mouseX, mouseY);
int pattern;
radius = 30.0;
noFill();
for (int i= 0; i< 8; i++){
radius=radius+25.0;
segment=segment+ 6;
pattern = (int) random(4);
    spiral(centerX,centerY,radius,segment, pattern);
}
  
}
void spiral(float x_c,float y_c, float r, int seg, int pattern){
    for(int i =0;i<seg;i++){
  float theta = i*2*PI/seg;
     float x = x_c+cos(theta)*r;  //算出x的位置
     float y = y_c+sin(theta)*r;  //算出y的位置
//      point(x,y);  // 畫出點,也能用其他形狀
    ellipse(x_c, y_c, 2*r, 2*r);
    if (pattern == 0) ellipse(x_c, y_c, 2*r+8, 2*r+8);
if (pattern == 1) drawPattern1(x,y,theta);
if (pattern == 2) drawPattern2(x,y,theta);
if (pattern == 3) drawPattern3(x,y,theta);
//pattern
    }
}
void drawPattern1(float x, float y, float theta){
pushMatrix();
      translate(x,y);
      rotate(theta);
point(5, 0);
line(0,10, 20,0);
line(0,-10, 20, 0);
     popMatrix();
}
void drawPattern2(float x, float y, float theta){
pushMatrix();
      translate(x,y);
      rotate(theta);
ellipse(5, 0, 5, 5);
arc(0, 0, 30, 20, PI*1.5, PI*2.5);
   arc(0, 0, 25, 15, PI*1.5, PI*2.5);
     popMatrix();
}
void drawPattern3(float x, float y, float theta){
pushMatrix();
      translate(x,y);
      rotate(theta);
   beginShape();
       curveVertex(0,  8);
       curveVertex(0,  8);
       curveVertex(16,  6);
       curveVertex(16,  -6);
       curveVertex(0, -8);
       curveVertex(0, -8);
      endShape();
beginShape();
       curveVertex(0,  10);
       curveVertex(0,  10);
       curveVertex(20,  8);
       curveVertex(20,  -8);
       curveVertex(0, -10);
       curveVertex(0, -10);
      endShape();
//    bezier(0,-10, 15, -9, 0, 0, 18,0);
//    bezier(0,10, 15, 9,0, 0, 18,0);

     popMatrix();
}

 
 

Basic sample 1:


float resolution;
float radius = 100;
float circleX;
float circleY;
float offset;

void setup() {
  size(700, 700);
  circleX = 0;
  circleY = 0;
}

void draw() {
  fill(0,0,0,25);
  rect(0,0,width,height);
  translate(width/2, height/2);
  fill(255);
  noStroke();
  offset++;

  resolution = map(sin(offset*0.005), -1, 1, 2, 20);
 // resolution = 20;



  for (int i = 0; i < resolution; i++) {
    float angle = map(i, 0, resolution, 0, TWO_PI);
//    float waveOffset = sin(angle*circles) * 200;
 

 
   float circleOffsetX = cos(angle) * (radius); //xposition
   float circleOffsetY = sin(angle) * (radius); //yposition
  //  float circleOffsetX = cos(angle) * (radius + waveOffset); //xposition
  //  float circleOffsetY = sin(angle) * (radius + waveOffset); //yposition
 

    float x = circleX + circleOffsetX;
    float y = circleY + circleOffsetY;
 
    ellipse(x, y, 10, 10);
 

  }
}

Sample 2:

float resolution;
float radius = 100;
float circleX;
float circleY;
float circles;
float circleW;
float circleH;
float offset;

void setup() {
  size(700, 700);
  circleX = 0;
  circleY = 0;
}

void draw() {
  fill(0,0,0,25);
  rect(0,0,width,height);
  translate(width/2, height/2);
  fill(255);
  noStroke();
  offset++;

  resolution = map(sin(offset*0.00025), -1, 1, 11, 13);
  //resolution = 50;
  circles = 400;

  circleW = map(sin(offset*0.05), -1, 1, 1, 8);
  circleH = map(sin(offset*0.05), -1, 1, 1, 8);

  for (int i = 0; i < resolution; i++) {

    float scale = 200;

    float waveAngle = map(i, 0, resolution, 0, TWO_PI * circles);
    float waveOffset = sin(waveAngle) * scale;  //scale the -1 to 1 up

    float angle = map(i, 0, resolution, 0, TWO_PI * 156); //multiply for weirdness

    float circleOffsetX = cos(angle) * (radius + waveOffset); //xposition
    float circleOffsetY = sin(angle) * (radius + waveOffset); //yposition

    float x = circleX + circleOffsetX;
    float y = circleY + circleOffsetY;
 
    ellipse(x, y, circleW, circleH);

  }

}

example 3:


float resolution;
float radius = 100;
float circleX;
float circleY;
float circles;
float offset;

void setup() {
  size(700, 700);
  circleX = 0;
  circleY = 0;
}

void draw() {
  fill(0,0,0,25);
  rect(0,0,width,height);
  translate(width/2, height/2);
  fill(255, 255, 0);
  noStroke();
  offset++;

  resolution = map(cos(offset*0.0002), -1, 1, 2, 200);
  circles = 100;


beginShape();
noFill();
  for (int i = 0; i < resolution; i++) {

    float scale = 400;

    float waveAngle = map(i, 0, resolution, 0, TWO_PI * circles);
    float waveOffset = sin(waveAngle) * scale;  //scale the -1 to 1 up

    float angle = map(i, 0, resolution, 0, TWO_PI * 156); //multiply for weirdness
 

    float circleOffsetX = cos(angle) * (radius + waveOffset); //xposition
    float circleOffsetY = sin(angle) * (radius + waveOffset); //yposition

    float x = circleX + circleOffsetX;
    float y = circleY + circleOffsetY;
 
    stroke(255);
    //line(x, y, 0, 0);
    vertex(x,y);
 

  }
  endShape(CLOSE);

}

reference:
search "mandala" on
https://www.openprocessing.org/

also 

沒有留言:

張貼留言