We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I am creating a matrix with two for loops, 'i' and 'j'. I would like to append the values to an empty list 'ptList' in this fashion: [0] i, j [1] i, j [2] i, j etc...
any ideas would be appreciated as I think this should be simple since I can do it quite easily in python.
//colour lists
color [] dragonKingdom = {#1143D9, #0D8BD9, #6BE5F2, #F2E205, #F9C3B3};
color[] palette = dragonKingdom;
int d = 50; //matrix subdivision values
int[] ptList = new int [50];
void setup() {
size(800, 400);
background(palette[4]);
ellipseMode(CENTER);
attractorPt();
//create list of x,y pts matrix values
for (int i = 0; i < width; i += d) {
for (int j = 0; j < height; j += d) {
//int [] ptList = append(ptList, [i, j]);
strokeWeight(5);
stroke(palette[int(random(1, 5))]);
point(i,j);
}
}
}
//attractor point method
void attractorPt() {
stroke(palette[3]);
strokeWeight(20);
point(random(width),random(height));
}
Answers
Still very novice in Python. Nonetheless, here's my attempt: >-)
No need for an extra ShortTuple class though, given Python already got tuples builtin. :P
@gotoloop: I appreciate the elegance of your code. :)>- Also, the way you are solving some problems of randomly choosing palette indices and how to set up a grid are quite helpful. Nevertheless, the method you have employed to create the x,y tuples is rather complex for me at this point in my learning. Is there a simpler way?
Here is how I can achieve it in python (just a snippet b/c code is for Rhino:
As you can see, it is so simple, I wonder if I use IntList or another functionality in processing/java. I am mostly confused with this code.
Why do you multiply by x*Rows and then add why? Also, this array seems to be either a class or embedded within a class. It is elegant, but a bit advanced for me :(
final ShortTuple[] dots = new ShortTuple[GRID];
Thank you again. My end goal is to measure the list of x,y values against the attractor point.
Now that I have done more research, it seems that a 2 dimensional array will solve the issue. This example is essentially what I want, but I am not sure what to set the 'myArray[i][j] equal to as I simply wish for it to contain the values for the action of creating ellipses.
I was trying to mirror Java Mode's sketch in Python Mode as much as possible. :-\"
In Java, array's length is determined at the time of its instantiation and can't be changed later.
Processing's append() function doesn't actually change the passed array's length btW.
Instead it creates another 1 w/ length + 1. Then internally arrayCopy() everything from passed array to the new 1, appends passed value parameter to it and finally returns the newly array.
As you can see, append() shouldn't be abused for performance reasons.
At most for sporadically append() a new element for when mousePressed() and such.
Otherwise, if its length changes too much within a short range of time...
Or we simply can't easily determine the length for it, we're better off going w/ some ArrayList.
For
int
,float
& String, containers IntList, FloatList & StringList are even better options.B/c it's a double loop. And iterators x & y behave as a 2D index pair.
Given that dots[] is a 1D array, that 2D index gotta be converted to 1D index 1st. :-B
If dots were an ArrayList instead, we'd use its add() method:
dots.add(new ShortTuple(i, j));
Similar case in Python if we replace
dots = GRID*[None]
w/dots = []
:dots.append((i, j))
As you see now, we don't need to worry about getting the right index at the moment of initializing the container when its initial length is 0. ;;)
Anyways for both Java & Python, assigning values by index is faster than changing container's length in order to append() some value at its tail. $-)
Yup, array dot[] can only store references of the "homemade" nested class ShortTuple. :P
Obviously we can change it to Processing's own PVector class:
https://Processing.org/reference/PVector.html
Or if you prefer, Java's Point class:
http://docs.Oracle.com/javase/8/docs/api/java/awt/Point.html
Or anything else which can store at least 2 values to form a coordinate pair: ;))
Thank you once again for your immensely enlightened response. It may take me a few days or more to digest this information; however, I am realizing that I might have over complicated the program for myself. Nevertheless, I think your 'ShortTuple' will come in handy.
I know that I can take segments of your ideology and apply it towards a more concise version of the program that uses either Array lists or PVectors. I will do some more research and thanks again for such a comprehensive response!