Advertisement
xeromino

blob

Sep 4th, 2015
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. int numAnchors = 7; // numbers of anchor points
  2. int n=5; // number of subdivisions
  3. int frames = 100;
  4. int exp = int(pow(2, n));
  5. int dir;
  6. float theta, fc;
  7. boolean save;
  8. Curve[] myCurves = new Curve[numAnchors];
  9. PVector[] origAnchors = new PVector[numAnchors];
  10. PVector[] myAnchors = new PVector[numAnchors];
  11.  
  12. void setup() {
  13. size(750, 500, P2D);
  14. smooth(8);
  15. background(20);
  16. generateAnchors();
  17. updateAnchors();
  18. generateBlob();
  19. }
  20.  
  21. void draw() {
  22. updateAnchors();
  23. generateBlob();
  24. if (save) {
  25. if (frameCount<=fc+frames) saveFrame("image-###.gif");
  26. }
  27. theta += TWO_PI/frames;
  28. }
  29.  
  30. void generateAnchors() {
  31. for (int i=0; i<numAnchors; i++) {
  32. float r = 25;
  33. float d = 200+i*random(-r, r);
  34. float vx = width/2 + cos(TWO_PI/numAnchors*i)*d;
  35. float vy = height/2 + sin(TWO_PI/numAnchors*i)*d;
  36. origAnchors[i] = new PVector(vx, vy);
  37. myAnchors[i] = origAnchors[i].get();
  38. }
  39. }
  40.  
  41. void updateAnchors() {
  42. for (int i=0; i<numAnchors; i++) {
  43. dir=i%2==0?1:-1;
  44. myAnchors[i].x = origAnchors[i].x + cos(theta)*(i+1)*10*dir;
  45. myAnchors[i].y = origAnchors[i].y + sin(theta)*(i+1)*10*dir;
  46. }
  47. }
  48.  
  49. void generateBlob() {
  50. background(0);
  51. fill(255);
  52. beginShape();
  53. for (int i=0; i<numAnchors; i++) {
  54. myCurves[i] = new Curve(myAnchors[(i+1)%numAnchors], myAnchors[(i+2)%numAnchors], myAnchors[i%numAnchors]);
  55. myCurves[i].generateRatios();
  56. myCurves[i].createVertices();
  57. }
  58. endShape(CLOSE);
  59. }
  60.  
  61. void mouseReleased() {
  62. generateAnchors();
  63. generateBlob();
  64. }
  65.  
  66. void keyPressed() {
  67. fc = frameCount;
  68. save = true;
  69. }
  70. void generateRatios() {
  71. float denom = pow(4, n);
  72. for (int i=0; i<(exp/2); i++) {
  73. ratioB[i] = (((i*(i+1))/2.0))/denom;
  74. ratioC[i] = ((((exp-i-1)*(exp-i))/2.0))/denom;
  75. ratioA[i] = 1-ratioB[i]-ratioC[i];
  76. }
  77. for (int i=exp/2; i<exp; i++) {
  78. ratioB[i] = ((i*(i+1))/2)/denom;
  79. ratioC[i] = (((exp-i-1)*(exp-i))/2)/denom;
  80. ratioA[i] = 1-ratioB[i]-ratioC[i];
  81. }
  82. }
  83.  
  84. void createVertices() {
  85. for (int i=0; i<exp; i++) {
  86. float px = A.x*ratioA[i]+B.x*ratioB[i]+C.x*ratioC[i];
  87. float py = A.y*ratioA[i]+B.y*ratioB[i]+C.y*ratioC[i];
  88. vertex(px,py);
  89. }
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement