Loading...
Logo
Processing Forum
Hi,

I have a small question with arrays. 
I have 10 ellipses drawn in a circle. Each ellipse is connected by a string.

How to write the last string in order to connect the last ellipse with the first one?

Thank you in advance:)

The code:

Copy code
  1. ball[] balls;
  2. stick[] sticks;


  3. int stickLen = 80;
  4. int nBalls = 10;
  5. int nSticks = nBalls-1;

  6. void setup() {

  7.   size(1280, 720);
  8.   smooth();
  9.   frameRate(1);

  10.   balls = new ball[nBalls];
  11.   sticks = new stick[nSticks];

  12.   for (int i = 0; i < nBalls; i++) {

  13.     balls[i] = new ball(new PVector(sin(radians(i*36))*100+width/2, cos(radians(i*36))*100+height/2), 10);

  14.     if (i > 0) {
  15.       sticks[i-1] = new stick(balls[i-1], balls[i]);
  16.     }
  17.     
  18.     
  19.   }
  20. }

  21. void draw() {

  22.   background(255);

  23.   for (int i = 0; i < nSticks; i++) {


  24.     sticks[i].update();
  25.     sticks[i].display();
  26.   }
  27. }
  28. class ball {

  29.   color ballColor;
  30.   float radius;

  31.   PVector location;
  32.   PVector oldLocation;
  33.   PVector nudge;

  34.   ball(PVector loc, float r) {
  35.     location = loc;
  36.     radius = r;
  37.     ballColor = color(0);

  38.     oldLocation = location.get();
  39.     nudge = new PVector(random(1, 3), random(1, 3));
  40.     location.add(nudge);
  41.   }//ball()

  42.   void display() {

  43.     stroke(1);
  44.     noFill();
  45.     ellipse(location.x, location.y, 2*radius, 2*radius);
  46.   }

  47.   void move() {

  48.     PVector temp = location.get();
  49.     location.x += (location.x-oldLocation.x);
  50.     location.y += (location.y-oldLocation.y);
  51.     oldLocation.set(temp);

  52.     stroke(10);
  53.     line(location.x, location.y, oldLocation.x, oldLocation.y);

  54.     bounce();
  55.   }//move


  56.     void bounce() {

  57.     if (location.x > (width-radius)) {
  58.       location.x = width-radius;
  59.       oldLocation.x = location.x;
  60.       location.x -= nudge.x;
  61.     }

  62.     if (location.x < radius) {
  63.       location.x = radius;
  64.       oldLocation.x = location.x;
  65.       location.x += nudge.x;
  66.     }

  67.     if (location.y > (height-radius)) {
  68.       location.y = height-radius;
  69.       oldLocation.y = location.y;
  70.       location.y -= nudge.y;
  71.     }

  72.     if (location.y < radius) {
  73.       location.y = radius;
  74.       oldLocation.y = location.y;
  75.       location.y += nudge.y;
  76.     }
  77.   }
  78. }//end of class ball
  79. class stick {

  80.   ball b1, b2;
  81.   float r;

  82.   stick(ball b1, ball b2) {

  83.     this.b1 = b1;
  84.     this.b2 = b2;

  85.     //compute the length of the stick
  86.     //same as the intial distance between two balls

  87.     r = b1.location.dist(b2.location);
  88.   }

  89.   void display() {

  90.     b1.display();
  91.     b2.display();

  92.     stroke(255, 0, 0);
  93.     strokeWeight(5);
  94.     line(b1.location.x, b1.location.y, b2.location.x, b2.location.y);
  95.   }

  96.   void update() {
  97.     b1.move();  
  98.     b2.move();
  99.     constrainLength();
  100.   }

  101.   void constrainLength() {

  102.     float k = 0.1;
  103.     //delta = atstumas
  104.     //r = distance
  105.     PVector delta = PVector.sub(b2.location, b1.location);

  106.     float deltaLength = delta.mag();
  107.     float d = ( (deltaLength - r) / deltaLength);

  108.     b1.location.x += delta.x * k * d/2;
  109.     b1.location.y += delta.y * k * d/2;
  110.     b2.location.x -= delta.x * k * d/2;
  111.     b2.location.y -= delta.y * k * d/2;
  112.   }//constrainLength()
  113. }//end of stick

