Guest User

Untitled

a guest
Nov 17th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. /* -*- mode: java; indent-tabs-mode; nil -*- */
  2.  
  3. int canvasWidth = 500;
  4. int canvasHeight = 500;
  5.  
  6. ControlPoint[] controlPoints;
  7. CheckBox chkShowControlPoints;
  8. Setaria setaria;
  9.  
  10. void setup() {
  11. frameRate(16);
  12. size(canvasWidth, canvasHeight);
  13.  
  14. controlPoints = new ControlPoint[4];
  15. controlPoints[0] = new ControlPoint(400, 520, 32);
  16. controlPoints[1] = new ControlPoint(480, 400, 32);
  17. controlPoints[2] = new ControlPoint(200, 64, 32);
  18. controlPoints[3] = new ControlPoint(64, 64, 32);
  19. for (int i = 0; i < controlPoints.length; i++) {
  20. controlPoints[i].setRange(controlPoints[i].x - 100,
  21. 50,
  22. controlPoints[i].x + 100,
  23. 550);
  24. }
  25.  
  26. chkShowControlPoints = new CheckBox(8, 8, 32, "Show Control Points", true);
  27.  
  28. setaria = new Setaria(controlPoints);
  29.  
  30. update();
  31. }
  32.  
  33. void draw() {
  34. update();
  35.  
  36. background(#193608);
  37.  
  38. int currentTimeSec = millis() / 1000;
  39.  
  40. strokeWeight(3);
  41. stroke(255);
  42. noFill();
  43. if (chkShowControlPoints.value) {
  44. for (int i = 0; i < controlPoints.length; i++) {
  45. controlPoints[i].draw();
  46. }
  47. }
  48.  
  49. chkShowControlPoints.draw();
  50.  
  51. setaria.draw();
  52. }
  53.  
  54. void update() {
  55. int mx = mouseX;
  56. int my = mouseY;
  57. for (int i = 0; i < controlPoints.length; i++) {
  58. controlPoints[i].track(mx, my);
  59. }
  60.  
  61. setaria.update();
  62. }
  63.  
  64. void mousePressed() {
  65. for (int i = 0; i < controlPoints.length; i++) {
  66. controlPoints[i].mousePressed();
  67. }
  68. chkShowControlPoints.mousePressed();
  69. }
  70.  
  71. void mouseReleased() {
  72. for (int i = 0; i < controlPoints.length; i++) {
  73. controlPoints[i].mouseReleased();
  74. }
  75. }
  76.  
  77. interface Drawable {
  78. void draw();
  79. }
  80.  
  81. class Point implements Drawable {
  82. int x;
  83. int y;
  84. Point(int x, int y) {
  85. moveTo(x, y);
  86. }
  87. void moveTo(int x, int y) {
  88. this.x = x;
  89. this.y = y;
  90. }
  91. void add(int dx, int dy) {
  92. this.x += dx;
  93. this.y += dy;
  94. }
  95. void add(Point pt) {
  96. add(pt.x, pt.y);
  97. }
  98. void scalar(float f) {
  99. this.x *= f;
  100. this.y *= f;
  101. }
  102. void draw() {
  103. point(this.x, this.y);
  104. }
  105. }
  106.  
  107. class ControlPoint implements Drawable {
  108. int left;
  109. int top;
  110. int right;
  111. int bottom;
  112. int x;
  113. int y;
  114. int radius;
  115. boolean dragging;
  116. ControlPoint(int x, int y, int r) {
  117. setRange(x, y, x, y);
  118. moveTo(x, y);
  119. this.radius = r;
  120. this.dragging = false;
  121. }
  122. void setRange(int left, int top, int right, int bottom) {
  123. this.left = left;
  124. this.top = top;
  125. this.right = right;
  126. this.bottom = bottom;
  127. moveTo(this.x, this.y);
  128. }
  129. boolean isBusy() {
  130. return (this.isMouseOver() || this.dragging);
  131. }
  132. boolean isMouseOver() {
  133. int mx = mouseX;
  134. int my = mouseY;
  135. int dx = this.x - mx;
  136. int dy = this.y - my;
  137. return (dx * dx + dy * dy <= this.radius * this.radius);
  138. }
  139. void mousePressed() {
  140. this.dragging = isMouseOver();
  141. }
  142. void mouseReleased() {
  143. this.dragging = false;
  144. }
  145. void moveTo(int x, int y) {
  146. if (x < this.left) {
  147. x = this.left;
  148. } else if (x >= this.right) {
  149. x = this.right;
  150. }
  151. if (y < this.top) {
  152. y = this.top;
  153. } else if (y >= this.bottom) {
  154. y = this.bottom;
  155. }
  156. this.x = x;
  157. this.y = y;
  158. }
  159. void track(int mx, int my) {
  160. if (this.dragging) {
  161. moveTo(mx, my);
  162. }
  163. }
  164. void draw() {
  165. noFill();
  166. int alpha = abs((millis() % 2000) - 1000) / 1000 * 255;
  167. stroke(255, alpha);
  168. ellipse(this.x, this.y, this.radius, this.radius);
  169. }
  170. }
  171.  
  172. class Setaria implements Drawable {
  173. Point[] stemPoints;
  174. ControlPoint[] controlPoints;
  175. Setaria(ControlPoint[] controlPoints) {
  176. this.controlPoints = controlPoints;
  177. this.stemPoints = new Point[7];
  178. for (int i = 0; i < stemPoints.length; i++) {
  179. this.stemPoints[i] = new Point(0, 0);
  180. }
  181. update(6);
  182. }
  183. void update(int dw) {
  184. int w = dw * 3;
  185. for (int i = 0; i < 3; i++) {
  186. int j = i;
  187. if (i > 0) j = i - 1;
  188. int dx = controlPoints[j].y - controlPoints[i + 1].y;
  189. int dy = controlPoints[i + 1].x - controlPoints[j].x;
  190. float r = w / sqrt(dx*dx + dy*dy);
  191. stemPoints[i].moveTo(controlPoints[i].x, controlPoints[i].y);
  192. stemPoints[i].add(r*dx, r*dy);
  193. dx *= -1;
  194. dy *= -1;
  195. stemPoints[6 - i].moveTo(controlPoints[i].x, controlPoints[i].y);
  196. stemPoints[6 - i].add(r*dx, r*dy);
  197. w -= dw;
  198. }
  199. stemPoints[3].moveTo(controlPoints[3].x, controlPoints[3].y);
  200. }
  201. void draw() {
  202. drawStem(6, #629a37);
  203. drawSpikes();
  204. }
  205. void drawStem(int dw, color c) {
  206. update(dw);
  207. strokeWeight(1);
  208. stroke(c);
  209. fill(c);
  210. beginShape();
  211. vertex(stemPoints[0].x, stemPoints[0].y);
  212. bezierVertex(stemPoints[1].x, stemPoints[1].y,
  213. stemPoints[2].x, stemPoints[2].y,
  214. stemPoints[3].x, stemPoints[3].y);
  215. bezierVertex(stemPoints[4].x, stemPoints[4].y,
  216. stemPoints[5].x, stemPoints[5].y,
  217. stemPoints[6].x, stemPoints[6].y);
  218. endShape(CLOSE);
  219. }
  220. void drawSpikes() {
  221. update(6);
  222. drawSpikes(stemPoints[0].x, stemPoints[0].y,
  223. stemPoints[1].x, stemPoints[1].y,
  224. stemPoints[2].x, stemPoints[2].y,
  225. stemPoints[3].x, stemPoints[3].y,
  226. 20, 60, 60, 90, 40);
  227. drawSpikes(stemPoints[3].x, stemPoints[3].y,
  228. stemPoints[4].x, stemPoints[4].y,
  229. stemPoints[5].x, stemPoints[5].y,
  230. stemPoints[6].x, stemPoints[6].y,
  231. 0, 40, 60, 40, 90);
  232. }
  233. void drawSpikes(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4,
  234. int start, int end, int count, int w1, int w2) {
  235. randomSeed(0);
  236. for (int i = start; i <= end; i++) {
  237. float t = (float)i / count;
  238. float weight = w1 + (w2 - w1)*t;
  239. float x = bezierPoint(x1, x2, x3, x4, t);
  240. float y = bezierPoint(y1, y2, y3, y4, t);
  241. float tx = bezierTangent(x1, x2, x3, x4, t);
  242. float ty = bezierTangent(y1, y2, y3, y4, t);
  243. float tr = sqrt(tx*tx + ty*ty);
  244. float dx = random(16) - 8;
  245. float dy = random(16) - 8;
  246. x += dx;
  247. y += dy;
  248.  
  249. strokeWeight(16);
  250. stroke(#d0f0a7, 128);
  251. point(x, y);
  252.  
  253. strokeWeight(1);
  254. stroke(#d9f1c3, 64);
  255. int n = 50;
  256. for (int j = 0; j < n; j++) {
  257. float r = weight + random(weight / 2);
  258. float cs = - ty / tr;
  259. float sn = tx / tr;
  260. float angle = radians(random(120) - 60);
  261. float cs2 = cos(angle);
  262. float sn2 = sin(angle);
  263. cs = cs*cs2 - sn*sn2;
  264. sn = sn*cs2 + cs*sn2;
  265. line(x, y, x + cs*r, y + sn*r);
  266. }
  267. }
  268. }
  269.  
  270. }
  271.  
  272. class CheckBox implements Drawable {
  273. int left;
  274. int top;
  275. int height;
  276. String caption;
  277. boolean value;
  278. PFont font;
  279. CheckBox(int x, int y, int h, String caption, boolean initialValue) {
  280. this.left = x;
  281. this.top = y;
  282. this.height = h;
  283. this.value = initialValue;
  284. this.caption = caption;
  285. this.font = loadFont(PFont.list()[0], this.height);
  286. }
  287. boolean isMouseOver() {
  288. int mx = mouseX;
  289. int my = mouseY;
  290. return ((mx >= this.left) && (mx < this.left + this.height) &&
  291. (my >= this.top) && (my < this.top + this.height));
  292. }
  293. void mousePressed() {
  294. if (this.isMouseOver()) {
  295. this.value = !this.value;
  296. }
  297. }
  298. void draw() {
  299. stroke(#80c060);
  300. strokeWeight(4);
  301. noFill();
  302. rect(this.left, this.top, this.height, this.height);
  303. fill(#80c060);
  304. textFont(this.font);
  305. textAlign(LEFT, BOTTOM);
  306. text(this.caption, this.left + this.height * 1.2, this.top + this.height);
  307. if (this.value) {
  308. line(this.left, this.top + this.height / 2,
  309. this.left + this.height * 1 / 3, this.top + this.height);
  310. line(this.left + this.height * 1 / 3, this.top + this.height,
  311. this.left + this.height, this.top);
  312. }
  313. }
  314. }
Add Comment
Please, Sign In to add comment