Array
From Design Computation
An array is a list of data. Each piece of data in an array is identified by an index number representing its position in the array. Arrays are zero based, which means that the first element in the array is [0], the second element is [1], and so on.
In this example, an array named "coswave" is created and filled with the cosine values. This data is displayed three separate ways on the screen.
Contents
Synonyms
Definition
Application
Processing
1 float[] coswave;
2
3 void setup() {
4 size(640, 360);
5 coswave = new float[width];
6 for (int i = 0; i < width; i++) {
7 float amount = map(i, 0, width, 0, PI);
8 coswave[i] = abs(cos(amount));
9 }
10 background(255);
11 noLoop();
12 }
13
14 void draw() {
15
16 int y1 = 0;
17 int y2 = height/3;
18 for (int i = 0; i < width; i++) {
19 stroke(coswave[i]*255);
20 line(i, y1, i, y2);
21 }
22
23 y1 = y2;
24 y2 = y1 + y1;
25 for (int i = 0; i < width; i++) {
26 stroke(coswave[i]*255 / 4);
27 line(i, y1, i, y2);
28 }
29
30 y1 = y2;
31 y2 = height;
32 for (int i = 0; i < width; i++) {
33 stroke(255 - coswave[i]*255);
34 line(i, y1, i, y2);
35 }
36
37 }