Guest User

Untitled

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