Advertisement
MrMusAddict

Bezier Curve in Processing 3.2.3

Feb 21st, 2017
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. //Created by MrMusAddict
  2. //3:00 PM February 21, 2017
  3. //Inspired by https://www.reddit.com/r/processing/comments/5unt8u/i_made_this_b%C3%A9zier_curve/
  4.  
  5. int maxDots = 4; //minimum 3, theoretically no maximum
  6. int resolution = 1000;
  7.  
  8. ArrayList<Dot> dots = new ArrayList<Dot>();
  9. int counter = 1;
  10. Path trail = new Path();
  11.  
  12. Line[][]  lines;
  13.  
  14. void setup() {
  15.   size(600, 600);
  16.  
  17.   lines = new Line[maxDots][maxDots];
  18.  
  19.   //add dots
  20.   for (int i = 0; i < maxDots; i++) {
  21.     dots.add(new Dot(random(width), random(height)));
  22.   }
  23.  
  24.   //add lines & tracing dot
  25.   if (maxDots >= 3) {
  26.     for (int i = 0; i < maxDots; i++) {
  27.       //add stationary lines
  28.       if (i == 0) {
  29.         for (int j = 0; j < maxDots - 1; j++) {
  30.           lines[i][j] = new Line(new PVector(dots.get(j).pos.x, dots.get(j).pos.y), new PVector(dots.get(j+1).pos.x, dots.get(j+1).pos.y), color(100, 100, 100));
  31.         }
  32.  
  33.         //add mobile lines
  34.       } else if (i < maxDots - 1) {
  35.         for (int j = 0; j < maxDots - i - 1; j++) {
  36.           //set the color as a gradient from yellow to blue
  37.           color c = color(map(i, 0, maxDots - 1, 255, 0), map(i, 0, maxDots - 1, 255, 0), map(i, 0, maxDots - 1, 0, 255));
  38.           lines[i][j] = new Line(new PVector(lines[i-1][j].start.x, lines[i-1][j].start.y), new PVector(lines[i-1][j+1].start.x, lines[i-1][j+1].start.y), c);
  39.         }
  40.  
  41.         //add tracing dot
  42.       } else {
  43.         dots.add(new Dot(dots.get(0).pos.x, dots.get(0).pos.y));
  44.         dots.get(dots.size()-1).col = color(255, 255, 0);
  45.       }
  46.     }
  47.   }
  48.  
  49.   //start the trail
  50.   trail.addSpot(dots.get(dots.size()-1).pos.x, dots.get(dots.size()-1).pos.y);
  51. }
  52.  
  53. void draw() {
  54.   background(51);
  55.  
  56.   //draw stationary lines
  57.   for (int i = 0; i < maxDots - 1; i++) {
  58.     lines[0][i].show();
  59.   }
  60.  
  61.   //draw mobile lines
  62.   for (int i = 1; i < maxDots; i++) {
  63.     for (int j = 0; j < maxDots - i - 1; j++) {
  64.  
  65.       lines[i][j].start.x = (counter*(lines[i-1][j+1].start.x - lines[i-1][j].start.x)/resolution)+lines[i-1][j].start.x;
  66.       lines[i][j].start.y = (counter*(lines[i-1][j+1].start.y - lines[i-1][j].start.y)/resolution)+lines[i-1][j].start.y;
  67.       lines[i][j].end.x = (counter*(lines[i-1][j+1].end.x - lines[i-1][j].end.x)/resolution)+lines[i-1][j].end.x;
  68.       lines[i][j].end.y = (counter*(lines[i-1][j+1].end.y - lines[i-1][j].end.y)/resolution)+lines[i-1][j].end.y;
  69.  
  70.       lines[i][j].show();
  71.     }
  72.   }
  73.  
  74.   //update tracing dot, and store that location in our path
  75.   dots.get(dots.size()-1).pos.x = (counter*(lines[maxDots - 2][0].end.x - lines[maxDots - 2][0].start.x)/resolution) + lines[maxDots - 2][0].start.x;
  76.   dots.get(dots.size()-1).pos.y = (counter*(lines[maxDots - 2][0].end.y - lines[maxDots - 2][0].start.y)/resolution) + lines[maxDots - 2][0].start.y;
  77.   trail.addSpot(dots.get(dots.size()-1).pos.x, dots.get(dots.size()-1).pos.y);
  78.  
  79.   //draw the dots
  80.   for (Dot d : dots) {
  81.     d.show();
  82.   }
  83.  
  84.   //draw the trail
  85.   trail.show();
  86.  
  87.   //increase our counter variable, and stop the simulation when we get to the end.
  88.   if (counter == resolution) {
  89.     noLoop();
  90.   } else {
  91.     counter++;
  92.   }
  93. }
  94.  
  95. class Dot {
  96.  
  97.   PVector pos = new PVector();
  98.  
  99.   color col = color(120);
  100.  
  101.   Dot(float x_, float y_) {
  102.     pos.x = x_;
  103.     pos.y = y_;
  104.   }
  105.  
  106.   void show() {
  107.     stroke(170);
  108.     strokeWeight(2);
  109.     fill(col);
  110.     ellipse(pos.x, pos.y, 10, 10);
  111.   }
  112. }
  113.  
  114. class Line {
  115.  
  116.   PVector start = new PVector();
  117.   PVector end = new PVector();
  118.   color col = color(0, 0, 0);
  119.  
  120.   Line(PVector start_, PVector end_, color col_) {
  121.     start = start_;
  122.     end = end_;
  123.     col = col_;
  124.   }
  125.  
  126.   void show() {
  127.     stroke(col);
  128.     strokeWeight(3);
  129.     line(start.x, start.y, end.x, end.y);
  130.   }
  131. }
  132.  
  133. class Path {
  134.   ArrayList<PVector> spots = new ArrayList<PVector>();
  135.  
  136.   void addSpot(float x, float y) {
  137.     spots.add(new PVector(x, y));
  138.   }
  139.  
  140.   void show() {
  141.  
  142.     stroke(color(255, 0, 0));
  143.     noFill();
  144.     beginShape();
  145.     for (PVector s : spots) {
  146.       vertex(s.x, s.y);
  147.     }
  148.     endShape();
  149.   }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement