I would like to arrange some spheres randomly in space. To do that I run the following:
import peasy.*;
PeasyCam cam;
void setup(){
size(1300, 1300, P3D);
cam = new PeasyCam(this, 200);
randomSeed(0);
float posx=random(-50, 50);
float posy=random(-50, 50);
float posz=random(-50, 50);
}
void draw() {
background(0);
for (int i=0; i < numRows; i ++) { // where numRows is defined somewhere and are some values that I loop over to determine the size of the sphere
pushMatrix();
lights();
translate(posx, posy, posz);
sphere(50); // in my code, the size of the sphere is determined by some imported values
popMatrix();
}
But I don’t know to store the values in an array so I can access them in my for loop one by one and determine a random position. Is there a remedy? Thank you!
2 Likes
Look at ArrayList and set one up to type PVector
(before setup()
)
Then in a for loop make PVectors and add them to the arraylist
(All in setup()
)
In draw() read and show arraylist
See reference ArrayList
Example
import peasy.*;
PeasyCam cam;
ArrayList<PVector> list = new ArrayList();
void setup() {
size(1300, 1300, P3D);
cam = new PeasyCam(this, 200);
// randomSeed(0);
for (int i=0; i<5; i++) {
float posx=random(0, 150);
float posy=random(0, 150);
float posz=random(-450, 0);
list.add(new PVector(posx, posy, posz));
}
}
void draw() {
background(0);
lights();
noStroke();
fill(255, 0, 0);
for (PVector pv : list) {
pushMatrix();
translate(pv.x, pv.y, pv.z);
sphere(14);
popMatrix();
}
}
3 Likes
Awesome, thanks!
One follow-up question:
In this for-loop
for (PVector pv : list) {
pushMatrix();
translate(pv.x, pv.y, pv.z);
sphere(14);
popMatrix();
}
How could I use the loop index (pv) to extract elements from an IntList that I’ve defined outside of the loop?
When I run the following
float var = map(myIntList.get(pv), 0, 10000000000, 0, 100);
I get the error: The function “get()” expects parameters like: “get(int)”
Thank you!
When you want it in the same for loop and both lists have the same length use a conventional for loop with i and use get(i) on both lists
2 Likes
Alternatively you can use an int i and say i++; in the for loop
So I’m using the conventional for loop but when I run the following
translate(list.x, list.y, list.z);
I get the error “The global variable “x” does not exist” (same for y and z). Why is that?
It’s aliiiive! Thank you
2 Likes
A bit of help with ArrayLists;
ArrayList<Integer> abc = new ArrayList<Integer>();
abc.add(123);
println(abc.get(0)*2); //it acts the same as int value = 123;
essentially ArrayListName.get(<id>)
acts the same as varriableName.
It helps if you want to do something like:
ArrayList<ArrayList<Float>> info = new ArrayList<ArrayList<Float>>();
Thanks.
If I could just ask a follow-up question. I would like to be able to retrieve a name for each of my x, y, z coordinates.
Something like
String name = myTable.getString(i, 0);
float posx=random(0, 150);
float posy=random(0, 150);
float posz=random(0, 150);
I’d like to store these in some sort of array so that I can access the coordinates together with the name. But I see that Arrays and ArrayLists are type specific. Is there a datatype that is not type specific? Or is there a smarter way to access the name-coordinate pairs later in my code?
Thanks!
You can store 3 coordiantes in the PVector class. It is in the format new PVector(x,y,z)
.
To access it use:
float x = 123, y = 321, z = -333;
PVector pos = new PVector(x,y,z);
//instead of sphere(x,y,z) use sphere(pos.x,pos.y,pos.z);
//or in ArrayList form
ArrayList<PVector> list = new ArrayList<PVector>();
list.add(new PVector(x,y,z));
square(list.get(i).x,list.get(i).y,sphere.get(i).z);
In the PVector class, you can essentially store 3 float values.
The only minor “issue” with PVectors is that you cannot use
PVector pos = 1,5,-4; //this will not work
//you have to use:
PVector pos = new PVector(1,5,-4);
I see, I understand the PVector part. What if I wanted to link a name to each PVector? So that it kind of looks like this
name1 posx1 posy1 posz1
name2 posx2 posy2 posz2
name3 posx3 posy3 posz3
...
I don’t fully understand, but…
PVector name1 = new PVector(posx1,posy1,posz1);
PVector name2 = new PVector(posx2,posy2,post2);
PVector name3 = new PVector(posx3,posy3,posz3);
//to get varriables use
float posx1 = name1.x;
float posy1 = name1.y;
float posz1 = name1.z;
float posx2 = name2.x;
float posy2 = name2.y;
float posz2 = name2.z;
float posx3 = name3.x;
float posy3 = name3.y;
float posz3 = name3.z
You need to make a class
class MyPoint {
PVector pvPoint;
String namePoint;
}
Then the Arraylist ist of type MyPoint
ArrayList<MyPoint > list = new ....
Then it’s e.g. println(list.get(3).namePoint);
see website | tutorials | objects to learn more
2 Likes
Awesome, that’s exactly what I was looking for @Chrisir. Thanks!
2 Likes