|
Author |
Topic: How do I get my object parameters in an array? (Read 291 times) |
|
st33d
|
How do I get my object parameters in an array?
« on: Jul 27th, 2004, 12:18am » |
|
My latest creation creates random new lines based on lines drawn before. I'm moving from lines to paths now and that means many more variables. The information for the paths with be stored in a dynamic array (xy[iteration][path information]). So when it creates a new path it picks a random line from the database (xy[][]) and loads it into a carrier object. It then calculates the lengths of lines and angles. Then some random alterations are made to an angle or a length and the start position is moved. The new co-ords are calculated and then read from the object and passed to the array xy[][] that stores the line history. Here's the problem. Before I got away with just using functions like a good old basic subroutine programmer but the amount of variables I have to juggle makes me think I need some of this object stuff. The variables that have to go into the carrier number twenty (c[20]). That's all the co-ords for three beziers in a chain. The line lengths and angles from the c[20] are put into theta[9] and L[9]. I've worked out some neat loops that cycle through c[] theta[] and L[]. However, I can't find an example of an object with opening parameters and having an array in it. What is the correct syntax for doing this and do I have to do some weird trick like you do when you have to expand an array (which I hope to be be doing with xy[][])?
|
I could murder a pint.
|
|
|
fry
|
Re: How do I get my object parameters in an array?
« Reply #1 on: Jul 27th, 2004, 12:28am » |
|
not sure i follow.. maybe post code as an example of what you're trying to do?
|
|
|
|
st33d
|
Re: How do I get my object parameters in an array?
« Reply #2 on: Jul 27th, 2004, 6:20pm » |
|
On st33d.net, pattern 3 is the latest example of what I've been doing. It creates a pattern of lines that are all either parallel or perpendicular to each other with the odd line set at a random angle. I calculate the co-ords of the new line by taking an existing line and then altering the information about that line according to certain rules. The new line is recorded and the process starts all over again. I alter an existing line by loading it's information into a temporary container. I used to use a bunch of variables for that container, now I'd like to use an object. But I've never used an object before. So what follows is a bit of pseudo code that should lay out the structure of the object I'm trying to create. Just how erroneous it is I've no idea. I also don't know how to read information from the object once I've done all the changes. I'd be happy to elaborate further if anyone might be able to help. Code: //object for loading information on a three part path with anchors ABCD and control points 123456 and then altering that information with methods and finding out some *!$%ing way of reading it. float p=5; //random percentage mutation carrier; void setup(){ carrier=new mutation (//insert 20 blank variables that comprise the locations of ABCD and 123456) } class mutation { float [] c=new float[20]; //path information float [] theta=new float[20]; //angles at given points float [] L=new float[20]; //lengths between given points mutation(float 1, float 2, float 3 ... float 20){ c[0]=float 1; c[1]=float 2; c[2]=float 3; ... c[19]=float 20; } void cLoad (int index,float temp){ //this is my method for loading the carrier c[index]=temp; } void cTrig (){ for (i=0;i<2;i++){ //length between points abcd L[i]=sqrt(sq(abs(c[i*2]-c[(i*2)+2]))+sq(abs(c[(i*2)+1]-c[(i*2)+3]))); //angles at points abcd theta [i]=atan((c[(i*2)+1]-c[(i*2)+3]),(c[i*2]-c[(i*2)+2])); } for (i=1;i<6;i++){ //length between points 123456 L[i+2]=sqrt(sq(abs(c[int(i/2)*2]-c[(i+3)*2]))+sq(abs(c[(int(i/2)*2)+1]-c[((i+3)*2)+1]))); //angles at points 123456 theta [i+2]=atan((c[(int(i/2)*2)+1]-c[((i+3)*2)+1]),(c[int(i/2)*2]-c[(i+3)*2])); } //length and angle between a and d L[9]=sqrt(sq(abs(c[0]-c[6]))+sq(abs(c[1]-c[7]))); theta[9]=atan((c[1]-c[7]),(c[0]-c[6])); } void reDraw () { //calculate points based on lengths and angles that may have changed for (i=o;i<8;i++){ c[(i+1)*2]=cos(theta[i])*L[i]; c[((i+1)*2)+1]=sin(theta[i])*L[i]; } } void randTheta(int index){ //randomise an angle at a given point theta[index]=rand(TWO_PI); } void randL (int index){ //randomise a given length 20% shorter or longer L[index]=(L[index]-(L[index]/p))+random((L[index]/p)*2); } void parallel (){ // moves point a to somewhere nearby, effectively moving the whole line // imagine the code is here ok? This isn't the bit I need help with } void perpendicular (){ // moves point a and rotates angle at a 90 degrees so the new line will be // perpendicular to the last one. } } |
|
|
I could murder a pint.
|
|
|
st33d
|
Re: How do I get my object parameters in an array?
« Reply #3 on: Jul 27th, 2004, 6:23pm » |
|
WHOOPS! Theta[] and L[] are only 10 wide. There's 9 lines and angles to keep track of and the distance between A and D for calculating the perpendicular function.
|
I could murder a pint.
|
|
|
narain
|
Re: How do I get my object parameters in an array?
« Reply #4 on: Jul 28th, 2004, 6:42am » |
|
Why do you need to put all the parameters into a single array That's, like, nasty, dude. Hmmm... I guess what you want to do is group all those variables into more logical classes. You could start by creating a class for paths themselves, that stores the information of a single path, and then make an array of those Path objects. To get started, you can use the class as a container for data, with just an initialization function (called a constructor), like this: Code:class Path { float xa, xb, xc, xd, ya, yb, yc, yd; // or float[] x,y, or maybe float[] a,b,c,d, or even float[][] xy // and more float[]s for the control points... Path (float arg1, float arg2, ... ) { xa = arg1; ya = arg2; // ... } } |
| Then you can create a new Path object by Code:Path myPath = new Path(x1,y1,...); |
| and access its data (technically, its member variables) by myPath.xa, myPath.ya, etc. You can even assign to them like regular variables. Just think of it as a easy way to group data together. Btw, when you create an array of objects, only the array is created, but there is no object in any location. So you'll get an error if you try to do stuff with an array location, if it hasn't been filled with a bona fide object first. Code:Path[] paths = new Path[2000]; Path myPath = paths[42]; // no error here, actually: paths[42] has the special empty value 'null' // and myPath gets assigned 'null' as well. print(paths[42].xa); // this is where you get a NullPointerException error paths[42] = new Path(...); // now you can do stuff with it print(paths[42].xa); // this will work |
| See also: http://processing.org/discourse/yabb/board_TheoryandPractice_ac_tion_display_num_1065619874.html
|
« Last Edit: Jul 28th, 2004, 9:13am by narain » |
|
|
|
|
st33d
|
Re: How do I get my object parameters in an array?
« Reply #5 on: Jul 29th, 2004, 2:25am » |
|
Ah yes, thankyou. The 20 variable path I realised was silly. Why not break the path into lines and handle everything that way. I can alter any single line and stitch the path back together afterwards. It'll all fit it one nice happy array called xy[path iterations][number of lines in path][bezier co-ords]. And then I'll have a separate class to alter lines in paths. It will keep track of line lengths and angles inside of it. Hi ho.
|
I could murder a pint.
|
|
|
|