Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. int xSpeed = 5, ySpeed = 3;
  2. int amountOfLines = 100;
  3. int []speedXArr = new int[2];
  4. int []speedYArr = new int [2];
  5. color []lineColorArr = new color [amountOfLines];
  6. int []startLineXArr = new int [amountOfLines];
  7. int []startLineYArr = new int [amountOfLines];
  8. int []endLineXArr = new int [amountOfLines];
  9. int []endLineYArr = new int [amountOfLines];
  10.  
  11. void setup()
  12. {
  13. size(842, 480);
  14. speedXArr[0]=xSpeed;
  15. speedYArr[0]=ySpeed;
  16. speedXArr[1]=xSpeed;
  17. speedYArr[1]=ySpeed;
  18. startLineXArr[0]=(int)random(width+1);
  19. startLineYArr[0]=(int)random(height+1);
  20. endLineXArr[0]=(int)random(width+1);
  21. endLineYArr[0]=(int)random(height+1);
  22. float angle = (float)millis()/1000.0;
  23. lineColorArr[0] = color(sin(angle) * 255,
  24. sin(angle + 6.28 / 3) * 255,
  25. sin(angle + 6.28 / 3 * 2) * 255 );
  26. }
  27.  
  28. void draw()
  29. {
  30. float alpha;
  31.  
  32. clear();
  33. background(0);
  34. addPosition();
  35. // Draw all lines
  36. for (int i = 0; i < amountOfLines; i++) {
  37. // alpha will define the transparancy.
  38. // The number of steps between 255 and 0 will be inline with number of elements in the array.
  39. alpha = map(i, 0, amountOfLines-1, 255, 0);
  40. stroke(lineColorArr[i], alpha);
  41. line(startLineXArr[i], startLineYArr[i], endLineXArr[i], endLineYArr[i]);
  42. }
  43. }
  44.  
  45. void addPosition() {
  46. // Shift the array backwards, so the next line
  47. // can be added to the array on index position 0
  48. for (int i = amountOfLines-1; i > 0; i--) {
  49. startLineXArr[i] = startLineXArr[i-1];
  50. startLineYArr[i] = startLineYArr[i-1];
  51. endLineXArr[i] = endLineXArr[i-1];
  52. endLineYArr[i] = endLineYArr[i-1];
  53. lineColorArr[i] = lineColorArr[i-1];
  54. }
  55.  
  56. // Create new line color
  57. float angle = (float)millis()/1000.0;
  58. lineColorArr[0] = color(sin(angle) * 255,
  59. sin(angle + 6.28 / 3) * 255,
  60. sin(angle + 6.28 / 3 * 2) * 255 );
  61.  
  62. // Create new line points
  63. // Check if you will go over the borders and change direction if so
  64. // New start point
  65. if (startLineXArr[1]+speedXArr[0] > width || startLineXArr[1]+speedXArr[0] < 0) {
  66. speedXArr[0] *= -1;
  67. }
  68. startLineXArr[0] = startLineXArr[1]+speedXArr[0];
  69.  
  70. if (startLineYArr[1]+speedYArr[0] > height || startLineYArr[1]+speedYArr[0] < 0) {
  71. speedYArr[0] *= -1;
  72. }
  73. startLineYArr[0] = startLineYArr[1]+speedYArr[0];
  74.  
  75. // New end point
  76. if (endLineXArr[1]+speedXArr[1] > width || endLineXArr[1]+speedXArr[1] < 0) {
  77. speedXArr[1] *= -1;
  78. }
  79. endLineXArr[0] = endLineXArr[1]+speedXArr[1];
  80.  
  81. if (endLineYArr[1]+speedYArr[1] > height || endLineYArr[1]+speedYArr[1] < 0) {
  82. speedYArr[1] *= -1;
  83. }
  84. endLineYArr[0] = endLineYArr[1]+speedYArr[1];
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement