Guest User

Untitled

a guest
Jan 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. int xspacing = 32; // How far apart should each horizontal location be spaced
  2. int w; // Width of entire wave
  3.  
  4. float theta = 0.0; // Start angle at 0
  5. float amplitude = 400.0; // Height of wave
  6. float period = 500.0; // How many pixels before the wave repeats
  7. float dx; // Value for incrementing X, a function of period and xspacing
  8. float[] yvalues; // Using an array to store height values for the wave
  9.  
  10. void setup() {
  11. size(1000, 1000);
  12. w = width+250;
  13. dx = (TWO_PI / period) * xspacing;
  14. yvalues = new float[w/xspacing];
  15. strokeWeight(10);
  16. }
  17.  
  18. void draw() {
  19. background(0);
  20. calcWave();
  21. renderWave();
  22.  
  23. }
  24.  
  25. void calcWave() {
  26. // Increment theta (try different values for 'angular velocity' here
  27. theta += 0.02;
  28.  
  29. // For every x value, calculate a y value with sine function
  30. float x = theta;
  31. for (int i = 0; i < yvalues.length; i++) {
  32. yvalues[i] = sin(x)*amplitude;
  33. x+=dx;
  34. }
  35. }
  36.  
  37. void renderWave() {
  38. noStroke();
  39. fill(255);
  40. stroke(255);
  41. strokeCap(SQUARE);
  42. // A simple way to draw the wave with an ellipse at each location
  43. for (int x = 0; x < yvalues.length; x++) {
  44. // ellipse(x*xspacing-250, height/2+yvalues[x], 16, 16);
  45. // ellipse(x*xspacing, (height/2+yvalues[x]), 16, 16);
  46. ellipse(height/2+yvalues[x], x*xspacing-256, 16, 16);
  47. ellipse(height/2+yvalues[x], x*xspacing, 16, 16);
  48. // line(height/2+yvalues[x], x*xspacing, height/2+1, x*xspacing);
  49. // line(height/2+yvalues[x], x*xspacing-256, height/2-1, x*xspacing-256);
  50. // line(height/2+yvalues[x], x*xspacing-256, height/2+sin(dx)*amplitude, (x+1)*xspacing-256);
  51.  
  52. }
  53. }
Add Comment
Please, Sign In to add comment