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 › Passing data between objects
Page Index Toggle Pages: 1
Passing data between objects (Read 855 times)
Passing data between objects
Aug 19th, 2009, 6:44am
 
Hi there,
I am sure this is a stupid noob question, but I just can't seem to understand how to pass data between objects.  In this particular example, I want to calculate the distance between two different objects.

Basically, I want to create a shape (obj 1) that responds to the distance from a point (obj 2).  Later, there will be multiple shapes and points.

I have this working inside a draw, but when I try to convert it into objects I run into all sorts of problems.  I have included the code below.  The commented out part is the non-working part.

Code:
Component a;
VectorPoint p;

void setup() {
 size (600, 300);
 a = new Component(100., 100.);
 p = new VectorPoint (200., 200.);
 
}

void draw() {
 background (160);
//  a.display();
 p.display();
 
}

void mouseDragged() {
 p.update();
}


class Component {

 // Methods
 VectorPoint scalePoint;
 PVector center;

 PVector p1, p2, p3, p4;
 PVector v1, v2, v3, v4;

 float componentMin = 20.;
 float componentMax = 60.;

 // Constructor
 Component (float cx_, float cy_) {

   scalePoint = new VectorPoint(400,100);
   center = new PVector (cx_, cy_);

   p1 = new PVector (center.x + 30, center.y - 30);
   p2 = new PVector (center.x - 30, center.y - 30);
   p3 = new PVector (center.x - 30, center.y + 30);
   p4 = new PVector (center.x + 30, center.y + 30);

   v1 = new PVector (0,0);
   v2 = new PVector (0,0);
   v3 = new PVector (0,0);
   v4 = new PVector (0,0);
 }

 void display() {
   // Calculate the maximum size possible on the screen
   float maxSize = sqrt (width*width + height*height);

   // Inner square to calculate the distances
   ellipse (p1.x, p1.y, 5, 5);
   ellipse (p2.x, p2.y, 5, 5);
   ellipse (p3.x, p3.y, 5, 5);
   ellipse (p4.x, p4.y, 5, 5);
/*
   // Distances from scalePoint to point on inner square
   float m1 = PVector.dist (scalePoint, p1);
   float m2 = PVector.dist (scalePoint, p2);
   float m3 = PVector.dist (scalePoint, p3);
   float m4 = PVector.dist (scalePoint, p4);

   float magnitude1 = map(m1, 0, maxSize, componentMin, componentMax);
   float magnitude2 = map(m2, 0, maxSize, componentMin, componentMax);
   float magnitude3 = map(m3, 0, maxSize, componentMin, componentMax);
   float magnitude4 = map(m4, 0, maxSize, componentMin, componentMax);

   v1.set ((center.x + (magnitude1 * cos ((7*PI) / 4))), (center.y + (magnitude1 * sin ((7*PI) / 4))), 0);
   v2.set ((center.x + (magnitude2 * cos ((5*PI) / 4))), (center.y + (magnitude2 * sin ((5*PI) / 4))), 0);
   v3.set ((center.x + (magnitude3 * cos ((3*PI) / 4))), (center.y + (magnitude3 * sin ((3*PI) / 4))), 0);
   v4.set ((center.x + (magnitude4 * cos ((PI) / 4))), (center.y + (magnitude4 * sin ((PI) / 4))), 0);

   // The component - center lines
   strokeWeight(0.5);
   ellipse (center.x, center.y, 10, 10);
   line (center.x, center.y, v1.x, v1.y);
   line (center.x, center.y, v2.x, v2.y);
   line (center.x, center.y, v3.x, v3.y);
   line (center.x, center.y, v4.x, v4.y);

   // The component - border edges
   strokeWeight(1);
   line (v1.x, v1.y, v2.x, v2.y);
   line (v2.x, v2.y, v3.x, v3.y);
   line (v3.x, v3.y, v4.x, v4.y);
   line (v4.x, v4.y, v1.x, v1.y);

   // Line from center to scalePoint
   //line (center.x, center.y, scalePoint.x, scalePoint.y);
   */
 }  
}


class VectorPoint {
 PVector vp;

 VectorPoint (float x_, float y_) {
   vp = new PVector (x_, y_);
 }

 void display() {

   // Scale Point
   strokeWeight(0.5);
   ellipse (vp.x, vp.y, 10, 10);
   line (vp.x - 10, vp.y, vp.x + 10, vp.y);
   line (vp.x, vp.y - 10, vp.x, vp.y + 10);


 }

 void update() {
   if (dist(mouseX, mouseY, vp.x, vp.y) < 10) {
     vp.x += (mouseX - pmouseX);
     vp.y += (mouseY - pmouseY);
   }
 }
}
Re: Passing data between objects
Reply #1 - Aug 19th, 2009, 6:46am
 
I think there is some fundamental gap in my knowledge of OOP. Just in case, this is how it should function:
Code:
PVector center;
PVector scalePoint;

PVector p1, p2, p3, p4;
PVector v1, v2, v3, v4;

float componentMin = 20.;
float componentMax = 60.;

void setup() {
 size (800,400);
 center = new PVector (width/2, height/2);
 scalePoint = new PVector (100,100);
 
 p1 = new PVector (center.x + 30, center.y - 30);
 p2 = new PVector (center.x - 30, center.y - 30);
 p3 = new PVector (center.x - 30, center.y + 30);
 p4 = new PVector (center.x + 30, center.y + 30);
 
 v1 = new PVector (0,0);
 v2 = new PVector (0,0);
 v3 = new PVector (0,0);
 v4 = new PVector (0,0);
 
}

