Advertisement
xeromino

prim

Mar 19th, 2016
247
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // based on this video: https://www.youtube.com/watch?v=BxabnKrOjT0
  2.  
  3. var vertices = [];
  4. var reached, unreached;
  5. var saving = false;
  6. var counter = 100;
  7.  
  8. function setup() {
  9.   createCanvas(540, 540);
  10. }
  11.  
  12. function mousePressed() {
  13.   var v = createVector(mouseX, mouseY);
  14.   vertices.push(v);
  15.   saving = true;
  16. }
  17.  
  18. function keyTyped() {
  19.   if (key === 'r') {
  20.     vertices = [];
  21.     init();
  22.   }
  23. }
  24.  
  25. function init() {
  26.   reached = [];
  27.   unreached = [];
  28.   for (var i = 0; i < vertices.length; i++) {
  29.     unreached.push(vertices[i]);
  30.   }
  31.  
  32.   reached.push(unreached[0]);
  33.   unreached.splice(0, 1);
  34. }
  35.  
  36.  
  37. function draw() {
  38.   background(20);
  39.  
  40.   init();
  41.  
  42.   while (unreached.length > 0) {
  43.     var record = 10000;
  44.     var rIndex = 0,
  45.       uIndex = 0;
  46.  
  47.     for (var i = 0; i < reached.length; i++) {
  48.       for (var j = 0; j < unreached.length; j++) {
  49.         var v1 = reached[i];
  50.         var v2 = unreached[j];
  51.         var d = dist(v1.x, v1.y, v2.x, v2.y);
  52.  
  53.         if (d < record) {
  54.           record = d;
  55.           rIndex = i;
  56.           uIndex = j;
  57.         }
  58.       }
  59.     }
  60.  
  61.     var r1 = reached[rIndex];
  62.     var r2 = unreached[uIndex];
  63.  
  64.     stroke(255);
  65.     strokeWeight(2);
  66.     line(r1.x, r1.y, r2.x, r2.y);
  67.  
  68.     reached.push(unreached[uIndex]);
  69.     unreached.splice(uIndex, 1);
  70.  
  71.   }
  72.  
  73.   for (var i = 0; i < vertices.length; i++) {
  74.     fill(255);
  75.     noStroke();
  76.     ellipse(vertices[i].x, vertices[i].y, 16, 16);
  77.   }
  78.  
  79.   if (saving) {
  80.     save("anim/image-" + counter + ".png");
  81.     println("saved image");
  82.     saving = false;
  83.     counter++;
  84.   }
  85. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement