Guest User

Untitled

a guest
Jan 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. // @themoep
  2.  
  3. // keep track of color. is float so change can be slow without huge numbers
  4. float c = 0;
  5.  
  6. // the moving points are balls, but we don't draw them as such see visualize() later
  7. Ball[] balls;
  8.  
  9. void setup() {
  10. size(800,800);
  11.  
  12. // make 50 new balls to bounce around
  13. balls = new Ball[50];
  14. for(int i=0; i<50; i++) {
  15. balls[i] = new Ball();
  16. }
  17.  
  18. strokeWeight(2); //makes lines 2 pixels thick
  19. colorMode(HSB, 100); // changes red-gree-blue mode to hue-saturation-brightness with a range of 0-100
  20. }
  21.  
  22. void draw() {
  23. // change this to make the colours change faster/slower
  24. c+=0.05;
  25. if(c > 100) c = 0; // if it gets bigger than our colormode, wrap around back to 0
  26.  
  27. // the hue alone now controls the color; saturation and brightness at max
  28. stroke(color(int(c), 100, 100));
  29.  
  30. // first we update the position of each ball
  31. for(Ball ball : balls) {
  32. ball.update();
  33. }
  34.  
  35. // and then we draw the connections. if we do both in the same loop we might
  36. // update a position after drawing some lines and before drawing others, making tiny glitches
  37. for(Ball ball : balls) {
  38. ball.visualize(balls);
  39. }
  40. }
  41.  
  42.  
  43. class Ball {
  44. // each ball has a position and velocity. we could use PVectors instead, but I forgot about them.
  45. float x, y, dx, dy;
  46.  
  47. // a new ball starts at a random position and with a random speed that's limited by 5 in either dimension
  48. Ball() {
  49. x = random(width);
  50. y = random(height);
  51. dx = random(-5,5);
  52. dy = random(-5,5);
  53. }
  54.  
  55. // here we apply the velocity and check if it hits an edge. if so, we swap the direction of the velocity to make it bounce off the edge
  56. void update() {
  57. x+=dx;
  58. y+=dy;
  59. if(x > width || x < 0) dx*=-1;
  60. if(y > height || y < 0) dy*=-1;
  61. }
  62.  
  63. // this is pythagoras applied: we first get the absolute value of the difference of x and y respectively
  64. // then square the x and y difference and take the square root of that.
  65. // would be prettier with PVectors, but as I said, I forgot about them >_>
  66. float distance(Ball other) {
  67. return sqrt(pow(abs(this.x-other.x),2)+pow(abs(this.y-other.y),2));
  68. }
  69.  
  70. void visualize(Ball[] balls) {
  71. // we need all the balls there are
  72. for(Ball other : balls) {
  73. // and skip if the current one is the same
  74. if(this.equals(other)) {
  75. continue;
  76. }
  77. // and only draw a line if the distance is less than 150
  78. if(this.distance(other) < 150) {
  79. line(x,y,other.x, other.y);
  80. }
  81. }
  82. }
  83. }
Add Comment
Please, Sign In to add comment