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:
- ball[] balls;
- stick[] sticks;
- int stickLen = 80;
- int nBalls = 10;
- int nSticks = nBalls-1;
- void setup() {
- size(1280, 720);
- smooth();
- frameRate(1);
- balls = new ball[nBalls];
- sticks = new stick[nSticks];
- for (int i = 0; i < nBalls; 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]);
- }
- }
- }
- void draw() {
- background(255);
- for (int i = 0; i < nSticks; i++) {
- sticks[i].update();
- sticks[i].display();
- }
- }
- class ball {
- color ballColor;
- float radius;
- PVector location;
- PVector oldLocation;
- PVector nudge;
- ball(PVector loc, float r) {
- location = loc;
- radius = r;
- ballColor = color(0);
- oldLocation = location.get();
- nudge = new PVector(random(1, 3), random(1, 3));
- location.add(nudge);
- }//ball()
- void display() {
- stroke(1);
- noFill();
- ellipse(location.x, location.y, 2*radius, 2*radius);
- }
- void move() {
- PVector temp = location.get();
- location.x += (location.x-oldLocation.x);
- location.y += (location.y-oldLocation.y);
- oldLocation.set(temp);
- stroke(10);
- line(location.x, location.y, oldLocation.x, oldLocation.y);
- bounce();
- }//move
- void bounce() {
- if (location.x > (width-radius)) {
- location.x = width-radius;
- oldLocation.x = location.x;
- location.x -= nudge.x;
- }
- if (location.x < radius) {
- location.x = radius;
- oldLocation.x = location.x;
- location.x += nudge.x;
- }
- if (location.y > (height-radius)) {
- location.y = height-radius;
- oldLocation.y = location.y;
- location.y -= nudge.y;
- }
- if (location.y < radius) {
- location.y = radius;
- oldLocation.y = location.y;
- location.y += nudge.y;
- }
- }
- }//end of class ball
- class stick {
- ball b1, b2;
- float r;
- stick(ball b1, ball b2) {
- this.b1 = b1;
- this.b2 = b2;
- //compute the length of the stick
- //same as the intial distance between two balls
- r = b1.location.dist(b2.location);
- }
- void display() {
- b1.display();
- b2.display();
- stroke(255, 0, 0);
- strokeWeight(5);
- line(b1.location.x, b1.location.y, b2.location.x, b2.location.y);
- }
- void update() {
- b1.move();
- b2.move();
- constrainLength();
- }
- void constrainLength() {
- float k = 0.1;
- //delta = atstumas
- //r = distance
- PVector delta = PVector.sub(b2.location, b1.location);
- float deltaLength = delta.mag();
- float d = ( (deltaLength - r) / deltaLength);
- b1.location.x += delta.x * k * d/2;
- b1.location.y += delta.y * k * d/2;
- b2.location.x -= delta.x * k * d/2;
- b2.location.y -= delta.y * k * d/2;
- }//constrainLength()
- }//end of stick
1