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 › how to use translate with this
Page Index Toggle Pages: 1
how to use translate with this (Read 472 times)
how to use translate with this
Oct 26th, 2006, 7:29pm
 
Hi,

How do I use translate with this. In the code below I want to translate the entire group of ellipses, but I also want to translate each one of the ellipses when I mouse over it:

import processing.opengl.*;

MyEllipse [] myEllipse = new MyEllipse[9];

class MyEllipse{
 float x, y, wide, high, translateX, translateY;
 MyEllipse(float x, float y, float wide, float high, float translateX, float translateY){
   this.x = x;
   this.y = y;
   this.wide = wide;
   this.high = high;
   this.translateX = translateX;
 }
 void ellipsChange(){
   if(over()){
     fill(255);
     high = 10; wide = 10; //translateX=20;
   }
   else{
     fill(255, 255, 0);
     high = 20; wide = 20; //translateX=-20;
   }
   pushMatrix();
   translate(translateX, translateY);
   ellipse(x, y, wide, high);
   popMatrix();
 }
 boolean over(){
   if(dist(mouseX, mouseY, x, y) < wide / 2){
   return true;
   }
   return false;
 }
}  

void setup(){
 size(300, 300);
 for(int i = 0; i < myEllipse.length; i++){
   myEllipse[i] = new MyEllipse((i % 3) * 30, (i / 3) * 30, 20, 20, 0, 0);
 }
}
void draw(){
 background(0);
 smooth();
 // here i want to translate the entire group of ellipses, but if I put translate
 //around this, the interaction doesn't follow ...
 for(int i = 0; i < myEllipse.length; i++){
   myEllipse[i].ellipsChange();
 }
}

Re: how to use translate with this
Reply #1 - Oct 26th, 2006, 8:02pm
 
Simplest thing to do (based on your existing code) is to just add translation into your instantiation. Here's the modified setup()
Code:

void setup(){
size(300, 300);
float tx = width/2-30;
float ty = height/2-30;

for(int i = 0; i < myEllipse.length; i++){
myEllipse[i] = new MyEllipse((i % 3) * 30 + tx, (i / 3) * 30 + ty, 20, 20, 0, 0);
}
}


The translates weren't changing the x and y values, so the  detection wasn't getting updated.

Hope this helps some.
ira
Page Index Toggle Pages: 1