Advertisement
xeromino

bezier

Mar 28th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. boolean first = true;
  2. int col = 0;
  3. int step = 5;
  4. int NumCurves = 5;
  5. int NumPoints = (3*NumCurves)+1;  // 3 points per curve, plus 1 at the start
  6. PVector[] curvePoints = new PVector[NumPoints];
  7. int Border = 50;  // how far to keep endpoints from the edges. The curve can still swirl off-screen.
  8.  
  9. void setup() {
  10.   size(1280, 720);
  11.   background(#202020);
  12.   colorMode(HSB, 360, 100, 100);
  13.   //stroke(0,100);
  14.   noFill();
  15.   frameRate(10);
  16. }
  17.  
  18. void draw() {
  19.   drawStuff();
  20. }
  21.  
  22. void drawStuff() {
  23.   drawCurve();
  24.   first = false;
  25.   col += 10;
  26. }
  27.  
  28.  
  29. void keyPressed() {
  30.   if (key=='s') {
  31.     saveFrame("image-#####.tif");
  32.   }
  33.   else {
  34.     background(#202020);
  35.     col = 0;
  36.     initCurve();
  37.   }
  38. }
  39.  
  40. PVector getBezXY(int i) {
  41.   return(new PVector(random(Border, width-Border), random(Border, height-Border)));
  42. }
  43.  
  44. void drawCurve() {
  45.   stroke(col%360, 90, 90, 50);
  46.   if (first) {
  47.     initCurve();
  48.   }
  49.   else {
  50.     alterCurve();
  51.   }
  52.   beginShape();
  53.   vertex(curvePoints[0].x, curvePoints[0].y);
  54.   for (int i=0; i<NumCurves; i++) {
  55.     int p = 1+(i*3);  // index of starting point
  56.     bezierVertex(curvePoints[p].x, curvePoints[p].y, curvePoints[p+1].x, curvePoints[p+1].y, curvePoints[p+2].x, curvePoints[p+2].y);
  57.   }
  58.   endShape();
  59.   /*
  60.   for (int i=0; i<curvePoints.length; i++) {
  61.    ellipse(curvePoints[i].x, curvePoints[i].y, 10, 10);
  62.    }
  63.    */
  64. }
  65.  
  66. void initCurve() {
  67.   for (int i=0; i<NumCurves; i++) {
  68.     if (i==0) { // first curve, so no continuity from before. Fill 4 points
  69.       for (int j=0; j<4; j++) {
  70.         curvePoints[j] = getBezXY(i);
  71.       }
  72.     }
  73.     else { // continuation of curve
  74.       int p = 1+(i*3);  // index of starting point
  75.       // first point is a control point. We need to follow the continuity rules for
  76.       // smoothness. Find the difference between the preceding two points, and add
  77.       // that to the last point of the previous curve
  78.       PVector diffcurvePoints = curvePoints[p-1].get();  // get the previous point
  79.       diffcurvePoints.sub(curvePoints[p-2]);             // and find the vector to it from the point before
  80.       curvePoints[p] = curvePoints[p-1].get();           // put the previous point into curvePoints[p]
  81.       curvePoints[p].add(diffcurvePoints);               // and add in the difference we just found
  82.       // the next points can be anywhere
  83.       curvePoints[p+1] = getBezXY(i);
  84.       curvePoints[p+2] = getBezXY(i);
  85.     }
  86.   }
  87. }
  88.  
  89. void alterCurve() {
  90.   for (int i=0; i<curvePoints.length; i++) {
  91.     curvePoints[i].x += random(-step, step);
  92.     curvePoints[i].y += random(-step, step);
  93.   }
  94.   ///*
  95.   for (int i=0; i<curvePoints.length; i++) {
  96.     if (i> 3 && (i-1)%3==0 ) {
  97.       PVector diffcurvePoints = curvePoints[i-1].get();  // get the previous point
  98.       diffcurvePoints.sub(curvePoints[i-2]);             // and find the vector to it from the point before
  99.       curvePoints[i] = curvePoints[i-1].get();           // put the previous point into curvePoints[p]
  100.       curvePoints[i].add(diffcurvePoints);
  101.     }
  102.   }
  103.   //*/
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement