1. /**
    2. * Array.
    3. *
    4. * An array is a list of data. Each piece of data in an array
    5. * is identified by an index number representing its position in
    6. * the array. Arrays are zero based, which means that the first
    7. * element in the array is [0], the second element is [1], and so on.
    8. * In this example, an array named "coswave" is created and
    9. * filled with the cosine values. This data is displayed three
    10. * separate ways on the screen.
    11. */
    12. float[] coswave; //创建了一个数组
    13. void setup()
    14. {
    15. size(640, 360); //设置窗口大小
    16. coswave = new float[width]; //将数据放入数组内
    17. for (int i = 0; i < width; i++)
    18. {
    19. //使用map函数将0-width与0-PI对应
    20. float amount = map(i, 0, width, 0, PI);
    21. //abs()计算绝对值,cos()函数
    22. coswave[i] = abs(cos(amount));
    23. }
    24. background(255); //背景色
    25. noLoop(); //禁止循环
    26. }
    27. void draw() {
    28. int y1 = 0;
    29. int y2 = height/3;
    30. for (int i = 0; i < width; i++)
    31. {
    32. //绘制线条或者边框的颜色值
    33. stroke(coswave[i]*255);
    34. //绘制一条线
    35. line(i, y1, i, y2);
    36. }
    37. y1 = y2;
    38. y2 = y1 + y1;
    39. for (int i = 0; i < width; i++) {
    40. stroke(coswave[i]*255 / 4);
    41. line(i, y1, i, y2);
    42. }
    43. y1 = y2;
    44. y2 = height;
    45. for (int i = 0; i < width; i++) {
    46. stroke(255 - coswave[i]*255);
    47. line(i, y1, i, y2);
    48. }
    49. }