| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 38b269e commit 4dd95fd
18 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,19 +1,19 @@ | |||
| 1 | 1 | --- | |
| 2 | - slug: '/tutorials/2darray' | ||
| 3 | - title: 'Two-Dimensional Arrays' | ||
| 4 | - author: 'Daniel Shiffman' | ||
| 5 | - intro: 'Store and acess data in a matrix using a two-dimensional array.' | ||
| 6 | - level: 'Intermediate' | ||
| 2 | + slug: "/tutorials/2darray" | ||
| 3 | + title: "Two-Dimensional Arrays" | ||
| 4 | + author: "Daniel Shiffman" | ||
| 5 | + intro: "Store and acess data in a matrix using a two-dimensional array." | ||
| 6 | + level: "Intermediate" | ||
| 7 | 7 | coverImage: 2darray.jpg | |
| 8 | 8 | --- | |
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is from the book [Learning Processing](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is from the book [Learning Processing](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| 16 | - An [array](http://www.processing.org/reference/Array.html) keeps track of multiple pieces of information in linear order, a one-dimensional list. However, the data associated with certain systems (a digital image, a board game, etc.) lives in two dimensions. To visualize this data, we need a multi-dimensional data structure, that is, a multi-dimensional array. A two-dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays). Think of your dinner. You could have a one-dimensional list of everything you eat: | ||
| 16 | + An [array](http://www.processing.org/reference/Array.html) keeps track of multiple pieces of information in linear order, a one-dimensional list. However, the data associated with certain systems (a digital image, a board game, etc.) lives in two dimensions. To visualize this data, we need a multi-dimensional data structure, that is, a multi-dimensional array. A two-dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays). Think of your dinner. You could have a one-dimensional list of everything you eat: | ||
| 17 | 17 | ||
| 18 | 18 | `(lettuce, tomatoes, steak, mashed potatoes, cake, ice cream)` | |
| 19 | 19 | ||
@@ -22,6 +22,7 @@ Or you could have a two-dimensional list of three courses, each containing two t | |||
| 22 | 22 | `(lettuce, tomatoes) and (steak, mashed potatoes) and (cake, ice cream)` | |
| 23 | 23 | ||
| 24 | 24 | In the case of an array, our old-fashioned one-dimensional array looks like this: | |
| 25 | + | ||
| 25 | 26 | ``` | |
| 26 | 27 | int[] myArray = {0,1,2,3}; | |
| 27 | 28 | ``` | |
@@ -56,7 +57,7 @@ int[][] myArray = { {236, 189, 189, 0}, | |||
| 56 | 57 | {236, 189, 189, 80} }; | |
| 57 | 58 | ``` | |
| 58 | 59 | ||
| 59 | - To walk through every element of a one-dimensional array, we use a for loop, that is: | ||
| 60 | + To walk through every element of a one-dimensional array, we use a for loop, that is: | ||
| 60 | 61 | ||
| 61 | 62 | ``` | |
| 62 | 63 | int[] myArray = new int[10]; | |
@@ -72,7 +73,7 @@ int cols = 10; | |||
| 72 | 73 | int rows = 10; | |
| 73 | 74 | int[][] myArray = new int[cols][rows]; | |
| 74 | 75 | ||
| 75 | - // Two nested loops allow us to visit every spot in a 2D array. | ||
| 76 | + // Two nested loops allow us to visit every spot in a 2D array. | ||
| 76 | 77 | // For every column I, visit every row J. | |
| 77 | 78 | for (int i = 0; i < cols; i++) { | |
| 78 | 79 | for (int j = 0; j < rows; j++) { | |
@@ -113,7 +114,7 @@ for (int i = 0; i < cols; i++) { | |||
| 113 | 114 | } | |
| 114 | 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. | ||
| 117 | + 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. | ||
| 117 | 118 | ||
| 118 | 119 | <FixedImage side width={200} > | |
| 119 | 120 | ||
@@ -142,8 +143,8 @@ void setup() { | |||
| 142 | 143 | ||
| 143 | 144 | void draw() { | |
| 144 | 145 | background(0); | |
| 145 | - // The counter variables i and j are also the column and row numbers and | ||
| 146 | - // are used as arguments to the constructor for each object in the grid. | ||
| 146 | + // The counter variables i and j are also the column and row numbers and | ||
| 147 | + // are used as arguments to the constructor for each object in the grid. | ||
| 147 | 148 | for (int i = 0; i < cols; i++) { | |
| 148 | 149 | for (int j = 0; j < rows; j++) { | |
| 149 | 150 | // Oscillate and display each object | |
@@ -155,7 +156,7 @@ void draw() { | |||
| 155 | 156 | ||
| 156 | 157 | // A Cell object | |
| 157 | 158 | class Cell { | |
| 158 | - // A cell object knows about its location in the grid | ||
| 159 | + // A cell object knows about its location in the grid | ||
| 159 | 160 | // as well as its size with the variables x,y,w,h | |
| 160 | 161 | float x,y; // x,y location | |
| 161 | 162 | float w,h; // width and height | |
@@ -168,21 +169,18 @@ class Cell { | |||
| 168 | 169 | w = tempW; | |
| 169 | 170 | h = tempH; | |
| 170 | 171 | angle = tempAngle; | |
| 171 | - } | ||
| 172 | - | ||
| 172 | + } | ||
| 173 | + | ||
| 173 | 174 | // Oscillation means increase angle | |
| 174 | 175 | void oscillate() { | |
| 175 | - angle += 0.02; | ||
| 176 | + angle += 0.02; | ||
| 176 | 177 | } | |
| 177 | 178 | ||
| 178 | 179 | void display() { | |
| 179 | 180 | stroke(255); | |
| 180 | 181 | // Color calculated using sine wave | |
| 181 | 182 | fill(127+127*sin(angle)); | |
| 182 | - rect(x,y,w,h); | ||
| 183 | + rect(x,y,w,h); | ||
| 183 | 184 | } | |
| 184 | 185 | } | |
| 185 | 186 | ``` | |
| 186 | - | ||
| 187 | - | ||
| 188 | - | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: color.jpg | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is from the book [Learning Processing](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is from the book [Learning Processing](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: data.jpg | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is from the book [Learning Processing, 2nd Edition](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2015 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is from the book [Learning Processing, 2nd Edition](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2015 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: electronics.jpg | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is “Extension 5” from [Processing: A Programming Handbook for Visual Designers and Artists, Second Edition](../../handbook), published by MIT Press. © 2014 MIT Press. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is “Extension 5” from [Processing: A Programming Handbook for Visual Designers and Artists, Second Edition](../../handbook), published by MIT Press. © 2014 MIT Press. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: network.jpg | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is “Extension 2” from [Processing: A Programming Handbook for Visual Designers and Artists, Second Edition](../../handbook), published by MIT Press. © 2014 MIT Press. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is “Extension 2” from [Processing: A Programming Handbook for Visual Designers and Artists, Second Edition](../../handbook), published by MIT Press. © 2014 MIT Press. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: objects.jpg | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is from the book [Learning Processing](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is from the book [Learning Processing](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: pixels.jpg | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is from the book [Learning Processing](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is from the book [Learning Processing](https://processing.org/books/#shiffman) by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc. All rights reserved. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: print.jpg | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is “Extension 4” from [Processing: A Programming Handbook for Visual Designers and Artists, Second Edition](../../handbook), published by MIT Press. © 2014 MIT Press. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is “Extension 4” from [Processing: A Programming Handbook for Visual Designers and Artists, Second Edition](../../handbook), published by MIT Press. © 2014 MIT Press. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: pshape.jpg | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - If you see any errors in this tutorial or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). This work is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-nc-sa/4.0/). | ||
| 12 | + If you see any errors in this tutorial or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). This work is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-nc-sa/4.0/). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ coverImage: sound.png | |||
| 9 | 9 | ||
| 10 | 10 | <Note> | |
| 11 | 11 | ||
| 12 | - This tutorial is “Extension 3” from [Processing: A Programming Handbook for Visual Designers and Artists, Second Edition](../../handbook), published by MIT Press. © 2014 MIT Press. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-docs/issues?q=is%3Aopen). | ||
| 12 | + This tutorial is “Extension 3” from [Processing: A Programming Handbook for Visual Designers and Artists, Second Edition](../../handbook), published by MIT Press. © 2014 MIT Press. If you see any errors or have comments, [please let us know](https://github.com/processing/processing-website/issues?q=is%3Aopen). | ||
| 13 | 13 | ||
| 14 | 14 | </Note> | |
| 15 | 15 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments