0% found this document useful (0 votes)
2K views29 pages

13 Array

The document discusses arrays and multi-dimensional arrays. It provides examples of declaring and initializing 1D and 2D arrays in code and using them to store and display data. Code snippets show arrays being used to store color values and coordinate points and display colored rectangles and dots on a canvas by accessing the array values.

Uploaded by

Gene Kao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views29 pages

13 Array

The document discusses arrays and multi-dimensional arrays. It provides examples of declaring and initializing 1D and 2D arrays in code and using them to store and display data. Code snippets show arrays being used to store color values and coordinate points and display colored rectangles and dots on a canvas by accessing the array values.

Uploaded by

Gene Kao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Processing

Array

13

Data 4 : Array

Database

()

Relational
Hierarchical
Network

()
()
()

Excel
Folder
Internet

Array (Data 4)
Object (Structure 4)
Pointer

int[] x1 = { 10, 30, 50, 70, 90, 110,130,150,170,190};


int[] x2 = { 20, 40, 60, 80,100,120,140,160,180,200};
x1[0] = 10;
x1[5]= 110;

x1[1]= 30;
x1[2] = 50;
x1[6] = 130; x1[7]= 150;

x1.length = 10;
0 1 2 3
10 30 50

x1[3]= 70;
x1[4] = 90;
x1[8] = 170; x1[9]= 190;

size(200,200);
strokeWeight(3);
smooth();
int[] x1 = { 10, 30, 50, 70, 90, 110,130,150,170,190};
int[] x2 = { 20, 40, 60, 80,100,120,140,160,180,200};

for (int i = 0; i < x1.length; i++) {


line(x1[i],10,x2[i],190);
}

line(x1[0],10,x2[0],190);
line(x1[1],10,x2[1],190);
line(x1[2],10,x2[2],190);
line(x1[3],10,x2[3],190);
line(x1[4],10,x2[4],190);

line(10,10,20,190);
line(30,10,40,190);
line(50,10,60,190);
line(70,10,80,190);
line(90,10,100,190);

size(200,200);
strokeWeight(3);
smooth();
int[] x1 = { 10, 30, 50, 70, 90, 110,130,150,170,190};
int[] x2 = { 20, 40, 60, 80,100,120,140,160,180,200};
for (int i = 0; i < x1.length; i++) {
line(x1[i],10,x2[i],190);
}
for (int i = 0; i < x1.length - 1; i++) {
line(x2[i],190,x1[i+1],10);
}

size(200,200);
strokeWeight(3);
smooth();
int[] x1 = { 10, 30, 50, 70, 90, 110,130,150,170,190};
int[] x2 = { 20, 40, 60, 80,100,120,140,160,180,200};
for (int i = 0; i < x1.length; i++) {
line(x1[i],10,x2[x2.length-i-1],190);
}

size(200,200);
strokeWeight(3);
smooth();
int[] x1 = { 10, 30, 50, 70, 90, 110,130,150,170,190};
int[] x2 = { 20, 40, 60, 80,100,120,140,160,180,200};
for (int i = 0; i < x1.length; i++) {
println(x1[i]+x2[i]);
}
for (int i = 0; i < x1.length; i++) {
line(x1[i],10,x2[i],190);
}
for (int i = 0; i < x1.length; i++) {
line(x1[i],10,x2[x2.length-i-1],190);
}

size(200,200);
int[] x1 = { 10, 30, 50, 70, 90, 110,130,150,170,190};
int[] x2 = { 20, 40, 60, 80,100,120,140,160,180,200};
for (int i = 0; i < x1.length; i++) {
for (int j = 0; j < x2.length; j++) {
line(x1[i],10,x2[j],190);
}
}

int[] data = new int[50]; // Declare, create


void setup() {
size(100, 100);
for (int i = 0; i < 100; i += 2) {
data[i/2] = i;
}
for (int i = 0; i < data.length; i++) {
println(data[i]);
}
}

int[] data = new int[50]; // Declare, create


void setup() {
size(100, 100);
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
data[i/2] = i;}
}
for (int i = 0; i < data.length; i++) {
println(data[i]);
}
}

2 Dimensional Array

size(1000,25);
noStroke();
int[] myArray = new int[40];
for (int i = 0; i < 40; i++) {
myArray[i] = i % 2 * 255;
fill(myArray[i]);
rect(i*25,0,25,25);
}

int[] x = new int[40];


void setup () {
size(800,20);
noLoop();
}

void cells () {
for (int i = 0; i < x.length; i++) {
x[i] = i % 2;
}
}

void draw () {
cells();
for (int i = 0; i < x.length; i++) {
if (x[i] == 0) {fill(0);} else {fill(255);}
rect(i*20,0,20,20);
}
}

int[] x = new int[40];