void draw() {
 background(150);
 
 // Calculate the maximum size possible on the screen
 float maxSize = sqrt (width*width + height*height);
 
 // Inner square to calculate the distances
 ellipse (p1.x, p1.y, 5, 5);
 ellipse (p2.x, p2.y, 5, 5);
 ellipse (p3.x, p3.y, 5, 5);
 ellipse (p4.x, p4.y, 5, 5);
 
 // Distances from scalePoint to point on inner square
 float m1 = PVector.dist (scalePoint, p1);
 float m2 = PVector.dist (scalePoint, p2);
 float m3 = PVector.dist (scalePoint, p3);
 float m4 = PVector.dist (scalePoint, p4);
 
 float magnitude1 = map(m1, 0, maxSize, componentMin, componentMax);
 float magnitude2 = map(m2, 0, maxSize, componentMin, componentMax);
 float magnitude3 = map(m3, 0, maxSize, componentMin, componentMax);
 float magnitude4 = map(m4, 0, maxSize, componentMin, componentMax);
 
 v1.set ((center.x + (magnitude1 * cos ((7*PI) / 4))), (center.y + (magnitude1 * sin ((7*PI) / 4))), 0);
 v2.set ((center.x + (magnitude2 * cos ((5*PI) / 4))), (center.y + (magnitude2 * sin ((5*PI) / 4))), 0);
 v3.set ((center.x + (magnitude3 * cos ((3*PI) / 4))), (center.y + (magnitude3 * sin ((3*PI) / 4))), 0);
 v4.set ((center.x + (magnitude4 * cos ((PI) / 4))), (center.y + (magnitude4 * sin ((PI) / 4))), 0);
 
 // The component - center lines
 strokeWeight(0.5);
 ellipse (center.x, center.y, 10, 10);
 line (center.x, center.y, v1.x, v1.y);
 line (center.x, center.y, v2.x, v2.y);
 line (center.x, center.y, v3.x, v3.y);
 line (center.x, center.y, v4.x, v4.y);
 
 // The component - border edges
 strokeWeight(1);
 line (v1.x, v1.y, v2.x, v2.y);
 line (v2.x, v2.y, v3.x, v3.y);
 line (v3.x, v3.y, v4.x, v4.y);
 line (v4.x, v4.y, v1.x, v1.y);

   
 
 // Scale Point
 strokeWeight(0.5);
 ellipse (scalePoint.x, scalePoint.y, 10, 10);
 line (scalePoint.x - 10, scalePoint.y, scalePoint.x + 10, scalePoint.y);
 line (scalePoint.x, scalePoint.y - 10, scalePoint.x, scalePoint.y + 10);
 
 // Line from center to scalePoint
 line (center.x, center.y, scalePoint.x, scalePoint.y);
 
}

void mouseDragged() {
 if (dist(mouseX, mouseY, scalePoint.x, scalePoint.y) < 10) {
   scalePoint.x += (mouseX - pmouseX);
   scalePoint.y += (mouseY - pmouseY);
 }
}

Re: Passing data between objects
Reply #2 - Aug 19th, 2009, 7:31am
 
I haven't looked at all the code but I think I can answer your base question.
To have two objects to interact (eg. one computing the distance to another), you just have to pass the second object to the first one. It might be as a one shot parameter to a function, or it might become a field of the object, either by giving it in the constructor or by setting it later in a setter function (just assigning the parameter to the field).
Of course, you can also just pass some fields of the second object in methods call on the first one:

A a = new A();
B b = new B(); // or B b = new B(a);
b.setA(a); // => B has a A myA field and does myA = a
// or
b.doStuff(a);
// or
b.doOtherStuff(a.f); // or a.getF()
Re: Passing data between objects
Reply #3 - Aug 20th, 2009, 12:04am
 
Thanks PhiLho,
I have it working with a simple sketch and two classes - no problems, but I think my problem might be passing the pvector.x and .y values.  Maybe this is not possible?
Re: Passing data between objects
Reply #4 - Aug 20th, 2009, 4:55am
 
I think it is possible... But I don't understand the problem. Where are you stuck?
Re: Passing data between objects
Reply #5 - Aug 20th, 2009, 6:00am
 
So if the other code is working, what is it that you are changing, and why?
Re: Passing data between objects
Reply #6 - Aug 20th, 2009, 7:33am
 
I am stuck with this part:
Code:
float m1 = PVector.dist (scalePoint, p1); 


I want to be able to pass the coordinates of the scalePoint into the PVector.dist . I am sure there is a simple answer, but I can't seem to find it.

As to why, I wanted to write it as a class because the plan is to use many of them in an ArrayList.
Re: Passing data between objects
Reply #7 - Aug 20th, 2009, 8:04am
 
Code:
float m1 = PVector.dist (scalePoint.vp, p1); 



You wrote scaledPoint, which is a VectorPoint object, not a PVector object.

The PVector.dist() method expects two PVector objects as arguments, so better use scalePoint.vp.
Re: Passing data between objects
Reply #8 - Aug 20th, 2009, 8:16am
 
Aha!

Thanks antiplastik - now I understand!
Page Index Toggle Pages: 1