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 › Another example from "Learning Processing"
Page Index Toggle Pages: 1
Another example from "Learning Processing" (Read 1141 times)
Another example from "Learning Processing"
Aug 10th, 2009, 12:30pm
 
Here is a sketch i arrived at after messing with one of the examples in Shiffman's book.  I have been trying (with not much success) to add in functionality so that this pattern would be aligned over an ellipse and stop growing when it reached the size of the ellipse.

Code:


// Polar coordinates (r, theta) are converted to Cartesian (x,y)
// for use in the ellipse() function.
float r = 0;
float theta = 0;

void setup() {
 size(500,500);
 background(50);
 smooth();
}

void draw() {
 
 // Polar to Cartesian conversion
 float x = r * cos(theta);
 float y = r * sin(theta);
 
 // Draw an ellipse at x,y
 noStroke();
 fill(0);
 ellipse(x + width/2, y + height/2, 7, 7); // Adjust for center of window
 
 // Increment the angle
 theta += 0.11;
 r = r+.15;
}
Re: Another example from "Learning Processing"
Reply #1 - Aug 10th, 2009, 1:10pm
 
you mean like this?


Code:
// Polar coordinates (r, theta) are converted to Cartesian (x,y) 
// for use in the ellipse() function.
float r = 0;
float theta = 0;

float mySize=100; //set the size of the ellipse

void setup() {
size(500,500);
background(50);
smooth();
frameRate(200);
}

void draw() {

//draw the ellipse
stroke(255);strokeWeight(2);noFill();
ellipse(width/2,height/2,2*mySize,2*mySize);


// Polar to Cartesian conversion
float x = r * cos(theta);
float y = r * sin(theta);

// Draw an ellipse at x,y
noStroke();
fill(0);
ellipse(x + width/2, y + height/2, 7, 7); // Adjust for center of window

// Increment the angle
theta += 0.11;
r = r+.15;


if(r > mySize) //check if reached the ellipse
{
noLoop();
println("yay");
}

}
Re: Another example from "Learning Processing"
Reply #2 - Aug 10th, 2009, 1:47pm
 
Wow...  I was making that way to complicated..  Thanks so much.. Huh
Re: Another example from "Learning Processing"
Reply #3 - Aug 10th, 2009, 2:39pm
 
I'd suggest a couple of slight changes to tavern's code (see comments in the code):

Code:
float r = 0; 
float theta = 0;

// It's always a good idea to use meaningful variable names:
// Makes life a lot easier when you're working with a lot of code!
float radiusLimit =100;

void setup() {
 size(500,500);
 background(50);
 smooth();
 // To make things move faster don't just increase the frameRate.
 // Beyond a certain point it's going to be limited by the CPU,
 // so results will depend on the system the sketch is running
 // on...
 frameRate(60);
}

void draw() {
 // I'd avoid getting into the habit of using noLoop()
 // In this case it works perfectly well, but it means your draw
 // loop quits altogether: not always what you want
 if (r < radiusLimit) {
     float x = r * cos(theta);
     float y = r * sin(theta);
     
     noStroke();
     fill(0);
     ellipse(x + width/2, y + height/2, 7, 7);
     
     theta += 0.11;
     r = r+.15;
     
     // By drawing the ellipse at the end it is always the last
     // thing to be drawn and is therefore always on top of
     // the black ellipses.
     stroke(255);strokeWeight(2);noFill();
     ellipse(width/2,height/2,2*radiusLimit,2*radiusLimit);
 }  

 
}
Re: Another example from "Learning Processing"
Reply #4 - Aug 17th, 2009, 9:43am
 
I would be most grateful if anyone could offer a hint as to why my spiral class is not working. Huh

Quote:
Ball ball1;
Ball ball2;
Spiral spiral1;

void setup() {
  size(1100,800);
  smooth();

  ball1 = new Ball(55);
  ball2 = new Ball(175);
  spiral1 = new Spiral(ball1.x, ball1.y, ball1.r);
}

void draw() {
  background(50);

  ball1.move();
  ball2.move();
  if (ball1.intersect(ball2)) {
    ball1.highlight();
    ball2.highlight();
  }
  spiral1.display();
  ball1.display();
  ball2.display();

  spiral1.x = ball1.x;
  spiral1.y = ball1.y;
}