void setup () {
size(800,20);
frameRate(1);
cells();
}
void draw () {
background(255);
for (int i = 0; i < x.length; i++) {
if (x[i] == 0) {fill(0);} else {fill(255);}
rect(i*20,0,20,20);
}
swap_cells();
}

void cells () {
for (int i = 0; i < x.length; i++) {
x[i] = i % 2;
}
}
void swap_cells () {
for (int i = 0; i < x.length; i++) {
if (x[i] == 0) {x[i] = 1;} else {x[i] = 0;}
}
}

int[] myArray = new int[40];


void setup () {
size(1000,25);
noStroke();
frameRate(1);
for (int i = 0; i < 40; i++) {
myArray[i] = i % 2;
println(myArray[i]);
fill(myArray[i]*255);
rect(i*25,0,25,25);
}
}
void draw () {
background(255);
for (int i = 0; i < 40; i++) {
if (myArray[i] == 0) {myArray[i] = 1;}
else {myArray[i] = 0;}
fill(myArray[i]*255);
rect(i*25,0,25,25);
}
}

size(320,320);
int[][] data = new int[16][16];
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
data[i][j] = (i + j) % 2;
fill(data[i][j]*255);
rect(i*20,j*20,20,20);
}
}

int[][] data = new int[16][16];


void setup () {
size(320,320);
Initialization();
frameRate(5);
}
void draw () {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
if (data[i][j] == 0) {data[i][j] = 1;}
else {data[i][j] = 0;}
fill(data[i][j]*255);
rect(i*20,j*20,20,20);
}}}
void Initialization () {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
data[i][j] = (i + j) % 2;
fill(data[i][j]*255);
rect(i*20,j*20,20,20);
}}}

int[][] data = new int[16][16];

void setup () {
size(320,320);
Initialization();
}
void draw () {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
data[i][j] = data[i][j] + 1;
if (data[i][j] == 256) {data[i][j] = 0;}
fill(data[i][j],0,0);
rect(i*20,j*20,20,20);
}}}
void Initialization () {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
data[i][j] = (i * 16 + j);
fill(data[i][j],0,0);
rect(i*20,j*20,20,20);
}}}

size(200,200);
int[][] myArray = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
myArray[i][j] = (i + j) % 2;
fill(myArray[i][j] * 255);
rect(i*25,j*25,25,25);
}}

size(200,200);
int[][] myArray =
{{255, 191, 127, 0},
{255, 63, 191, 127},
{255, 0, 191, 63},
{255, 191, 0, 63}};

for (int i = 0; i < 4; i++) {


for (int j = 0; j < 4; j++) {
fill(myArray[i][j]);
rect(j*50,i*50,50,50);
}}

size(300,200);
int[][] myArray =
{{255, 191, 127, 0, 0, 0},
{255, 63, 191, 127, 127, 127},
{255, 0, 191, 63, 63, 63},
{255, 191, 0, 63, 63, 63}};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 6; j++) {
fill(myArray[i][j]);
rect(j*50,i*50,50,50);
}}

size(200,200);
int[][] myArray = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
myArray[i][j] = (abs(i-j) * 32);
}}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
fill(myArray[i][j]);
rect(i*25,j*25,25,25);
}}

size(200,200);
int[][] myArray = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
myArray[i][j] = 255 - (abs(i-j) * 32);
}}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
fill(myArray[i][j]);
rect(i*25,j*25,25,25);
}}

int[][] myArray = new int[8][8];

void setup () {
size(200,200);
frameRate(5);
}
void draw() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
myArray[i][j] = int(random(2));
fill(myArray[i][j] * 255);
rect(i*25,j*25,25,25);
}}
}

size(200,200);

int cols = width; int rows = height;


int[][] myArray = new int[cols][rows];

// Declare 2D array
// Initialize 2D array values

for (int i = 0; i < cols; i++) {


for (int j = 0; j < rows; j++) {
myArray[i][j] = int(random(255));
}
}
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
stroke(myArray[i][j]);
point(i,j);
}
}

// Draw points

int x;
int y;
void setup(){
size(1000, 400);
}
void draw(){
noStroke();
colorMode(HSB, width, height, 100);
x = mouseX + 2;
y = mouseY + 2;
for (int j = 0; j < height; j += y){
for (int i = 0; i < width; i += x){
fill(i, height - j, 100);
rect(i, j, x, y);
}
}
}

int num = 50;


int[] x = new int[num];
int[] y = new int[num];
void setup() {
size(100, 100);
noStroke();
smooth();
fill(255, 102);
}
void draw() {
background(0);
for (int i = num-1; i > 0; i--) {
x[i] = x[i-1];
y[i] = y[i-1];
}
x[0] = mouseX;
y[0] = mouseY;
for (int i = 0; i < num; i++) {
ellipse(x[i], y[i], i/2.0, i/2.0);
}
}

Ch. 33

You might also like