Advertisement
xeromino

bez2

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