Guest User

Untitled

a guest
Apr 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.79 KB | None | 0 0
  1. // Plane folding
  2. // Copyright by tsulej 2014, edited by Etienne Jacob (necessary-disorder.tumblr.com)
  3. // click mouse to change drawing
  4. float step ;
  5. Folds f;
  6.  
  7. int[] folds = new int[8];
  8.  
  9. void setup() {
  10. randomSeed(11);
  11.  
  12. size(500, 500);
  13. colorMode(RGB,255);
  14. smooth();
  15. noStroke();
  16. step = 0.75 / (2.0*width);
  17. textAlign(LEFT,TOP);
  18. initialize();
  19. }
  20.  
  21. float start;
  22. boolean go = true;
  23. boolean dosinusoidal = true;
  24.  
  25. void mouseClicked() {
  26. go = false;
  27. initialize();
  28. go = true;
  29. }
  30.  
  31. int fnumber;
  32. int nameSeed;
  33. int iPart = 0;
  34. int iFrame = 0;
  35.  
  36.  
  37. float alphaMax = 10;
  38. int numberOfParts = 4;
  39. int numFrames = 75;
  40. float rbegin = 1.5;
  41. float rend = 2.5;
  42.  
  43. void initialize() {
  44. f = new Folds();
  45. iPart = 0;
  46. iFrame = 0;
  47. fill(255,30);
  48. stroke(255,alphaMax);
  49. background(0);
  50. nameSeed = int(random(10000000));
  51. //dosinusoidal = random(0,1)<0.5?true:false;
  52. dosinusoidal = false;
  53. fnumber = (int)floor(random(3,6.99));
  54. for(int i=0;i<fnumber;i++) {
  55. folds[i]=(int)floor(random(1,21.99));
  56. }
  57. println(constructName());
  58. }
  59.  
  60. String constructName() {
  61. String r = "point -> ";
  62. for(int n = fnumber-1;n>=0;n--) {
  63. r += f.getNamebyNo(folds[n]) + " -> ";
  64. }
  65. if(dosinusoidal) r+= "sinusoidal -> ";
  66. return r + "point";
  67. }
  68.  
  69. void draw() {
  70.  
  71. if(go) {
  72. float t = 1.0*iFrame/numFrames;
  73. float theta = map(iPart+t,0,numberOfParts,0,TWO_PI);
  74. float s = alphaMax;
  75. stroke(255,s);
  76. for(float theta2=theta;theta2<=theta+PI/16;theta2+=0.001){
  77. for(float r=rbegin;r<=rend;r+=step){
  78. float x = r*cos(theta2);
  79. float y = r*sin(theta2);
  80. drawme(x,y);
  81. }
  82. }
  83.  
  84. iPart++;
  85.  
  86. if(iPart == numberOfParts) {
  87. iFrame++;
  88. iPart = 0;
  89.  
  90. println(iFrame+"/"+numFrames);
  91.  
  92. saveFrame("fr"+String.format("%04d", iFrame)+".png");
  93.  
  94. if(iFrame < numFrames) background(0);
  95.  
  96. if(iFrame == numFrames){
  97. go = false;
  98. println("finished");
  99. }
  100. }
  101. }
  102. }
  103.  
  104.  
  105. void drawme(float x, float y) {
  106. PVector p = new PVector(x,y);
  107.  
  108. PVector n = new PVector(0,0);
  109.  
  110. switch(fnumber) {
  111. case 1:
  112. n.add( f.getFoldbyNo(folds[0],p,1) );
  113. break;
  114. case 2:
  115. n.add( f.getFoldbyNo(folds[0], f.getFoldbyNo(folds[1],p,1), 1) );
  116. break;
  117. case 3:
  118. n.add( f.getFoldbyNo(folds[0], f.getFoldbyNo(folds[1],f.getFoldbyNo(folds[2],p,1),1), 1) );
  119. break;
  120. case 4:
  121. n.add( f.getFoldbyNo(folds[0], f.getFoldbyNo(folds[1],f.getFoldbyNo(folds[2],f.getFoldbyNo(folds[3],p,1),1),1), 1) );
  122. break;
  123. case 5:
  124. n.add( f.getFoldbyNo(folds[0], f.getFoldbyNo(folds[1],f.getFoldbyNo(folds[2],f.getFoldbyNo(folds[3],f.getFoldbyNo(folds[4],p,1),1),1),1), 1) );
  125. break;
  126. case 6:
  127. n.add( f.getFoldbyNo(folds[0], f.getFoldbyNo(folds[1],f.getFoldbyNo(folds[2],f.getFoldbyNo(folds[3],f.getFoldbyNo(folds[4],f.getFoldbyNo(folds[5],p,1),1),1),1),1), 1) );
  128. break;
  129. case 7:
  130. n.add( f.getFoldbyNo(folds[0], f.getFoldbyNo(folds[1],f.getFoldbyNo(folds[2],f.getFoldbyNo(folds[3],f.getFoldbyNo(folds[4],f.getFoldbyNo(folds[5],f.getFoldbyNo(folds[6],p,1),1),1),1),1),1), 1) );
  131. break;
  132. case 8:
  133. n.add( f.getFoldbyNo(folds[0], f.getFoldbyNo(folds[1],f.getFoldbyNo(folds[2],f.getFoldbyNo(folds[3],f.getFoldbyNo(folds[4],f.getFoldbyNo(folds[5],f.getFoldbyNo(folds[6],f.getFoldbyNo(folds[7],p,1),1),1),1),1),1),1), 1) );
  134. break;
  135. }
  136.  
  137. n.add( f.realgaussian(p,0.0045));
  138. //p.add( f.realgaussian(p,0.0035));
  139. if(dosinusoidal)
  140. n = f.sinusoidal(n,1);
  141. f.draw( n );
  142. //f.draw(p);
  143. }
  144.  
  145. float nextGaussian = 0;
  146. boolean haveNextGaussian = false;
  147.  
  148. /*
  149. float randomGaussian() {
  150. if(haveNextGaussian) {
  151. haveNextGaussian = false;
  152. return nextGaussian;
  153. } else {
  154. float v1,v2,s;
  155. do {
  156. v1 = 2 * random() - 1;
  157. v2 = 2 * random() - 1;
  158. s = v1 * v1 + v2 * v2;
  159. } while (s >=1 || s == 0);
  160.  
  161. float mult = sqrt(-2 * log(s) / s);
  162. nextGaussian = v2 * mult;
  163. haveNextGaussian = true;
  164. return v1 * mult;
  165. }
  166. }*/
  167.  
  168. class Folds {
  169. int screenl, screenr;
  170. int middle;
  171. float ymin;
  172. float ymax;
  173. float xmin;
  174. float xmax;
  175. boolean crop = true;
  176.  
  177. Folds() {
  178. float middle = width/2;
  179. float size = 0.95 * middle;
  180. screenl = (int)(-size + middle);
  181. screenr =(int)(size + middle);
  182. ymin = -3.5;
  183. ymax = 3.5;
  184. xmin = -3.5;
  185. xmax = 3.5;
  186.  
  187. precalc();
  188. }
  189.  
  190. PVector getFoldbyNo(int no, PVector p, float weight) {
  191. switch(no) {
  192. case 1: return sinusoidal(p,weight);
  193. case 2: return butterfly(p,weight);
  194. case 3: return wedgejulia(p,weight);
  195. case 4: return rotate(p,weight);
  196. case 5: return cross(p,weight);
  197. case 6: return pdj(p,weight);
  198. case 7: return fan2(p,weight);
  199. case 8: return rings2(p,weight);
  200. case 9: return heart(p,weight);
  201. case 10: return ex(p,weight);
  202. case 11: return popcorn(p,weight);
  203. case 12: return waves(p,weight);
  204. case 13: return polar(p,weight);
  205. case 14: return horseshoe(p,weight);
  206. case 15: return curl(p,weight);
  207. case 16: return scry(p,weight);
  208. case 17: return rectangles(p,weight);
  209. case 18: return julian(p,weight);
  210. case 19: return juliascope(p,weight);
  211. case 20: return twintrian(p,weight);
  212. case 21: return lines(p,weight);
  213. default: return p;
  214. }
  215. }
  216.  
  217. String getNamebyNo(int no) {
  218. switch(no) {
  219. case 1: return "sinusoidal";
  220. case 2: return "butterfly";
  221. case 3: return "wedgejulia";
  222. case 4: return "rotate";
  223. case 5: return "cross";
  224. case 6: return "pdj";
  225. case 7: return "fan2";
  226. case 8: return "rings2";
  227. case 9: return "heart";
  228. case 10: return "ex";
  229. case 11: return "popcorn";
  230. case 12: return "waves";
  231. case 13: return "polar";
  232. case 14: return "horseshoe";
  233. case 15: return "curl";
  234. case 16: return "scry";
  235. case 17: return "rectangles";
  236. case 18: return "julian";
  237. case 19: return "juliascope";
  238. case 20: return "twintrian";
  239. case 21: return "lines";
  240. default: return "none";
  241. }
  242. }
  243.  
  244. float lines_scale = random(0.03,0.4);
  245. boolean lines_squared = random(0,1) < 0.5 ? true : false;
  246.  
  247. PVector lines(PVector p, float weight) {
  248. float r;
  249. if(lines_squared) r=0.5*sq(randomGaussian());
  250. else r=0.25*randomGaussian();
  251. float y = lines_scale * (floor(p.y/lines_scale) - 0.5 + r);
  252.  
  253. return new PVector(weight * p.x, weight * y);
  254. }
  255.  
  256. float twintrian_weight = random(0.4,1);
  257.  
  258. PVector twintrian(PVector p, float weight) {
  259. float a = random(0,1) * twintrian_weight * p.mag();
  260. float sa = sin(a);
  261. float cla = cos(a) + log(sq(sa));
  262. if(cla<-30) cla = -30;
  263.  
  264. return new PVector(weight * 0.8 * p.x * cla, weight * 0.8 * p.x * (cla - sa * PI));
  265. }
  266.  
  267. float julian_power = random(0,1)<0.5? (int)random(4,10) : -(int)random(4,10);
  268. float julian_dist = random(0.5,3.0);
  269. float julian_cpower, julian_abspower;
  270.  
  271. PVector julian(PVector p, float weight) {
  272. float a = (atan2(p.y,p.x) + TWO_PI * floor(julian_abspower * random(0,1)))/julian_power;
  273. float r = weight * 2.0 * pow(sq(p.x)+sq(p.y),julian_cpower);
  274. return new PVector(r*cos(a),r*sin(a));
  275. }
  276.  
  277. float juliascope_power = random(0,1)<0.5? (int)random(4,10) : -(int)random(4,10);
  278. float juliascope_dist = random(0.5,2.0);
  279. float juliascope_cpower, juliascope_abspower;
  280.  
  281. PVector juliascope(PVector p, float weight) {
  282. int rnd = (int)(juliascope_abspower * random(0,1));
  283. float a;
  284. if(rnd % 2 == 0)
  285. a = (2 * PI * rnd +atan2(p.y,p.x))/juliascope_power;
  286. else
  287. a = (2 * PI * rnd -atan2(p.y,p.x))/juliascope_power;
  288. float r = weight * 2.0 * pow(sq(p.x)+sq(p.y),juliascope_cpower);
  289. return new PVector(r*cos(a),r*sin(a));
  290. }
  291.  
  292.  
  293. float rectangles_x = random(0.1,1);
  294. float rectangles_y = random(0.1,1);
  295.  
  296. PVector rectangles(PVector p, float weight) {
  297. float x = weight * ((2 * floor(p.x/rectangles_x) + 1)* rectangles_x - p.x);
  298. float y = weight * ((2 * floor(p.y/rectangles_y) + 1)* rectangles_y - p.y);
  299.  
  300. return new PVector(x,y);
  301. }
  302.  
  303. float scry_weight = random(0.4,1);
  304.  
  305. PVector scry(PVector p, float weight) {
  306. float r2 = sq(p.x)*sq(p.y);
  307. float r = 3.0 / (p.mag() * (r2 + 1.0/scry_weight));
  308. float x = weight * r * p.x;
  309. float y = weight * r * p.y;
  310. return new PVector(x,y);
  311. }
  312.  
  313. float fan2_x = random(-1,1);
  314. float fan2_y = random(2,7);
  315. float fan2_dx, fan2_dx2;
  316.  
  317. PVector fan2(PVector p, float weight) {
  318. float r = weight * 0.8 * p.mag();
  319. float theta = atan2(p.x,p.y);
  320. float t = theta + fan2_y - floor((theta + fan2_y) / fan2_dx) * fan2_dx;
  321. float ang;
  322. if(t > fan2_dx2)
  323. ang = theta - fan2_dx2;
  324. else
  325. ang = theta + fan2_dx2;
  326.  
  327. return new PVector(r * sin(ang), r * cos(ang));
  328. }
  329.  
  330. float pdj_a = random(-3.0,3.0);
  331. float pdj_b = random(-3.0,3.0);
  332. float pdj_c = random(-3.0,3.0);
  333. float pdj_d = random(-3.0,3.0);
  334.  
  335. PVector pdj(PVector p, float weight) {
  336. return new PVector( weight * 1.5 * (sin(pdj_a * p.y) - cos(pdj_b * p.x)),
  337. weight * 1.5 * (sin(pdj_c * p.x) - cos(pdj_d * p.y)));
  338. }
  339.  
  340. PVector sinusoidal(PVector p, float weight) {
  341. return new PVector(weight * 3.0 * sin(p.x),3.0 * sin(p.y));
  342. }
  343.  
  344. PVector butterfly(PVector p, float weight) {
  345. float y2 = 2.0 * p.y;
  346. float r = weight * 1.3029400317411197908970256609023 * sqrt(abs(p.y * p.x) / (1e-10 + sq(p.x) + sq(y2)));
  347. return new PVector(r * p.x,r * y2);
  348. }
  349.  
  350. int wedgejulia_count = (int)random(2,7);
  351. float wedgejulia_angle =random(-3,3);
  352. int wedgejulia_power = random(0,1)<0.5?(int)random(2,7):-(int)random(2,7);
  353. float wedgejulia_dist = random(1,4);
  354. float wedgejulia_cf, wedgejulia_cn, wedgejulia_rN;
  355.  
  356. PVector wedgejulia(PVector p, float weight) {
  357. float r = weight * pow(sq(p.x)+sq(p.y),wedgejulia_cn);
  358. int t_rnd = (int) ((wedgejulia_rN) * random(0,1));
  359. float a = (atan2(p.y,p.x) + TWO_PI * t_rnd) / wedgejulia_power;
  360. float c = floor((wedgejulia_count * a + PI) * (1/PI) * 0.5);
  361. a = a * wedgejulia_cf + c * wedgejulia_angle;
  362.  
  363. return new PVector(r * cos(a), r * sin(a));
  364. }
  365.  
  366. PVector rotate(PVector p, float angle) {
  367. float ca = cos(angle);
  368. float sa = sin(angle);
  369. return new PVector(ca * p.x - sa * p.y, sa * p.x + ca * p.y);
  370. }
  371.  
  372. PVector realgaussian(PVector p, float weight) {
  373. float a = TWO_PI * random(0,1);
  374. float r = weight * 3.0 * randomGaussian();
  375. return new PVector(r*cos(a),r*sin(a));
  376. }
  377.  
  378. PVector cross(PVector p, float weight) {
  379. float r = sqrt(1.0 / (sq(sq(p.x)-sq(p.y)))+1e-10);
  380. return new PVector(weight * 0.8 * p.x * r,weight * 0.8 * p.y * r);
  381. }
  382.  
  383. float curl_c1 = random(0.1,0.7);
  384. float curl_c2 = random(0.1,0.7);
  385.  
  386. PVector curl(PVector p, float weight) {
  387. float re = 1 + curl_c1 * p.x + curl_c2 * (sq(p.x)-sq(p.y));
  388. float im = curl_c1 * p.y + curl_c2 * 2 * p.x * p.y;
  389. float r = weight / (re * re + im * im);
  390.  
  391. return new PVector(r * (p.x * re + p.y * im),r * (p.y * re - p.x * im));
  392. }
  393.  
  394. float rings2_val = random(0.1,1.2);
  395. float rings2_val2;
  396.  
  397. PVector rings2(PVector p, float weight) {
  398. float r = p.mag();
  399. float theta = atan2(p.x,p.y);
  400. float d = weight*(r - 2.0 * rings2_val2 * floor( (r+rings2_val2)/(2.0 * rings2_val2)) + r * (1.0 - rings2_val2) );
  401. //float d = weight*(2.0 - rings2_val2 * ((int) ((r / rings2_val2 + 1) / 2) * 2 / r + 1));
  402. return new PVector(d*sin(theta),d*cos(theta));
  403. }
  404.  
  405. PVector heart(PVector p, float weight) {
  406. float r = p.mag();
  407. float theta = atan2(p.y,p.x);
  408. float sinr = sin(r * theta);
  409. float cosr = cos(r * theta);
  410.  
  411. return new PVector(weight * r * sinr, -r * weight * cosr);
  412. }
  413.  
  414. PVector ex(PVector p, float weight) {
  415. float r = p.mag();
  416. float theta = atan2(p.x,p.y);
  417. float xsin = sin(theta + r);
  418. float ycos = cos(theta - r);
  419. float x = weight * 0.7 * r * xsin * xsin * xsin;
  420. float y = weight * 0.7 * r * ycos * ycos * ycos;
  421. return new PVector(x+y,x-y);
  422. }
  423.  
  424. float popcorn_c = random(-0.8,0.8);
  425. float popcorn_f = random(-0.8,0.8);
  426.  
  427. PVector popcorn(PVector p, float weight) {
  428. float x = p.x + popcorn_c * sin(tan(3.0 * p.y));
  429. float y = p.y + popcorn_f * sin(tan(3.0 * p.x));
  430.  
  431. return new PVector(weight * 0.85 * x, weight * 0.85 * y);
  432. }
  433.  
  434. float waves_b = random(-0.8,0.8);
  435. float waves_e = random(-0.8,0.8);
  436. float waves_c = random(-0.8,0.8);
  437. float waves_f = random(-0.8,0.8);
  438.  
  439. PVector waves(PVector p, float weight) {
  440. float x = p.x + waves_b * sin(p.y * (1.0 / (waves_c * waves_c) ));
  441. float y = p.y + waves_e * sin(p.x * (1.0 / (waves_f * waves_f) ));
  442.  
  443. return new PVector(weight * x, weight * y);
  444. }
  445.  
  446. PVector horseshoe(PVector p, float weight) {
  447. float r = weight / (1.25 * (p.mag() + 1e-10));
  448. float x = r * ((p.x - p.y) * (p.x + p.y));
  449. float y = r * 2.0 * p.x * p.y;
  450. return new PVector(x,y);
  451. }
  452.  
  453. PVector polar(PVector p, float weight) {
  454. float r = p.mag();
  455. float theta = atan2(p.x,p.y);
  456. float x = theta / PI;
  457. float y = r - 2.0;
  458. return new PVector(weight * 1.5 * x, weight * 1.5 * y);
  459. }
  460.  
  461. void draw(PVector p) {
  462. float i = map(p.x,xmin,xmax,screenl,screenr);
  463. float j = map(p.y,ymin,ymax,screenl,screenr);
  464.  
  465. point(i,j);
  466. }
  467.  
  468. void precalc() {
  469. wedgejulia_cf = 1.0 - 0.5 / PI * wedgejulia_angle * wedgejulia_count;
  470. wedgejulia_cn = wedgejulia_dist / wedgejulia_power / 2.0;
  471. wedgejulia_rN = abs(wedgejulia_power);
  472. rings2_val2 = rings2_val * rings2_val;
  473. fan2_dx = PI * fan2_x * fan2_x + 1e-10;
  474. fan2_dx2 = 0.5 * fan2_dx;
  475. julian_cpower = julian_dist/julian_power/2.0;
  476. julian_abspower = abs(julian_power);
  477. juliascope_cpower = juliascope_dist/juliascope_power/2.0;
  478. juliascope_abspower = abs(juliascope_power);
  479. }
  480. }
Add Comment
Please, Sign In to add comment