freeze

edited June 2015 in Questions about Code

how to set the position so the yellow and green balls keep a fix distance with the grey? is there any way to achieve that...

do i need a calculation for this poisition (where to freeze)? or make the balls go into the space(gap) between the grey branch and freeze ?

ArrayList<Dot> dots = new ArrayList();
int frozen_red_counter;

color[] valid_colors = { 
  color(128), color(255, 128, 0), color(0, 255, 128)//, color(0,128,0), color(255,128,0)
};

int INITIAL_DOTS = 300;
int MAX_DOTS = 500;

void setup() {
  size(400, 400);
  int frozen_red_counter=0;
  for (int i=0; i<INITIAL_DOTS; i++) {
    dots.add(new Dot());
  }
  dots.get(0).x_position = width/2;
  dots.get(0).y_position = height/2;
  dots.get(0).is_frozen = true;
  dots.get(0).type = 0;
  noStroke();
}

void draw() {
  background(0);
  for (int i=0; i<dots.size (); dots.get(i++).draw());
}

void mousePressed () {
  if (key == 'e'){
  saveFrame("viruses-###.tif");
  }
}

    class Dot {
  float x_position;
  float y_position;
  float x_velocity;
  float y_velocity;
  int type;
  boolean is_frozen;
  int my_id;

  Dot() {
    x_position = 0;                            //random(width);
    y_position = 0;                            //random(height);
    x_velocity = random( -3, 3 );
    y_velocity = random( -3, 3 );
    type = int(random(valid_colors.length));
    is_frozen = false;
    my_id = dots.size();
  }

  void draw() {
    render();
    display();
  }

  void render() {
    // Draw this dot.
    if ( type == 0 ) {
      fill(valid_colors[0]);
      noStroke();
      ellipse(x_position, y_position, 8, 8);
    }
    if ( type == 1 ) {
      fill(valid_colors[1]);
      noStroke();
      ellipse(x_position, y_position, 8, 8);
    }

            if ( type == 2 ) {
      fill(valid_colors[2]);
      noStroke();
      ellipse(x_position, y_position, 8, 8);
    }
  }

  void display() {
    if (is_frozen) return;
    x_position += x_velocity + width;
    y_position += y_velocity + height;
    x_position %= width;
    y_position %= height;

    // If this dot should freeze...
    if (should_i_freeze()) {
      // Freeze it.
      is_frozen = true;
      // And also add a new dot if there's space for more.
      //      if ( dots.size() < MAX_DOTS ) { 
      //        dots.add(new Dot());
      //      }
    }
  }

  // This function determines if this dot should freeze.
  boolean should_i_freeze() {
    // Dots that are already frozen do not need to freeze again.
    if (is_frozen) { 
      return(false);
    }

          for (int i=0; i<dots.size (); i++) {
      // But only look at dots that are frozen...
      if (dots.get(i).is_frozen ) {
        // Now we find the distance between ourself and that frozen dot.
        float distance = dist(x_position, y_position, dots.get(i).x_position, dots.get(i).y_position); 

        // And if we are red and the other dot is also red...
        if (frozen_red_counter < 80) {  
          //control number of red
          if (type == 0 && distance <= 12 && dots.get(i).type == 0) {
            // We need to freeze this dot!
            frozen_red_counter++;
            return(true);
          }
        }

        if (frozen_red_counter >= 80 && frozen_red_counter <= 100) {                         //control number of grey andgreen
          // If we are non-red and touching a frozen red dot...
          if (type >= 1 && distance <= 12 && dots.get(i).type == 0) {
            // We need to freeze this dot!
            frozen_red_counter++;
            return(true);
          }

                      // If we are non-red and touching a non-red frozen dot...
          if (type >= 1 && distance <= 10 && dots.get(i).type >= 1) {
            // We need to freeze this dot!
            frozen_red_counter++;
            return(true);
          }
        }
      }
    }
    // If we've looked at all the other dots and determined that we are not near a frozen one, we do not need to freeze. 
    return(false);
  }
}

