Connecting point to previous point in array

Hi all,

I would like to connect the points in this circle to form a complete ring, having trouble though, this is what i have:

int numPoints = 50;
float angle, x, y;
float[] x2;
float[] y2;

void setup() {
  size(600, 300);
  smooth();
  noFill();
}

void draw() {
  background(#ffffff);
  pushMatrix();
  translate(width/2, height/2);

  angle = TWO_PI/numPoints;
  for (int i = 0; i < numPoints; i++) {
    x = cos(angle*i)*numPoints;
    y = sin(angle*i)*numPoints;
    x2 = new float[numPoints];
    y2 = new float[numPoints];
    x2[i] =+ x;
    y2[i] =+ y;
    point(x,y);
    // not sure what to do here
    line(x,y, x2[i], y2[i]);
  }
  popMatrix();
}
Tagged:

Answers

  • There's probably a way better solution but I enjoyed playing with it:

    int numPoints = 50;
    float angle;
    float[] x;
    float[] y;
    float[] x2;
    float[] y2;
    
    
    void setup() {
      size(600, 300);
      smooth();
      noFill();
    }
    
    void draw() {
      background(#ffffff);
      pushMatrix();
      translate(width/2, height/2);
    
      x = new float[numPoints];
      y = new float[numPoints];
      x2 = new float[numPoints];
      y2 = new float[numPoints];
    
      angle = TWO_PI/numPoints;
    
      for (int i = 0; i < numPoints; i++) {
    
        x[i] = sin(angle*i)*numPoints;
        y[i] = cos(angle*i)*numPoints;
    
    
        point(x[i], y[i]);
    
    
        if (x[i] != 0) {
          line(x[i], y[i], x2[i], y2[i]);
        }
    
        if (i < numPoints-1 ) {
          x2[i+1] += x[i];
          y2[i+1] += y[i];
        } 
        else {
    
          line(x[numPoints-1], y[numPoints-1], x[1], y[1]);
        }
      }
      popMatrix();
    }
    
  • edited November 2013 Answer ✓

    no need for arrays

      for (int i = 0; i < numPoints; i++) { 
        x1 = sin(angle * i);
        y1 = cos(angle * i);
        x2 = sin(angle * (i + 1));  // next point
        y2 = cos(angle * (i + 1));  // next point
        line(x1, y1, x2, y2);
      }
    

    (takes advantage of the circular nature of sin / cos - cos(360) = cos(0), does mean you're calculating each point twice though)

    if you do have an array of points (precalculated in setup(), perhaps) you use the % operator to ensure the index wraps at the end

      for (int i = 0; i < numPoints; i++) {
        int k = (i + 1) % numPoints; // next point, with wrapping
        line(x[i], y[i], x[k], y[k]);
      }
    

    because numPoints % numPoints = 0

    alternatively, if you use beginShape(); and vertex(x[i], y[i]) then you can use endShape(CLOSE); which will close the shape automatically. see endShape in reference.

  • edited November 2013

    Modification on no-array version provided by @koogs that avoids duplicate calculations.

    x1 = sin(0);
    y1 = cos(0);
    for (int i = 1; i < numPoints; i++) {
      x2 = sin(angle * i); 
      y2 = cos(angle * i);
      line(x1, y1, x2, y2);
      x1 = x2;
      y1 = y2;
    }
    
  • edited November 2013

    An even compacter full version: :P

    // forum.processing.org/two/discussion/1003/
    // connecting-point-to-previous-point-in-array
    
    final short NUM = 0100;
    final float ANG = TAU/NUM;
    
    size(200, 200);
    smooth(4);
    noLoop();
    
    stroke(#0000FF);
    strokeWeight(1.5);
    strokeJoin(ROUND);
    strokeCap(ROUND);
    
    background(-1);
    translate(width>>1, height>>1);
    
    float x1 = sin(0) * NUM, y1 = cos(0) * NUM;
    
    for (int i = 1; i != NUM + 1; ++i) {
      final float x2 = sin(i*ANG) * NUM, y2 = cos(i*ANG) * NUM;
      line(x1, y1, x1 = x2, y1 = y2);
    }
    
  • the point wasn't to be compact, it was to be clear.

Sign In or Register to comment.