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 & HelpPrograms › rotate question
Pages: 1 2 
rotate question (Read 5064 times)
rotate question
May 10th, 2010, 6:05pm
 
I'm trying to rotate a shape but it isn't working. I tried altering the example code to see what the basic issue was, but I still get NullPointerException. What's the problem?

Here's the code as I altered it:

PShape button;


void setup()
{
 size(200,200);
 noStroke();
 fill(255);
 frameRate(30);
 loadShape("B-01.svg");
}

float angle;
float cosine;
//float jitter;

void draw()
{
background(102);
 
 if(second()%2 == 0){
   //jitter = (random(-0.1, 0.1));
 }
 angle = angle + 0.1;
 cosine = cos(angle);
 
 translate(width/2, height/2);
 rotate(cosine);
 shapeMode(CENTER);
 shape(button, 0, 0, 115, 115);  
Re: rotate question
Reply #1 - May 10th, 2010, 9:38pm
 
You have defined button but you have not assigned it using loadShape().
Try this instead:
Code:
button = loadShape("B-01.svg"); 

Re: rotate question
Reply #2 - May 10th, 2010, 10:12pm
 
It worked! Thanks!

I have another question:

So it works, but I want to use more than one rotating shape without both rotating around the same axis. I want each to be spinning independently in two different areas, spinning in place.

But some reason, when I duplicate the code to the first shape and alter the translate(), it doesn't seem to work. I think I'm missing a basic logic to translate() but I can't seem to figure out what the problem is. Please help!

Here's the code I have now:

Code:
PShape button;


void setup()
{
size(200,200);
noStroke();
fill(255);
frameRate(30);
button = loadShape("B-01.svg");

}

float angle;
float cosine;

float angle2;
float cosine2;

void draw()
{
background(102);


angle = angle + 0.1;
cosine = cos(angle);

translate(width/2, height/2);
rotate(cosine);
shapeMode(CENTER);
shape(button, 0, 0, 115, 115);

angle2 = angle2 + 0.1;
cosine2 = cos(angle2);

translate(width/4, width/4);
rotate(cosine2);
//shapeMode(CENTER);
shape(button, 0, 0, 115, 115);
}
Re: rotate question
Reply #3 - May 10th, 2010, 11:37pm
 
Take a look at push- and popMatrix().
http://processing.org/reference/pushMatrix_.html
http://processing.org/reference/popMatrix_.html

They boil down to the following.
pushMatrix: store the current transformation to memory
popMatrix: recall transformation from memory

A stored transformation can be the default, making it easier to 'start from scratch' for each shape.

Hope that helps.
Re: rotate question
Reply #4 - Jun 5th, 2010, 4:33am
 
Hi can any one help me, how to rotate the ellipse for this code


PImage mapImage;
Table locationTable;
//Table nameTable;
int rowCount;
Table dataTable;
float dataMin;
float dataMax;
int col = 0 ;
int temp;
void setup( ) {
size(640, 400);
mapImage = loadImage("ireland.png");
locationTable = new Table("kumar.tsv");
rowCount = locationTable.getRowCount( );
// Read the data table.
dataTable = new Table("update.tsv");
//nameTable = new Table("irelandname.tsv");
PFont font = loadFont("Arial-BoldMT-16.vlw");
textFont(font);
// Find the minimum and maximum values.
for (int row = 0; row < rowCount; row++) {
float value = locationTable.getFloat(row, 3);
if (value > dataMax) {
dataMax = value;
}
if (value < dataMin) {
dataMin = value;
}
}
}
// Global variables set in drawData( ) and read in draw( )
float closestDist;
String closestText;
float closestTextX;
float closestTextY;
void draw( ) {
background(255);
image(mapImage, 0, 0);
closestDist = MAX_FLOAT;

for (int row = 0; row < rowCount; row++) {
String abbrev = locationTable.getRowName(row);
float x = locationTable.getFloat(abbrev, 1);
float y = locationTable.getFloat(abbrev, 2);
drawData(x, y, abbrev);

}
// Use global variables set in drawData( )
// to draw text related to closest circle.
if (closestDist != MAX_FLOAT) {
fill(0);
textAlign(CENTER);
text(closestText, closestTextX, closestTextY);
}
fill(0);
text("Emails Information",70,30);
}
void drawData(float x, float y, String abbrev) {
float value = locationTable.getFloat(abbrev, 3);
float radius = 0;
if (value >= 0 && value<=55) {
radius = map(value, 10, 55, 2, 15);
fill(#4422CC); // blue
} else {
radius = map(value, 0, 180, 4, 30);
fill(#FF4422); // red
}
ellipseMode(RADIUS);
ellipse(x, y, radius*0.8, radius*0.8);
float d = dist(x, y, mouseX, mouseY);
// Because the following check is done each time a new
// circle is drawn, we end up with the values of the
// circle closest to the mouse.
if ((d < radius + 2) && (d < closestDist)) {
closestDist = d;
String name = locationTable.getString(abbrev, 0);
closestText = name + " " + value;
closestTextX = x;
closestTextY = y-radius-4;
}
}
void keyPressed( ) {
if (key == CODED) {
  try{
     if (keyCode == RIGHT && col < 4 && col >= 0) {
       col = col+1;
       updateTable();
       }
    /*   if (keyCode == RIGHT && col ==4)
       {
          col=1;
       }
      */
       if (keyCode == LEFT && col > 0 && col < 4){
         col = col-1;  
         updateTable();
           
       }
  }catch(Exception e)
  {
    col=1;
 //   println("Error here:"+e);
  }
   
}
}

void updateTable(){
for (int row = 0; row < rowCount; row++) {
 float newValue = dataTable.getFloat(row, col);
//float newValue = random(dataMin, dataMax);
locationTable.setFloat(row, 3, newValue);
}
}

thank you
Re: rotate question
Reply #5 - Jun 8th, 2010, 4:29am
 
Hi can any one help me please
Re: rotate question
Reply #6 - Jun 8th, 2010, 4:47am
 
First advice: indent your code if you want people to look at it.
Second advice: be more precise in your questions: rotate how much (depending on what), around which point?
Re: rotate question
Reply #7 - Jun 8th, 2010, 5:28am
 
also there are a bunch of files that anyone wanting to test this will need but which you don't supply:

mapImage = loadImage("ireland.png");
locationTable = new Table("kumar.tsv");
dataTable = new Table("update.tsv");
PFont font = loadFont("Arial-BoldMT-16.vlw");

you have to help people to help you.
Re: rotate question
Reply #8 - Jun 8th, 2010, 7:10am
 
And rotating centered ellipses = Smiley
Re: rotate question
Reply #9 - Jun 8th, 2010, 9:26am
 
Sorry  to all,

ireland.png is ireland map of size 640,400

kumar.tsv table is(tsv tab separated value)

Name        X       Y     No.of emails
Cork              248      331      10                  
Derry      418      69      20          
Dublin      501      204      25          
Galway      253      213      30          
Limerick      252      280      39          

with X and Y values am drawing ellipse on ireland map, if more emails ellipse size will be big if less size is small.

update table is

Cork              25     34    40            
Derry      20       22      54          
Dublin      40     70    119          
Galway      59       79      108          
Limerick      29      34      33

and  i don't know weather we can rotate or not

thank you.      
Re: rotate question
Reply #10 - Jun 8th, 2010, 9:58am
 
like Rapatsk1 said, how do you want to rotate an ellipse ? i am sure the answer to your question is pretty easy but nobody understands the question.
Re: rotate question
Reply #11 - Jun 8th, 2010, 10:10am
 
Cannot find a class or type named "Table".
Re: rotate question
Reply #12 - Jun 8th, 2010, 10:14am
 
koogy wrote on Jun 8th, 2010, 10:10am:
Cannot find a class or type named "Table".

i believe this from bens book Visualizing Data Book for reading csvs
Re: rotate question
Reply #13 - Jun 8th, 2010, 11:33am
 
yes, i figured as much. but it's just another hurdle on top of all the others. and i'm not trying to single kumar out but he's been pretty textbook in how not to ask a question on these forums - vague, missing necessary data.

as far as i can tell he's just displaying ellipses but doesn't want them aligned with their xy along the screen's xy. which is solvable with a pushMatrix, a rotate and a popMatrix (maybe some translates). but i can't be sure without running his code...

(actually, given that his ellipses are actually circles i'm not sure what rotating them is going to achieve)
Re: rotate question
Reply #14 - Jun 8th, 2010, 11:48am
 
yep, me neither, i tried to reduce his code and made it work, but it doesnt make much sense to me.
Pages: 1 2