Answers

  • the problem is how to get rid of the overlapping

  • @GoToLoop,does this work...i will see ,thanks,anyhow.

  • still do not know how to do it, can anyone give me some idea?

  • Your colored (yellow, green) dots do not freeze until a certain number of grey dots have frozen.

    Once a certain number of grey dots has frozen, any colored dot that is already overlapping with a grey dot will freeze immediately.

    Could you possibly drawn the sort of result you are expecting? Post it here as an image.

  • edited July 2015

    i think the result will be like this...

    what i do not know is how can i 1.make the colored(yellow,green) dots at least keep some distance with the grey branch (as if repel).? 2.yellow and green stick closely to each other?(which i have done) 3.yellow and green dots look like embeded into the grey branch

    do i need to start a new code or change some behaviour of dots so that can achieve this result?

  • edited June 2015

    i am thinking about can i....avoid overlap by adjust distance between dots or when it's candidate to be frozen check if it's not overlap with other moving and then change eg. direction

  • leave the above image, you can make so that...make the grey dots form a river not like branch first..then the colored dots near to it and freeze ....(get rid of overlapping)..

  • yes, good ,thanks. but how can i do that.

  • i am trying to make so that. 1. grey dots collide the curve and freeze 2. then the color(green and yellow ) collide and freeze. is there any easier way to do the 1 one. (grey dot can recognise the curve)??

    ArrayList<Dot> dots = new ArrayList();
    int frozen_grey_counter;
    
    color[] valid_colors = { 
      color(128), color(255, 128, 0), color(0, 255, 128)
    };
    
    int INITIAL_DOTS = 200;
    int MAX_DOTS = 500;
    
    void setup() {
      size(300, 300);
      int frozen_grey_counter=0;
      for (int i=0; i<INITIAL_DOTS; i++) {
        dots.add(new Dot());
      }
    }
    
    void draw() {
      background(0);
      for (int i=0; i<dots.size (); dots.get(i++).draw());
      stroke(120);
      strokeWeight(1);
      noFill();
      bezier(185, 20, 10, 10, 90, 90, 15, 80 );
    }
    
       class Dot {
      float x_position;
      float y_position;
      float x_velocity;
      float y_velocity;
      int type;
      boolean is_frozen;
      int my_id;
    
      Dot() {
        x_position = 0;                            //random(width);
        y_position = 0;                            //random(height);
        x_velocity = random( -3, 3 );
        y_velocity = random( -3, 3 );
        type = int(random(valid_colors.length));
        is_frozen = false;
        my_id = dots.size();
      }
    
      void draw() {
        render();
        display();
      }
    
      void render() {
        // Draw this dot.
        if ( type == 0 ) {
          fill(valid_colors[0]);
          noStroke();
          ellipse(x_position, y_position, 8, 8);
        }
        if ( type == 1 ) {
          fill(valid_colors[1]);
          noStroke();
          ellipse(x_position, y_position, 8, 8);
        }
            if ( type == 2 ) {
          fill(valid_colors[2]);
          noStroke();
          ellipse(x_position, y_position, 8, 8);
        }
         }
    
      void display() {
        if (is_frozen) return;
        if (frozen_grey_counter <= 20 && type == 0) {
          x_position += x_velocity + width;
          y_position += y_velocity + height;
          x_position %= width;
          y_position %= height;
        }
        if (frozen_grey_counter >=20 && frozen_grey_counter <= 70 && type >= 1) {
          x_position += x_velocity + width ;
          y_position += y_velocity + height ;
          x_position %= width;
          y_position %= height;
        }
    
        // If this dot should freeze...
        if (should_i_freeze()) {
          // Freeze it.
          is_frozen = true;
    
          //      if ( dots.size() < MAX_DOTS ) { 
          //        dots.add(new Dot());
          //      }
        }
      }
    
           boolean should_i_freeze() {
        // Dots that are already frozen do not need to freeze again.
        if (is_frozen) { 
          return(false);
        }
    
        for (int i=0; i<dots.size (); i++) {
          // Now we find the distance between ourself and that frozen dot.
          float distance1 = dist(x_position, y_position, 185, 20); 
          if (frozen_grey_counter < 20) {  
            //control number of red
            if (type == 0 && distance1 <= 3.0  ) {
              frozen_grey_counter++;
              return(true);
            }
          }
    
          float distance2 = dist(x_position, y_position, 165, 20); 
          if (frozen_grey_counter < 80) {  
            //control number of red
            if (type == 0 && distance2 <= 3.0  ) {
              frozen_grey_counter++;
              return(true);
            }
          }
                float distance3= dist(x_position, y_position, 145, 20); 
          if (frozen_grey_counter < 80) {  
            //control number of red
            if (type == 0 && distance3 <= 3.0  ) {
              frozen_grey_counter++;
              return(true);
            }
          }
          float distance4 = dist(x_position, y_position, 125, 20); 
          if (frozen_grey_counter < 80) {  
            //control number of red
            if (type == 0 && distance4<= 3.0  ) {
              frozen_grey_counter++;
              return(true);
            }
          }
          float distance5 = dist(x_position, y_position, 105, 20); 
          if (frozen_grey_counter < 80) {  
            //control number of red
            if (type == 0 && distance5<= 3.0  ) {
              frozen_grey_counter++;
              return(true);
            }
          }
                float distance6 = dist(x_position, y_position, 85, 30); 
          if (frozen_grey_counter < 80) {  
            //control number of red
            if (type == 0 && distance6<= 3.0  ) {
              frozen_grey_counter++;
              return(true);
            }
          }
          float distance7 = dist(x_position, y_position, 65, 40); 
          if (frozen_grey_counter < 80) {  
            //control number of red
            if (type == 0 && distance7<= 3.0  ) {
              frozen_grey_counter++;
              return(true);
            }
          }
          float distance8 = dist(x_position, y_position, 55, 50); 
          if (frozen_grey_counter < 80) {  
            //control number of red
            if (type == 0 && distance8<= 3.0  ) {
              frozen_grey_counter++;
              return(true);
            }
          }
                   if (dots.get(i).is_frozen ) {
            // Now we find the distance between ourself and that frozen dot.
            float distance = dist(x_position, y_position, dots.get(i).x_position, dots.get(i).y_position); 
            if (frozen_grey_counter >= 20 && frozen_grey_counter <= 50) {                         //control number of grey and green
              // If we are non-red and touching a frozen red dot...
              if (type >= 1 && distance <= 15  && dots.get(i).type == 0) {
    
                // We need to freeze this dot!
                frozen_grey_counter++;
                return(true);
              }
    
              // If we are non-red and touching a non-red frozen dot...
              if (type >= 1 && distance <= 10 && dots.get(i).type >= 1) {
                // We need to freeze this dot!
                frozen_grey_counter++;
                return(true);
              }
            }
          }
        } 
        // If we've looked at all the other dots and determined that we are not near a frozen one, we do not need to freeze. 
        return(false);
      }
    }
    
Sign In or Register to comment.