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 › Perhaps a rather dumb question.
Page Index Toggle Pages: 1
Perhaps a rather dumb question. (Read 621 times)
Perhaps a rather dumb question.
Jan 8th, 2009, 7:56pm
 
Hello, I am learning programming through Processing which is great fun (I only know/knew GWBASIC). The only problem is that I really lack proper math knowledge (I'm learning Processing only for personal leisure). and although I'm picking it up as I go along, I somehow got stuck with some growing thingy.

Code:

//pseudocode: "Growing thingy"

draw a random line
create new line from center and perpendicular to previous line
end of the new line is half the distance of the previous line
repeat until a certain condition is met


For an alpha (is this distinction between alpha and beta in the Anglosphere a common distinction?) I was extremely happy that I found c.q. calculated the middle of the line and could draw something there. But to create the perpendicular line with half the length got me stuck.

I've tried everything Pythagorean I could amass, but to no avail.

Thanks in advance.
Re: Perhaps a rather dumb question.
Reply #1 - Jan 8th, 2009, 10:13pm
 
Kind of like this?

Code:

void setup(){
 size(800, 800);
 smooth();
 frameRate(2);
}

void draw(){

 translate(random(0,width), random(0,height));
 drawGrowingThingy(new PVector(random(-200,200), random(-200,200)), (int)random(0,10));
 
}

void drawGrowingThingy( PVector v, int numberOfReasonsToLive){
 stroke(30, 130);
 strokeWeight(2);
 line(0,0, v.x, v.y);
 if(numberOfReasonsToLive > 0){
   v.div(2);
   translate(v.x, v.y);

if(random(0,1) > 0.5){
rotate(HALF_PI);
}
else{
rotate(-HALF_PI);
}
   
   drawGrowingThingy( v, numberOfReasonsToLive-1);
 }

}



The concept of recursion is used here.  I think there is an example like this somewhere, but I'm too lazy to search Smiley
Re: Perhaps a rather dumb question.
Reply #2 - Jan 8th, 2009, 11:19pm
 
Thanks! I think I can work it out now, got to get my head around PVector, but this is really helpful.
Re: Perhaps a rather dumb question.
Reply #3 - Jan 8th, 2009, 11:22pm
 
You're welcome.  Trig gives me a headache, so I usually rely on the built in graphics transformation and translations.  Make sure to look up push and popMatrix, as well as screenX/Y/Z - they are absolutely useful.

PVector is handy in packing two float values in one package, along with all the vector calculation goodies. If the use of .div(2) looks strange, it actually changes the the vector value as a side-effect rather than returning a new vector.

Good luck!
Re: Perhaps a rather dumb question.
Reply #4 - Jan 14th, 2009, 10:34pm
 
I'm bumping my own thread, since I'm learning processing in a rather eclectic fashion. Jumping from idea to problem, to idea, to problem, et cetera. I'd rather not create a thread per question.

I get a nullpointerexception at the for loop in the update function, but can't seem to find what I am doing wrong. My goal is to write a simple class that creates a buffer of PVectors, and shifts down the PVectors upon calling the update function.

Thanks in advance.

Quote:
BufferXY newBuffer;

void setup(){
  background(0);
  size(200,200);
}

void draw(){
  newBuffer = new BufferXY(10, mouseX, mouseY);
  newBuffer.update();
}


class BufferXY{
  PVector[] vectors;
  int buffersize;
  float xpos;
  float ypos;

  BufferXY(int buffersize_,float xpos_,float ypos_){
    buffersize = buffersize_;
    xpos = xpos_;
    ypos = ypos_;
    
    //create the buffer
    PVector[] vectors = new PVector[buffersize];
    
    //fill the array with "empty" vectors.
    for (int i = 0; i <vectors.length; i++){
      vectors[i] = new PVector(0,0,0);
    }
  }

  void update(){
    //shift the values down one spot
    for (int i = 0; i < vectors.length-1; i++){
      vectors[i] = vectors[i+1];
    }

    //add a new position on the last spot.
    vectors[vectors.length-1] = new PVector(xpos, ypos, 0);
  }
}


Oh and I'm using the book "Learning Processing" by Shiffman, which is really helpful and understandable for someone with a non-technical background.
Re: Perhaps a rather dumb question.
Reply #5 - Jan 14th, 2009, 10:50pm
 
How did you add syntax coloring to your post?

I believe you are accidentally creating a new local variable (an array) in the constructor:

Code:

BufferXY newBuffer;

void setup(){
 background(0);
 size(200,200);
 smooth();
 newBuffer = new BufferXY(10, mouseX, mouseY);
}

void draw(){
 //newBuffer = new BufferXY(10, mouseX, mouseY); <- different problem.
 newBuffer.update();
}


class BufferXY{
 PVector[] vectors;
 int buffersize;
 float xpos;
 float ypos;

 BufferXY(int buffersize_,float xpos_,float ypos_){
   buffersize = buffersize_;
   xpos = xpos_;
   ypos = ypos_;
   
   //create the buffer
   //PVector[] vectors = new PVector[buffersize];  <== here.
   vectors = new PVector[buffersize];
   
   //fill the array with "empty" vectors.
   for (int i = 0; i <vectors.length; i++){
     vectors[i] = new PVector(0,0,0);
   }
 }

 void update(){
   //shift the values down one spot
  for (int i = 0; i < vectors.length-1; i++){
     vectors[i] = vectors[i+1];
   }

   //add a new position on the last spot.
   vectors[vectors.length-1] = new PVector(mouseX, mouseY, 0);  // <- I'm guessing this is what is needed
 }
}




So at the time of instantiation of BufferXY, you are creating two vectors[], but the one that you filled upon instantiation will lose its reference once out of scope, and subsequently deleted.


I also noticed another bug, so I fixed it, just in case.
Re: Perhaps a rather dumb question.
Reply #6 - Jan 14th, 2009, 11:36pm
 
I think I can answer the first question: somewhere in the menus of PDE, you have Copy for Discourse (in Edit, exactly).
I am not fan of the color scheme, so I don't use it... Smiley
Re: Perhaps a rather dumb question.
Reply #7 - Jan 14th, 2009, 11:53pm
 
Thank you PhiLho - I was trying to figure this one out for the longest time!
Re: Perhaps a rather dumb question.
Reply #8 - Jan 15th, 2009, 10:25pm
 
sw01, thanks again for helping out. By comparison with the rest of the board my questions seem like petty beginners problems. It were such obvious problems Tongue.

So I hope you guys/girls aren't bothered if I'll bump this thread again in the future.
Re: Perhaps a rather dumb question.
Reply #9 - Jan 15th, 2009, 11:02pm
 
Hey no problem because I'm a novice too - keep 'em coming Smiley
Page Index Toggle Pages: 1