Advertisement
xeromino

circles

Dec 30th, 2016
271
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ArrayList<Circle> circles = new ArrayList<Circle>();
  2. int edge = 0, rmin = 10, rmax = 50;
  3. float r = 0, max = 100;
  4.  
  5. void setup() {
  6.   size(1080, 720);
  7.   circles.add(new Circle(new PVector(width/2, height/2), 20, 0, new PVector(0, 0)));
  8. }
  9.  
  10. void draw() {
  11.   background(34);
  12.   paintCircles();
  13.   if (circles.size()<max) addCircle();
  14. }
  15.  
  16. void paintCircles() {
  17.   for (int i=0; i<circles.size(); i++) {
  18.     Circle pc;
  19.     Circle nc;
  20.     Circle c = circles.get(i);
  21.     noFill();
  22.     //stroke(255, 200);
  23.     //fill(255, 100, 200, 150);
  24.     //noStroke();
  25.     strokeWeight(1);
  26.     //ellipse(c.center.x, c.center.y, c.radius*2, c.radius*2);
  27.     if (i>0) {
  28.       pc = circles.get(i-1);
  29.       //ine(pc.center.x, pc.center.y, c.center.x, c.center.y);
  30.       //ellipse(c.in.x, c.in.y, 10, 10);
  31.     }
  32.     if (i<circles.size()-1) {
  33.       nc = circles.get(i+1);
  34.       float angle_in = atan2(c.center.y-c.in.y, c.center.x-c.in.x);
  35.       float angle_out = atan2(c.center.y-nc.in.y, c.center.x-nc.in.x);
  36.       float ai = map(angle_in, -PI, PI, 0, TWO_PI);
  37.       float ao = map(angle_out, -PI, PI, 0, TWO_PI);
  38.       //if (ao < ai) ao += TWO_PI;
  39.       strokeCap(SQUARE);
  40.       //strokeWeight(map(c.radius, 10,50,2,15));
  41.       strokeWeight(2);
  42.       stroke(238);
  43.       noFill();
  44.       //fill(150,0,255,60);
  45.       int num = (int) map(c.radius, rmin, rmax, 1, 5);
  46.       for (int j=0; j<num; j++) {
  47.         float rad = c.radius*map(j, 0, num, 2, 0.5);
  48.         if (i%2==0) {
  49.           if (ao < ai) ao += TWO_PI;
  50.           arc(c.center.x, c.center.y, rad, rad, ai, ao);
  51.         } else {
  52.           if (ai < ao) ai += TWO_PI;
  53.           arc(c.center.x, c.center.y, c.radius*2, c.radius*2, ao, ai);
  54.         }
  55.       }
  56.     }
  57.   }
  58. }
  59.  
  60. void addCircle() {
  61.   Circle c = circles.get(circles.size()-1);
  62.   r = random(TWO_PI);
  63.   float angle = random(-r, r);
  64.   float radius = random(rmin, rmax);
  65.   float x = c.center.x + cos(angle)*(radius+c.radius);
  66.   float y = c.center.y + sin(angle)*(radius+c.radius);
  67.   PVector v = new PVector(x, y);
  68.   x = c.center.x + cos(angle)*c.radius;
  69.   y = c.center.y + sin(angle)*c.radius;
  70.   PVector in = new PVector(x, y);
  71.   Circle nc = new Circle(v, radius, angle, in);
  72.   if (checkCircles(nc)) {
  73.     circles.add(nc);
  74.   }
  75. }
  76.  
  77. Boolean checkCircles(Circle c) {
  78.   Boolean outside = true;
  79.   for (int i=0; i<circles.size(); i++) {
  80.     Circle cc = circles.get(i);
  81.     if (c.center.dist(cc.center) < (c.radius + cc.radius)) {
  82.       outside = false;
  83.       return outside;
  84.     }
  85.   }
  86.   return outside;
  87. }
  88.  
  89. void mouseReleased() {
  90.   addCircle();
  91.   Circle c = circles.get(circles.size()-1);
  92.   println(atan2(c.in.y-c.center.y, c.in.x-c.center.x ));
  93.   println(circles.size());
  94. }
  95.  
  96. void keyPressed() {
  97.   if (key == 'n') {
  98.     r = 0;
  99.     circles = new ArrayList<Circle>();
  100.     circles.add(new Circle(new PVector(width/2, height/2), 20, 0, new PVector(0, 0)));
  101.   }
  102.   if (key == 's') save(random(9999)+".png");
  103. }
  104.  
  105. class Circle {
  106.  
  107.   PVector center;
  108.   PVector in, out;
  109.   float radius;
  110.   float angle;
  111.  
  112.   Circle(PVector _center, float _radius, float _angle, PVector _in) {
  113.     center = _center;
  114.     radius = _radius;
  115.     angle = _angle;
  116.     in = _in;
  117.   }  
  118. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement