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.
Page Index Toggle Pages: 1
Type on a path? (Read 494 times)
Type on a path?
Oct 5th, 2009, 12:58pm
 
New to processing. Learning through manipulating existing code. In this instance, I need help to input a line of type ie. "Hello World" in place of the drawn line. Is this possible? Thanks.

Here's the code:

final int NUMBER_OF_FISH = 500;
final float DISTANCE_TO_CENTER_THRESHHOLD = 25.0;
final float PREFER_DISTANCE_TO_CENTER = 20.0;
final float DISTANCE_TO_CENTER_SPEED_FACTOR = 100.0;
final float DISTANCE_TO_CLOSEST_FISH_THRESHHOLD = 15.0;
final float PREFER_DISTANCE_TO_CLOSEST_FISH = 20.0;
final float DISTANCE_TO_CLOSEST_FISH_SPEED_FACTOR = 5.0;
final float PREFER_AVERAGE_DIRECTION = 6.0;
final float TURN_RATE_LIMIT = PI/20.0;
final float SPEED_LIMIT = 3.0;
final float CLOSENESS = 150.0;

boolean followFood = false;

class Fish {
 float direction;
 float speed;
 float position[];
 int index;
 
 Fish( int index ){
   this.position = new float[2];
   this.position[0] = random(0,width);
   this.position[1] = random(0,height);
   this.speed = 1;
   this.direction = random(-PI,PI);
   this.index = index;
 }
 
 void draw(){
   pushMatrix();
   translate(position[0],position[1]);
   rotate(direction);
   stroke(0,255,0);
   line(0,0,100,0);
   popMatrix();
 }
 
 void move( Fish[] allOtherFishes ){
   float[] center = {0,0};
   float distanceToClosestFish = 1000;
   int closestFishIndex = 0;
   float averageDirection = 0;
   int count = 0;
   float turnRate = 0;
   
   for( int i=0;i<allOtherFishes.length;i++ ){
     float distanceToFish = dist(allOtherFishes[i].position[0], allOtherFishes[i].position[1], this.position[0], this.position[1]);
     
     if( i != this.index && distanceToFish < CLOSENESS ){
       center[0] += allOtherFishes[i].position[0];
       center[1] += allOtherFishes[i].position[1];
       averageDirection += allOtherFishes[i].direction;
       count++;
       
       if (distanceToFish < distanceToClosestFish){
         distanceToClosestFish = distanceToFish;
         closestFishIndex = i;
       }
     }
   }  
   if ( count != 0 ){
     center[0] /= count;
     center[1] /= count;
     averageDirection /= count;
     
     speed = 1;
     
     float distanceToCenter = dist( center[0], center[1], this.position[0], this.position[1] );
     
       if ( distanceToCenter > DISTANCE_TO_CENTER_THRESHHOLD ){
       float angleToCenter = atan2( this.position[1]-center[1], this.position[0]-center[0] );
       
       turnRate += (angleToCenter-direction)/PREFER_DISTANCE_TO_CENTER;
       speed = distanceToCenter/DISTANCE_TO_CENTER_SPEED_FACTOR;
     }
     
     /*if (distanceToCenter < 20){
       float angleToCenter = atan2( this.position[1]-center[1], this.position[0]-center[0] );
       
       direction -= (angleToCenter-direction)/10.0;
       speed = 1;
     }*/
     
     /*if (distanceToClosestFish > 15) {
       float angleToCenter = atan2( this.position[1]-allOtherFishes[closestFishIndex].position[1], this.position[0]-allOtherFishes[closestFishIndex].position[0] );
       
       turnRate += (angleToCenter-direction)/20.0;
     }*/
     
     if (distanceToClosestFish < DISTANCE_TO_CLOSEST_FISH_THRESHHOLD) {
       float angleToCenter = atan2( this.position[1]-allOtherFishes[closestFishIndex].position[1], this.position[0]-allOtherFishes[closestFishIndex].position[0] );
       
       turnRate -= (angleToCenter-direction)/PREFER_DISTANCE_TO_CLOSEST_FISH;
       speed = DISTANCE_TO_CLOSEST_FISH_SPEED_FACTOR/distanceToClosestFish;
     }
     
     turnRate += (averageDirection-direction)/PREFER_AVERAGE_DIRECTION;
     
     if( followFood ) {
       float angleToFood = atan2( this.position[1]-mouseY, this.position[0]-mouseX );
       turnRate += (angleToFood-direction)/2.0;
     }
   }
   
   if ( speed > SPEED_LIMIT ) speed = SPEED_LIMIT;
   if( turnRate > TURN_RATE_LIMIT ) turnRate = TURN_RATE_LIMIT;
   if( turnRate < -TURN_RATE_LIMIT ) turnRate = -TURN_RATE_LIMIT;
   
   direction += turnRate;

   if (direction > PI) direction -= 2*PI;
   if (direction < -PI) direction += 2*PI;
   
   position[0] -= cos(this.direction)*speed;
   position[1] -= sin(this.direction)*speed;
   
   if (position[0] > width) position[0] = 0;
   if (position[0] < 0) position[0] = width;
   if (position[1] > height) position[1] = 0;
   if (position[1] < 0) position[1] = height;
 }
}

Fish[] fishes;

void setup(){
 size(1024,768);
 fishes = new Fish[NUMBER_OF_FISH];
 
 for( int i=0;i<fishes.length;i++ ){
   fishes[i] = new Fish(i);
 }
 
 stroke(0);
}

void draw(){
 background(0,0,0);
 
 for( int i=0;i<fishes.length;i++ )
   fishes[i].move( fishes );
 
 for( int i=0;i<fishes.length;i++ )
   fishes[i].draw();
 
 float angleToFood = atan2( fishes[0].position[1]-mouseY, fishes[0].position[0]-mouseX );
}

void mousePressed(){
 followFood = !followFood;
}
Re: Type on a path?
Reply #1 - Oct 5th, 2009, 2:08pm
 
thats pretty easy, as everything was well prepared to just put some text in there. take a look at text() to make it youself next time :
http://processing.org/reference/text_.html

But its getting really really slow. i reduced it from 500 to 50 and its still slow. So maybe take a look at the Type Libaries there might be something that could improve alot of text...


Code:
final int NUMBER_OF_FISH = 50;
final float DISTANCE_TO_CENTER_THRESHHOLD = 25.0;
final float PREFER_DISTANCE_TO_CENTER = 20.0;
final float DISTANCE_TO_CENTER_SPEED_FACTOR = 100.0;
final float DISTANCE_TO_CLOSEST_FISH_THRESHHOLD = 15.0;
final float PREFER_DISTANCE_TO_CLOSEST_FISH = 20.0;
final float DISTANCE_TO_CLOSEST_FISH_SPEED_FACTOR = 5.0;
final float PREFER_AVERAGE_DIRECTION = 6.0;
final float TURN_RATE_LIMIT = PI/20.0;
final float SPEED_LIMIT = 3.0;
final float CLOSENESS = 150.0;

boolean followFood = false;