class Ball {

  int redc = 0;
  
  float r;
  float x,y;
  float xspeed,yspeed;
  color c = color(200,200);

  Ball(float tempR) {
    r = tempR;
    x = random(width);
    y = random(height);
    xspeed = random(0,5);
    yspeed = random(0,5);
  }

  void move() {
    x += xspeed;
    y += yspeed;
    if (x > width || x < 0) {
      xspeed *= -1;
    }
    if (y > height || y < 0) {
      yspeed *= -1;
    }
  }

  
  void highlight() {

    
    c = color(redc,0,0,99);
    redc = redc+1;
    if (redc > 255) {
      redc = redc -1;
  }
  }

  void display() {
    stroke(0);
    strokeWeight(15);
    fill(c);
    ellipse(x,y,r*2,r*2);
    c = color(0,150,0);
  }

  
  boolean intersect(Ball other) {
    float distance = dist(x,y,other.x,other.y);
    if (distance < r + other.r) {
      return true;
    } else {
      return false;
    }
  }
}

class Spiral {

  float r = 0;
  float theta = 0;
  float radiusLimit =0;
  float x;
  float y;      

  Spiral(float tempX,float tempY,float tempradiusLimit) {

    x = tempX;
    y = tempY;
    tempradiusLimit = radiusLimit;
  }


  void display() {

    if (r < radiusLimit) {
      float x = r * cos(theta);
      float y = r * sin(theta);

      noStroke();
      fill(0);
      ellipse(x + width/2, y + height/2, 5, 5);

      theta += 0.11;
      r = r+.15;

      stroke(255);
      strokeWeight(2);
      noFill();
      ellipse(width/2,height/2,2*radiusLimit,2*radiusLimit);
    }  
  } 
}




Re: Another example from "Learning Processing"
Reply #5 - Aug 17th, 2009, 9:58am
 
you're not setting radiusLimit...

(edited)
Re: Another example from "Learning Processing"
Reply #6 - Aug 17th, 2009, 10:44am
 
koogy wrote on Aug 17th, 2009, 9:58am:
you're not setting radiusLimit...

(edited)


Well i wanted the radius limit to be set by the radius of the Ball it is attached to.

Edit: Ohhhh    hold on...

I got it to display.. but im trying to make the spiral move with the ball and build as it goes.
Re: Another example from "Learning Processing"
Reply #7 - Aug 17th, 2009, 11:12am
 
Getting closer..  now i we just need the spiral to build not just display one dot at a time.

Quote:
Ball ball1;
Ball ball2;
Spiral spiral1;

void setup() {
  size(1100,800);
  smooth();

  ball1 = new Ball(55);
  ball2 = new Ball(175);
  spiral1 = new Spiral(ball1.x, ball1.y, ball1.r);
  spiral1.radiusLimit = ball1.r;
}

void draw() {
  background(50);

  ball1.move();
  ball2.move();
  if (ball1.intersect(ball2)) {
    ball1.highlight();
    ball2.highlight();
  }

  ball1.display();
  ball2.display();
  spiral1.display();

  spiral1.x = ball1.x;
  spiral1.y = ball1.y;

}

class Ball {

  int redc = 0;
  
  float r;
  float x,y;
  float xspeed,yspeed;
  color c = color(200,200);

  Ball(float tempR) {
    r = tempR;
    x = random(width);
    y = random(height);
    xspeed = random(0,5);
    yspeed = random(0,5);
  }

  void move() {
    x += xspeed;
    y += yspeed;
    if (x > width || x < 0) {
      xspeed *= -1;
    }
    if (y > height || y < 0) {
      yspeed *= -1;
    }
  }

  
  void highlight() {

    
    c = color(redc,0,0,99);
    redc = redc+1;
    if (redc > 255) {
      redc = redc -1;
  }
  }

  void display() {
    stroke(0);
    strokeWeight(15);
    fill(c);
    ellipse(x,y,r*2,r*2);
    c = color(0,150,0);
  }

  
  boolean intersect(Ball other) {
    float distance = dist(x,y,other.x,other.y);
    if (distance < r + other.r) {
      return true;
    } else {
      return false;
    }
  }
}