Replies(2)

I've made it! 
Copy code
    /** 
     * Ball-Chained (v2.21)
     * by p3tr4s (2013/Aug)
     * mod GoToLoop
     * 
     * http://forum.processing.org/topic/
     * how-to-connect-last-ball-of-the-array-with-the-first-ball
     */
    
    final static int BALLS_NUM = 10;
    
    final Ball[]  balls  = new Ball[BALLS_NUM];
    final Stick[] sticks = new Stick[BALLS_NUM];
    
    final PVector tmp = new PVector();
    
    void setup() {
      size(1280, 720);
      smooth();
      frameRate(10);
    
      strokeWeight(5);
      noFill();
    
      for (int i = 0; i != BALLS_NUM; i++) {
        balls[i] = new Ball(new PVector
          (sin(radians(i*36))*100+width/2, cos(radians(i*36))*100+height/2), 10);
    
        if (i > 0)  sticks[i-1] = new Stick(balls[i-1], balls[i]);
      }
    
      sticks[BALLS_NUM - 1] = new Stick(balls[BALLS_NUM - 1], balls[0]);
    }
    
    void draw() {
      background(-1);
      for (Stick st: sticks)  st.action();
    }
    
    class Ball {
      final static color BALL_COLOR = 1, CHAIN_COLOR = 10;
    
      final float radius, diameter;
    
      final PVector location = new PVector();
      final PVector oldLocation = new PVector();
      final PVector nudge = new PVector(random(1, 3), random(1, 3));
    
      Ball(PVector loc, float r) {
        oldLocation.set(loc);
        location.set(loc);
        location.add(nudge);
        diameter = (radius = r) * 2;
      }
    
      void display() {
        stroke(BALL_COLOR);
        ellipse(location.x, location.y, diameter, diameter);
      }
    
      void move() {
        PVector.sub(location, oldLocation, tmp);
        oldLocation.set(location);
        location.add(tmp);
    
        stroke(CHAIN_COLOR);
        line(location.x, location.y, oldLocation.x, oldLocation.y);
    
        bounce();
      }
    
      void bounce() {
        if (location.x > (width - radius)) {
          oldLocation.x = location.x = width - radius;
          location.x -= nudge.x;
        }
    
        else if (location.x < radius) {
          oldLocation.x = location.x = radius;
          location.x += nudge.x;
        }
    
        if (location.y > (height - radius)) {
          oldLocation.y = location.y = height - radius;
          location.y -= nudge.y;
        }
    
        else if (location.y < radius) {
          oldLocation.y = location.y = radius;
          location.y += nudge.y;
        }
      }
    }
    
    class Stick {
    
      Ball b1, b2;
      float r;
    
      Stick(Ball b1, Ball b2) {
        this.b1 = b1;
        this.b2 = b2;
    
        r = b1.location.dist(b2.location);
      }
    
      void action() {
        update();
        display();
      }
    
      void display() {
        b1.display();
        b2.display();
    
        stroke(#FF0000);
        line(b1.location.x, b1.location.y, b2.location.x, b2.location.y);
      }
    
      void update() {
        b1.move();
        b2.move();
    
        constrainLength();
      }
    
      void constrainLength() {
        final float deltaLen = PVector.sub(b2.location, b1.location, tmp).mag();
        tmp.mult((deltaLen - r) / deltaLen / 5);
    
        b1.location.add(tmp);
        b2.location.sub(tmp);
      }
    }
    
    
Thank you,

I wrote this line ten times but getting errors.

Now I see that it had to be written outside the loop.

Good Night;)