class Fish {
float direction;
float speed;
float position[];
int index;

Fish( int index ){
  this.position = new float[2];
  this.position[0] = random(0,width);
  this.position[1] = random(0,height);
  this.speed = 1;
  this.direction = random(-PI,PI);
  this.index = index;
}


void draw(){
  pushMatrix();
  translate(position[0],position[1]);
  rotate(direction);
  stroke(0,255,0);
  text("Hello Mister Fish!",0,0);
  line(0,0,100,0);
  popMatrix();
}

void move( Fish[] allOtherFishes ){
  float[] center = {0,0};
  float distanceToClosestFish = 1000;
  int closestFishIndex = 0;
  float averageDirection = 0;
  int count = 0;
  float turnRate = 0;
 
  for( int i=0;i<allOtherFishes.length;i++ ){
    float distanceToFish = dist(allOtherFishes[i].position[0], allOtherFishes[i].position[1], this.position[0], this.position[1]);
   
    if( i != this.index && distanceToFish < CLOSENESS ){
center[0] += allOtherFishes[i].position[0];
center[1] += allOtherFishes[i].position[1];
averageDirection += allOtherFishes[i].direction;
count++;

if (distanceToFish < distanceToClosestFish){
  distanceToClosestFish = distanceToFish;
  closestFishIndex = i;
}
    }
  }  
  if ( count != 0 ){
    center[0] /= count;
    center[1] /= count;
    averageDirection /= count;
   
    speed = 1;
   
    float distanceToCenter = dist( center[0], center[1], this.position[0], this.position[1] );
   
if ( distanceToCenter > DISTANCE_TO_CENTER_THRESHHOLD ){
float angleToCenter = atan2( this.position[1]-center[1], this.position[0]-center[0] );

turnRate += (angleToCenter-direction)/PREFER_DISTANCE_TO_CENTER;
speed = distanceToCenter/DISTANCE_TO_CENTER_SPEED_FACTOR;
    }
   
    /*if (distanceToCenter < 20){
float angleToCenter = atan2( this.position[1]-center[1], this.position[0]-center[0] );

direction -= (angleToCenter-direction)/10.0;
speed = 1;
    }*/
   
    /*if (distanceToClosestFish > 15) {
float angleToCenter = atan2( this.position[1]-allOtherFishes[closestFishIndex].position[1], this.position[0]-allOtherFishes[closestFishIndex].position[0] );

turnRate += (angleToCenter-direction)/20.0;
    }*/
   
    if (distanceToClosestFish < DISTANCE_TO_CLOSEST_FISH_THRESHHOLD) {
float angleToCenter = atan2( this.position[1]-allOtherFishes[closestFishIndex].position[1], this.position[0]-allOtherFishes[closestFishIndex].position[0] );

turnRate -= (angleToCenter-direction)/PREFER_DISTANCE_TO_CLOSEST_FISH;
speed = DISTANCE_TO_CLOSEST_FISH_SPEED_FACTOR/distanceToClosestFish;
    }
   
    turnRate += (averageDirection-direction)/PREFER_AVERAGE_DIRECTION;
   
    if( followFood ) {
float angleToFood = atan2( this.position[1]-mouseY, this.position[0]-mouseX );
turnRate += (angleToFood-direction)/2.0;
    }
  }
 
  if ( speed > SPEED_LIMIT ) speed = SPEED_LIMIT;
  if( turnRate > TURN_RATE_LIMIT ) turnRate = TURN_RATE_LIMIT;
  if( turnRate < -TURN_RATE_LIMIT ) turnRate = -TURN_RATE_LIMIT;
 
  direction += turnRate;

  if (direction > PI) direction -= 2*PI;
  if (direction < -PI) direction += 2*PI;
 
  position[0] -= cos(this.direction)*speed;
  position[1] -= sin(this.direction)*speed;
 
  if (position[0] > width) position[0] = 0;
  if (position[0] < 0) position[0] = width;
  if (position[1] > height) position[1] = 0;
  if (position[1] < 0) position[1] = height;
}
}

Fish[] fishes;

PFont font;

void setup(){
size(1024,768);
fishes = new Fish[NUMBER_OF_FISH];
font = createFont("Arial",12);
textFont(font, 12);

for( int i=0;i<fishes.length;i++ ){
  fishes[i] = new Fish(i);
}

stroke(0);
}

void draw(){
background(0,0,0);

for( int i=0;i<fishes.length;i++ )
  fishes[i].move( fishes );

for( int i=0;i<fishes.length;i++ )
  fishes[i].draw();

float angleToFood = atan2( fishes[0].position[1]-mouseY, fishes[0].position[0]-mouseX );
}

void mousePressed(){
followFood = !followFood;
}
Re: Type on a path?
Reply #2 - Oct 6th, 2009, 12:23am
 
Many Thanks!
Page Index Toggle Pages: 1