class Spiral {

  float r = 0;
  float theta = 0;
  float radiusLimit =0;
  float x;
  float y;      

  Spiral(float tempX,float tempY,float tempradiusLimit) {

    x = tempX;
    y = tempY;
    tempradiusLimit = radiusLimit;
  }


  void display() {

    if (r < radiusLimit) {
      float x = r * cos(theta);
      float y = r * sin(theta);

      noStroke();
      fill(0);
      ellipse(x + ball1.x, y + ball1.y, 5, 5);

      theta += 0.11;
      r = r+.15;

    }  
  } 
}


Re: Another example from "Learning Processing"
Reply #8 - Aug 17th, 2009, 11:48am
 
Im guessing i need to create an array for the dots that make up the spiral. Yup  (now if only i was not at work)   Lips Sealed
Re: Another example from "Learning Processing"
Reply #9 - Aug 17th, 2009, 1:46pm
 
yup i thot so too.. i changed your spiral class to let the spiral build.. wad are u trying to achieve in the end?


Code:
Ball ball1;
Ball ball2;
Spiral spiral1;


void setup() {
size(1100,800);
smooth();

ball1 = new Ball(55);
ball2 = new Ball(175);
spiral1 = new Spiral(ball1.x, ball1.y, ball1.r);
spiral1.radiusLimit = ball1.r;


}

void draw() {
background(50);

ball1.move();
ball2.move();
if (ball1.intersect(ball2)) {
ball1.highlight();
ball2.highlight();
}

ball1.display();
ball2.display();


spiral1.display();


}

class Ball {

int redc = 0;

float r;
float x,y;
float xspeed,yspeed;
color c = color(200,200);

Ball(float tempR) {
r = tempR;
x = random(width);
y = random(height);
xspeed = random(0,5);
yspeed = random(0,5);
}

void move() {
x += xspeed;
y += yspeed;
if (x > width || x < 0) {
xspeed *= -1;
}
if (y > height || y < 0) {
yspeed *= -1;
}
}


void highlight() {


c = color(redc,0,0,99);
redc = redc+1;
if (redc > 255) {
redc = redc -1;
}
}

void display() {
stroke(0);
strokeWeight(15);
fill(c);
ellipse(x,y,r*2,r*2);
c = color(0,150,0);
}


boolean intersect(Ball other) {
float distance = dist(x,y,other.x,other.y);
if (distance < r + other.r) {
return true;
} else {
return false;
}
}
}

class Spiral {

float r = 0;
float theta = 0;
float radiusLimit = 0;
float[] x= {0};
float[] y= {0};

Spiral(float tempX,float tempY,float tempradiusLimit) {

x[0] = tempX;
y[0] = tempY;
radiusLimit =tempradiusLimit;
}


void display() {

if (r <= radiusLimit)

{
float _x = r * cos(theta);
float _y = r * sin(theta);

x=append(x,_x);
y=append(y,_y);

noStroke();
fill(0);

for(int i=0;i<x.length;i++) ellipse(x[i] + ball1.x, y[i] + ball1.y, 5, 5);

theta += 0.11;
r = r+.15;

}

}
}
Re: Another example from "Learning Processing"
Reply #10 - Aug 17th, 2009, 2:21pm
 
tavern wrote on Aug 17th, 2009, 1:46pm:
yup i thot so too.. i changed your spiral class to let the spiral build.. wad are u trying to achieve in the end




Awesome !!!  Thanks - now all i have to do when i get home is look at how you did it and learn.

I dont know where this is going, just messing with some ideas and they keep merging and expanding...

Functionality that i would like to add is to be able to capture the smaller ball under certian conditions or release it.. have the spirals loop or start and stop or change up a bit...  turn the ball class into an array.  ect ect ect...

(Hey did you notice there is a spare ellipse from the spiral floating down and to the right a ways from the main spiral)
Re: Another example from "Learning Processing"
Reply #11 - Aug 17th, 2009, 2:40pm
 
haha yea i noticed that extra dot when the small ball is higher up.. no idea where that came from.. i tried to create a new spiral2 object for ball2 and it didnt work too.. so maybe you might wana look into that as well.. its 5.36am now and im going to bed.. good luck!
Page Index Toggle Pages: 1