|
Author |
Topic: Multi Dimensional Arrays (Read 521 times) |
|
GotCool
|
Multi Dimensional Arrays
« on: Aug 5th, 2004, 6:25pm » |
|
How would I do some thing like this: var myArray = [["Jim", "cars"], ["Sally", "bikes"], ["Ron", "movies"]]; in Processing?
|
|
|
|
TomC
|
Re: Multi Dimensional Arrays
« Reply #1 on: Aug 5th, 2004, 6:37pm » |
|
You can do it like this: Code: String myArray[][] = { {"Jim", "cars"}, {"Sally", "bikes"}, {"Ron", "movies"} }; |
| Which is actually shorthand for this: Code: String[][] myArray = new String[][] { new String[]{"Jim", "cars"}, new String[]{"Sally", "bikes"}, new String[]{"Ron", "movies"} }; |
| Incidentally, in Java the following two ways of declaring arrays are equivalent, so use the one which makes most sense to you. Code: String myArray[][]; String[][] myArray; |
|
|
« Last Edit: Aug 5th, 2004, 6:39pm by TomC » |
|
|
|
|
GotCool
|
Re: Multi Dimensional Arrays
« Reply #2 on: Aug 5th, 2004, 7:02pm » |
|
Thanks! There must be something else wrong with my code: Code: void setup() { size(600, 400); background(255); ellipseMode(CENTER_DIAMETER); noStroke(); fill(0); smooth(); } float radius=25; float gravity=2; float friction=.8; float balls=2; int ball[][]; for (int i=0; i<balls; i++) { boolean hit=true; while(hit) { int x=random(radius, width-radius); int y=random(radius, height-radius); hit=false; for (int j=0; j<i; j++) { if (dist(x, y, ball[j][0], ball[j][1]) < 2*radius) { hit=true; } } } ball[i] = {x, y, 0, 0}; } void loop() { background(255); for (int i=0; i<balls; i++) { ellipse(ball[i][0], ball[i][1], 2*radius, 2*radius); } } |
| It's supossed to create balls with individual properties to be later used and changed. Where did I go wrong?
|
|
|
|
arielm
|
Re: Multi Dimensional Arrays
« Reply #3 on: Aug 5th, 2004, 8:22pm » |
|
the problem is here: ball[i] = {x, y, 0, 0}; this form is only possible when declaring an array variable, while here: you use it in the middle of your code... solution: standard array access, e.g. ball[i][0] = x; ball[i][1] = y; etc... note: all this is over-complicated anyway (looks like c code), but in java, you can take advantage of object-oriented programming for such stuff. here's a processing tutorial staring o.o.p and balls: http://p5.chronotext.org/tutorials/motion_oop/
|
Ariel Malka | www.chronotext.org
|
|
|
GotCool
|
Re: Multi Dimensional Arrays
« Reply #4 on: Aug 5th, 2004, 9:38pm » |
|
Much thanks again, I got it working =) Code: Ball[] balls; void setup() { size(600, 400); ellipseMode(CENTER_DIAMETER); noStroke(); noSmooth(); balls = new Ball[100]; for (int i=0; i<balls.length; i++) { balls[i] = new Ball(); boolean hit=true; while(hit) { balls[i].x=random(radius, width-radius); balls[i].y=random(radius, height-radius); hit=false; for (int j=0; j<i; j++) { if (dist(balls[i].x, balls[i].y, balls[j].x, balls[j].y) < 2*radius) { hit=true; } } } balls[i].f = 40+random(40); } } float radius=10; float gravity=2; float friction=.9; void loop() { background(120, 140, 100); for (int i=0; i<balls.length; i++) { balls[i].refresh(); } } class Ball { float x; float y; float xSpeed=random(-20, 20); float ySpeed=0; float f; void refresh() { x+=xSpeed; y+=ySpeed; if (x-radius<0) { x=radius; xSpeed*=-friction; ySpeed*=friction;; } if (x+radius>width) { x=width-radius; xSpeed*=-friction; ySpeed*=friction;; } if (y-radius<0) { y=radius; xSpeed*=friction; ySpeed*=-friction;; } if (y+radius>height) { y=height-radius; xSpeed*=friction; ySpeed*=-friction;; } fill(f-20, f, f-40); ellipse(x, y, 2*radius, 2*radius); ySpeed+=gravity; } } |
|
|
« Last Edit: Aug 5th, 2004, 10:06pm by GotCool » |
|
|
|
|
narain
|
Re: Multi Dimensional Arrays
« Reply #5 on: Aug 6th, 2004, 3:07pm » |
|
Quote: ball[i] = {x, y, 0, 0}; this form is only possible when declaring an array variable, while here: you use it in the middle of your code... |
| You can, in fact, use something like this "in the middle of your code"; the syntax is ball[i] = new int[] {x, y, 0, 0}; because Java needs to be told to create a new array to assign ball[i] to. (In declarations, it knows it has to create one anyway, so no need, though you can probably use this syntax there as well.)
|
|
|
|
|