We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › composite class question
Page Index Toggle Pages: 1
composite class question (Read 1961 times)
composite class question
Mar 30th, 2010, 6:56am
 
hi,

i have constructed an array of composite objects including two objects (A and B). I want to draw a line between an object of one composite and another: ie. between object A of one composite and object B of another composite.

Is this possible?
Re: composite class question
Reply #1 - Mar 30th, 2010, 8:56am
 
Yes.
Re: composite class question
Reply #2 - Mar 30th, 2010, 9:28am
 
thanks.

i need to loop through the other objects and access their positions.
If same class i could just .get() but i'm stumped how to actually access the objects of another composite - how would i do this to access their x and y positions?

//loop through object A's
//drawing a line between object B of this composite and object A's of others
for(int i=0; i<objectA.size; i++) {    
 line(objectB.x, objectB.y, otherObjectA.x, otherObjectA.y);
 }
Re: composite class question
Reply #3 - Mar 30th, 2010, 1:15pm
 
I don't understand your question. What is objectA? Which class did you use? Looks like the code above doesn't make any sense. I would expect something like :

Code:
MyObject[] myArrayOfObjects = new MyObject[10];
...
for (int i = 0; i < myArrayOfObjects.length - 1; i++) {
 MyObject objectA = myArrayOfObjects[i];
 MyObject objectB = myArrayOfObjects[i + 1];
 line(objectA.x, objectA.y, objectB.x, objectB.y);
}
Re: composite class question
Reply #4 - Mar 30th, 2010, 2:07pm
 
What antiplastik said...
If you have two different kinds of objects, you can make them to extend a common class defining the common parts between the two classes, at least the fact they have both x and y fields.
Thus, MyObject in antiplastik's example is the common class and it should work.
Re: composite class question
Reply #5 - Mar 31st, 2010, 9:43am
 
thanks for the help.

i know my question was a bit vague - was trying to be clear without pasting loads of code as my programme is getting quite long so thought easier not to confuse.

anyway i figured it out - to access the variables of an object in an array of composite objects need to:
//loop through the composite objects
for(int i=0; i<compObj.length; i++){    
 if (compObj[i].objA.id != id) {
 //draw line between object B and object A of other composite objects
   line(objB.x, objB.y,compObj[i].objA.position.x, compObj[i].objA.position.y);
   float a = compObj[i].objA.Size;
   float distToBdry = a/2;
   if (dist(objB.x, objB.y,compObj[i].objA.position.x, compObj[i].objA.position.y) < distToBdry) {
       do something;
       }
     }
   }
Page Index Toggle Pages: 1