Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1.  
  2. /*
  3. How to use :
  4. Create the object, then init it in setup()
  5. Call its render() method at the end of draw()
  6. Call its method build() to get a new pattern
  7. */
  8.  
  9. final int F_CIRCLE = 0;
  10. final int F_PAUSE = 1;
  11. final int F_RING = 2;
  12. final int F_PATTERN_DIAL = 3;
  13. final int F_STRIPES = 4;
  14.  
  15. class Filter {
  16.  
  17. PShape shape;
  18. int mode = floor(random(0, 5));
  19.  
  20. Filter() {
  21. this.build();
  22. }
  23.  
  24. void build() {
  25. shape = createShape();
  26.  
  27. shape.beginShape();
  28. shape.fill(0);
  29. shape.noStroke();
  30. this.vertScreen();
  31.  
  32. // Style for the contours
  33. shape.fill(0);
  34. shape.stroke(255);
  35. shape.strokeWeight(3.0);
  36.  
  37. if ( mode == F_CIRCLE ) {
  38. shape.fill(255);
  39. shape.stroke(0);
  40. shape.strokeWeight(3.0);
  41.  
  42. this.vertCircle(new PVector(width/2, height/2), 430);
  43. }
  44.  
  45. if ( mode == F_PAUSE ) {
  46. this.makePause();
  47. }
  48.  
  49. if ( mode == F_RING ) {
  50. this.makeRing();
  51. }
  52.  
  53. if ( mode == F_PATTERN_DIAL ) {
  54. this.makeDial();
  55. }
  56.  
  57. if ( mode == F_STRIPES ) {
  58. this.makeStripes();
  59. }
  60.  
  61. shape.endShape(CLOSE);
  62. }
  63.  
  64. void render() {
  65. shape(shape);
  66. }
  67.  
  68. void vertScreen() {
  69. shape.vertex(0, 0);
  70. shape.vertex(width, 0);
  71. shape.vertex(width, height);
  72. shape.vertex(0, height);
  73. }
  74.  
  75. void vertCircle(PVector pos, float radius) {
  76. shape.beginContour();
  77. float step = TWO_PI / 150;
  78.  
  79. for ( float a = TWO_PI; a > 0.0; a -= step ) {
  80. shape.vertex(
  81. pos.x + cos(a) * radius,
  82. pos.y + sin(a) * radius);
  83. }
  84. shape.endContour();
  85. }
  86.  
  87. void vertRect(PVector pos, PVector dim) {
  88. shape.beginContour();
  89. dim.mult(0.5);
  90. shape.vertex(pos.x - dim.x, pos.y - dim.y);
  91. shape.vertex(pos.x - dim.x, pos.y + dim.y);
  92. shape.vertex(pos.x + dim.x, pos.y + dim.y);
  93. shape.vertex(pos.x + dim.x, pos.y - dim.y);
  94. shape.endContour();
  95. }
  96.  
  97. void makeDial() {
  98. float size = 210 + random(-100, 100);
  99. float spacing = 250 + random(-100, 100);
  100. PVector origin = new PVector(width/2, height/2);
  101. int[][] placer = {
  102. {-1, -1},
  103. {0, -1},
  104. {1, -1},
  105. {-1, 0},
  106. {1, 0},
  107. {-1, 1},
  108. {0, 1},
  109. {1, 1},
  110. };
  111.  
  112. for ( int i = 0; i < 8; ++i ) {
  113. PVector pos = new PVector(placer[i][0]*spacing, placer[i][1]*spacing);
  114. pos.add(origin);
  115. if ( random(1) > 0.3 ) {
  116. if ( random(1) > 0.5 ) {
  117. this.vertRect(pos, new PVector(size, size));
  118. } else {
  119. this.vertCircle(pos, size/2);
  120. }
  121. }
  122. }
  123. }
  124.  
  125. void makePause() {
  126. float spacing = random(width * 0.1, width * 0.3);
  127. float w = random(100, 300);
  128. float h = random(400, 700);
  129. this.vertRect(new PVector(width*0.5-spacing, height/2), new PVector(w, h));
  130. this.vertRect(new PVector(width*0.5+spacing, height/2), new PVector(w, h));
  131. }
  132.  
  133. void makeRing() {
  134. float outerRadius = random(300, 450);
  135. float innerRadius = outerRadius - random(50, 290);
  136. this.vertCircle(new PVector(width/2, height/2), outerRadius);
  137. this.vertCircle(new PVector(width/2, height/2), innerRadius);
  138. }
  139.  
  140. void makeStripes() {
  141. int count = floor(random(1, 6));
  142. float w = width * 0.7;
  143. float h = random(20, 100);
  144. float spacing = h + random(10, 50);
  145.  
  146. PVector pos = new PVector(width/2, height/2);
  147. pos.y -= (spacing*count)/2;
  148.  
  149. for ( int i = 0; i < count; ++i ) {
  150. this.vertRect(pos, new PVector(w, h));
  151. pos.y += spacing;
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement