Trim the End of these Beziers

edited February 2016 in Questions about Code

Hi, I have this sketch that I wrote to generate a sort of wobbly circle. I'm using beziers to draw a continuous curve through the points. Only thing is, I would like to trim the ends of the beziers so that I'm only left with the middle section. Is this possible, or is there a better way of drawing that curve through control points?

Tagged:

Answers

  • edited February 2016

  • edited February 2016
    float step = .01;    // resolution of lines
    float r = 300;     // radius
    
    int opacity = 0;
    
    float a = 1;
    float b = 1;
    
    float x1;
    float y1;
    float x2;
    float y2;
    float theta1;
    float rR;
    float[] bez = {0, 0, 0, 0};
    
    int count;
    
    float xa;
    float xb;
    float xc;
    float xd;
    float xe;
    float xf;
    float xg;
    float xh;
    float xi;
    float xj;
    float xk;
    float xl;
    float xm;
    float xn;
    float xo;
    float xp;
    float xq;
    
    float ya;
    float yb;
    float yc;
    float yd;
    float ye;
    float yf;
    float yg;
    float yh;
    float yi;
    float yj;
    float yk;
    float yl;
    float ym;
    float yn;
    float yo;
    float yp;
    float yq;
    
    void setup() {
      //size(1080, 720);
      fullScreen();
      background(0);
      stroke(255);
      noFill();
      strokeWeight(1);
      smooth();
      frameRate(5);
    }
    
    
    
    void draw() {
    
      background(0);
    
      // the center point of the circle
      float h = width/2;    
      float k = height/2;
    
      //step = random(.01,.5);
      step = .45;
    
      rR = 1;
    
      for (float theta = 0; theta <= 2*PI; theta += step) {
    
        float R = random(-30, 30);
        //rR = rR + R;
        //r = r + R;
    
        //bez[0] = x2;
        //bez[1] = y2;
    
        x1 = h + (r+R)*cos(theta);
        y1 = k + (r+R)*sin(theta);
        //line(x1, y1, x2, y2);
        //ellipse(x, y, s, s);
        //ellipse(x1, y1, s, s);
        theta1 = theta;
        theta1 = theta1 - step;
    
        x2 = x1;
        y2 = y1;
        saveCoordinates();
      }
      count = 0;
    
      drawBezier();
      //drawCurve();
    
      text(int(frameRate) + " fps", 30, 30, 30);
    }
    
    
    
    void saveCoordinates() {
    
      count += 1;
      switch(count) {
      case 1: 
        xa = x1;
        ya = y1;
        break;
      case 2: 
        xb = x1;
        yb = y1;
        break;
      case 3: 
        xc = x1;
        yc = y1;
        break;
      case 4: 
        xd = x1;
        yd = y1;
        break;
      case 5: 
        xe = x1;
        ye = y1;
        break;
      case 6: 
        xf = x1;
        yf = y1;
        break;
      case 7: 
        xg = x1;
        yg = y1;
        break;
      case 8: 
        xh = x1;
        yh = y1;
        break;
      case 9: 
        xi = x1;
        yi = y1;
        break;
      case 10:
        xj = x1;
        yj = y1;
        break;
      case 11:
        xk = x1;
        yk = y1;
        break;
      case 12:
        xl = x1;
        yl = y1;
        break;
      case 13:
        xm = x1;
        ym = y1;
        break;
      case 14:
        xn = x1;
        yn = y1;
        break;
      case 15:
        xo = x1;
        yo = y1;
        println("here2");
        break;
      case 16:
        xp = x1;
        yp = y1;
        println("here1");
        break;
      case 17:
        xq = x1;
        yq = y1;
        println("here");
        break;
      default:
        println("excess case");
        break;
      }
    }
    
    void drawBezier() {
    
      bezier(xa, ya, xb, yb, xc, yc, xd, yd);
      bezier(xb, yb, xc, yc, xd, yd, xe, ye);
      bezier(xc, yc, xd, yd, xe, ye, xf, yf);
      bezier(xd, yd, xe, ye, xf, yf, xg, yg);
      bezier(xe, ye, xf, yf, xg, yg, xh, yh);
      bezier(xf, yf, xg, yg, xh, yh, xi, yi);
      bezier(xg, yg, xh, yh, xi, yi, xj, yj);
      bezier(xh, yh, xi, yi, xj, yj, xk, yk);
      bezier(xi, yi, xj, yj, xk, yk, xl, yl);
      bezier(xj, yj, xk, yk, xl, yl, xm, ym);
      bezier(xk, yk, xl, yl, xm, ym, xn, yn);
      bezier(xl, yl, xm, ym, xn, yn, xa, ya);
      bezier(xm, ym, xn, yn, xa, ya, xb, yb);
      bezier(xn, yn, xa, ya, xb, yb, xc, yc);
    }
    
    void drawCurve() {
    
      curve(xa, ya, xb, yb, xc, yc, xd, yd);
      curve(xb, yb, xc, yc, xd, yd, xe, ye);
      curve(xc, yc, xd, yd, xe, ye, xf, yf);
      curve(xd, yd, xe, ye, xf, yf, xg, yg);
      curve(xe, ye, xf, yf, xg, yg, xh, yh);
      curve(xf, yf, xg, yg, xh, yh, xi, yi);
      curve(xg, yg, xh, yh, xi, yi, xj, yj);
      curve(xh, yh, xi, yi, xj, yj, xk, yk);
      curve(xi, yi, xj, yj, xk, yk, xl, yl);
      curve(xj, yj, xk, yk, xl, yl, xm, ym);
      curve(xk, yk, xl, yl, xm, ym, xn, yn);
      curve(xl, yl, xm, ym, xn, yn, xa, ya);
      curve(xm, ym, xn, yn, xa, ya, xb, yb);
      curve(xn, yn, xa, ya, xb, yb, xc, yc);
    }
    
Sign In or Register to comment.