Advertisement
Guest User

Untitled

a guest
Sep 12th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. var flock, cD, cN;
  2.  
  3. function setup() {
  4. createCanvas(640, 360);
  5.  
  6. flock = new Flock();
  7. for (var i = 0; i < 100; i++) {
  8. flock.add();
  9. }
  10. }
  11.  
  12. function draw() {
  13. background(51);
  14. flock.run();
  15. }
  16.  
  17. function Flock() {
  18. this.particles = [];
  19. }
  20.  
  21. Flock.prototype.add = function () {
  22. this.particles.push();
  23. }
  24.  
  25. Flock.prototype.run = function () {
  26. for (var i = 0; i < this.Particle.length; i++) {
  27. this.particles[i].update();
  28. this.particles[i].edge();
  29. this.particles[i].connect(this.particles);
  30. }
  31. }
  32.  
  33. function Particle() {
  34. this.velocity = createVector(random(-0.4, 0.4), random(-0.4, 0.4));
  35. this.location = createVector(random(0, width), random(0, height));
  36. }
  37.  
  38. Particle.prototype.update() = function(){
  39. this.location.add(this.velocity);
  40. }
  41.  
  42. Particle.prototype.edge() = function() {
  43. if (this.location.x > width) this.velocity.x *= -1;
  44. if (this.location.y > height) this.velocity.y *= -1;
  45. if (this.location.x < 0) this.velocity.x *= -1;
  46. if (this.location.y < 0) this.velocity.y *= -1;
  47. }
  48.  
  49. Particle.prototype.dots() = function() {
  50. noStroke();
  51. fill(255, 25 + alpha);
  52. ellipse(this.location.x, this.location.y, 1.5 + alpha, 1.5 + alpha);
  53. }
  54.  
  55. Particle.prototype.connect() = function(particles) {
  56. for (var i = 0; i < particles.length; i++) {
  57. var alpha = 255 - map(closestDistance, 0, 51, 0, 255);
  58. var d = p5.Vector.dist(this.location, particle[i].location);
  59.  
  60. if (boids[i] == this) {
  61. continue;
  62. }
  63.  
  64. if (d < cD) {
  65. cD = d;
  66. cN = particles[i];
  67. }
  68.  
  69. if (d < 51) {
  70.  
  71. stroke(255, alpha);
  72. strokeWeight(0.1 + alpha * 0.003);
  73. line(this.location.x, this.location.y, particle[i].location.x, particle[i].location.y);
  74. }
  75. }
  76.  
  77. alphaDbl = alpha * 2;
  78. alphaX = alpha*0.003;
  79.  
  80. if (closestDistance < 112) {
  81. this.display(alphaDbl, alphaX);
  82. closestDistance = 10000;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement