You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can use this type of data structure to encode information about an image. For example, the following grayscale image could be represented by the following array:
45
45
46
-
<FixedImagewidth={650}>
46
+
<FixedImagesidewidth={200}>
47
47
48
48

49
49
@@ -83,7 +83,7 @@ for (int i = 0; i < cols; i++) {
83
83
84
84
For example, we might write a program using a two-dimensional array to draw a grayscale image.
85
85
86
-
<FixedImagecenterwidth={650} >
86
+
<FixedImagesidewidth={200} >
87
87
88
88

89
89
@@ -115,7 +115,7 @@ for (int i = 0; i < cols; i++) {
115
115
116
116
A two-dimensional array can also be used to store objects, which is especially convenient for programming sketches that involve some sort of "grid" or "board." The following example displays a grid of Cell objects stored in a two-dimensional array. Each cell is a rectangle whose brightness oscillates from 0-255 with a sine function.
Copy file name to clipboardExpand all lines: content/tutorials/text/arrays/index.mdx
+75Lines changed: 75 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,8 +48,12 @@ int x9 = 39;
48
48
49
49
Using what we know about drawing without arrays, ten variables are needed to store the data; each variable is used to draw a single rectangle. This is tedious:
50
50
51
+
<FixedImagesidewidth={100}height={100}>
52
+
51
53

52
54
55
+
</FixedImage>
56
+
53
57
```
54
58
int x0 = 50;
55
59
int x1 = 61;
@@ -76,8 +80,12 @@ rect(0, 90, x9, 8);
76
80
77
81
In contrast, the following example shows how to use an array within a program. The data for each bar is accessed in sequence with a for loop. The syntax and usage of arrays is discussed in more detail in the following pages.
After an array is defined and assigned values, its data can be accessed and used within the code. An array element is accessed with the name of the array variable, followed by brackets around the element position to read.
158
166
167
+
<FixedImagesidewidth={100}height={100}>
168
+
159
169

160
170
171
+
</FixedImage>
172
+
161
173
```
162
174
int[] data = { 19, 40, 75, 76, 90 };
163
175
@@ -192,8 +204,13 @@ println(data3.length); // Prints "127" to the console
192
204
193
205
Usually, a for loop is used to access array elements, especially with large arrays. The following example draws the same lines as code 28-09 but uses a for loop to iterate through every value in the array.
194
206
207
+
<FixedImagesidewidth={100}height={100}>
208
+
195
209

196
210
211
+
</FixedImage>
212
+
213
+
197
214
```
198
215
int[] data = { 19, 40, 75, 76, 90 };
199
216
@@ -204,8 +221,13 @@ for (int i = 0; i < data.length; i++) {
204
221
205
222
A for loop can also be used to put data inside an array. For instance, it can calculate a series of numbers and then assign each value to an array element. The following example stores the values from the `sin()` function in an array within `setup()` and then displays these values as the stroke values for lines within `draw()`.
206
223
224
+
<FixedImagesidewidth={100}height={100}>
225
+
207
226

208
227
228
+
</FixedImage>
229
+
230
+
209
231
```
210
232
float[] sineWave;
211
233
@@ -232,12 +254,25 @@ void draw() {
232
254
233
255
As one example of how arrays may be used, this section shows how to use arrays to store data from the mouse. The `pmouseX` and `pmouseY` variables store the cursor coordinates from the previous frame, but there is no built-in way to access the cursor values from earlier frames. At every frame, the `mouseX`, `mouseY`, `pmouseX`, and `pmouseY` variables are replaced with new numbers and their previous numbers are discarded. Creating an array is the easiest way to store the history of these values. In the following example, the most recent 100 values from `mouseY` are stored and displayed on screen as a line from the left to the right edge of the screen. At each frame, the values in the array are shifted to the right and the newest value is added to the beginning.
234
256
257
+
<FixedImagesidewidth={100}height={100}>
258
+
235
259

236
260
261
+
</FixedImage>
262
+
263
+
<FixedImagesidewidth={100}height={100}>
264
+
237
265

238
266
267
+
</FixedImage>
268
+
269
+
<FixedImagesidewidth={100}height={100}>
270
+
239
271

240
272
273
+
</FixedImage>
274
+
275
+
241
276
```
242
277
int[] y;
243
278
@@ -263,12 +298,25 @@ void draw() {
263
298
264
299
Apply the same code simultaneously to the `mouseX` and `mouseY` values to store the position of the cursor. Displaying these values each frame creates a trail behind the cursor.
265
300
301
+
<FixedImagesidewidth={100}height={100}>
302
+
266
303

267
304
305
+
</FixedImage>
306
+
307
+
<FixedImagesidewidth={100}height={100}>
308
+
268
309

269
310
311
+
</FixedImage>
312
+
313
+
<FixedImagesidewidth={100}height={100}>
314
+
270
315

271
316
317
+
</FixedImage>
318
+
319
+
272
320
```
273
321
int num = 50;
274
322
int[] x = new int[num];
@@ -455,12 +503,25 @@ Working with arrays of objects is technically similar to working with arrays of
455
503
456
504
These steps are translated into code in the following example. It uses the Ring class from page 371, so copy it over or retype it. This code creates a `rings[]` array to hold fifty `Ring` objects. Space in memory for the `rings[]` array is allocated in `setup()` and each `Ring` object is created. The first time a mouse button is pressed, the first `Ring` object is turned on and its `x` and `y` variables are assigned to the current values of the cursor. Each time a mouse button is pressed, a new `Ring` is turned on and displayed in the subsequent trip through `draw()`. When the final element in the array has been created, the program jumps back to the beginning of the array to assign new positions to earlier `Rings`.
457
505
506
+
<FixedImagesidewidth={100}height={100}>
507
+
458
508

459
509
510
+
</FixedImage>
511
+
512
+
<FixedImagesidewidth={100}height={100}>
513
+
460
514

461
515
516
+
</FixedImage>
517
+
518
+
<FixedImagesidewidth={100}height={100}>
519
+
462
520

463
521
522
+
</FixedImage>
523
+
524
+
464
525
```
465
526
Ring[] rings; // Declare the array
466
527
int numRings = 50;
@@ -523,10 +584,19 @@ class Ring {
523
584
524
585
The next example requires the `Spot` class from page 363. Unlike the prior example, variable values are generated within the `setup()` and are passed into each array elements through the object's constructor. Each element in the array starts with a unique set of x-coordinate, diameter, and speed values. Because the number of objects is dependent on the width of the display window, it is not possible to create the array until the program knows how wide it will be. Therefore, the array is declared outside of `setup()` to make it global (see p. 12), but it is created inside setup, after the width of the display window is defined.
525
586
587
+
<FixedImagecenterwidth={700}height={100}>
588
+
526
589

527
590
591
+
</FixedImage>
592
+
593
+
<FixedImagecenterwidth={700}height={100}>
594
+
528
595

529
596
597
+
</FixedImage>
598
+
599
+
530
600
```
531
601
Spot[] spots; // Declare array
532
602
void setup() {
@@ -614,8 +684,13 @@ println(x[9][1]); // Prints "204"
614
684
615
685
This sketch shows how it all fits together.
616
686
687
+
<FixedImagesidewidth={100}height={100}>
688
+
617
689

618
690
691
+
</FixedImage>
692
+
693
+
619
694
```
620
695
int[][] x = { {50, 0}, {61,204}, {83,51}, {69,102},
Copy file name to clipboardExpand all lines: content/tutorials/text/color/index.mdx
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,8 +53,12 @@ In addition, if we draw two shapes, Processing will always use the most recently
53
53
54
54
Remember finger painting? By mixing three “primary” colors, any color could be generated. Swirling all colors together resulted in a muddy brown. The more paint you added, the darker it got. Digital colors are also constructed by mixing three primary colors, but it works differently from paint. First, the primaries are diff erent: red, green, and blue (i.e., “RGB” color). And with color on the screen, you are mixing light, not paint, so the mixing rules are different as well.
Copy file name to clipboardExpand all lines: content/tutorials/text/coordinate-system-and-shapes/index.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -126,7 +126,7 @@ It is important to acknowledge that these ellipses do not look particularly circ
126
126
"point" over and over again), but for now, we are content with allowing the "ellipse" statement to do the hard work. (For more about pixels, start with: [the pixels reference page](http://processing.org/reference/pixels.html), though be warned this is a great deal more advanced than this tutorial.)
127
127
Now let's look at what some code with shapes in more realistic setting, with window dimensions of 200 by 200. Note the use of the [`size()`](http://www.processing.org/reference/size_.html) function to specify the width and height of the window.
0 commit comments