Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Created by MrMusAddict
- //3:00 PM February 21, 2017
- //Inspired by https://www.reddit.com/r/processing/comments/5unt8u/i_made_this_b%C3%A9zier_curve/
- int maxDots = 4; //minimum 3, theoretically no maximum
- int resolution = 1000;
- ArrayList<Dot> dots = new ArrayList<Dot>();
- int counter = 1;
- Path trail = new Path();
- Line[][] lines;
- void setup() {
- size(600, 600);
- lines = new Line[maxDots][maxDots];
- //add dots
- for (int i = 0; i < maxDots; i++) {
- dots.add(new Dot(random(width), random(height)));
- }
- //add lines & tracing dot
- if (maxDots >= 3) {
- for (int i = 0; i < maxDots; i++) {
- //add stationary lines
- if (i == 0) {
- for (int j = 0; j < maxDots - 1; j++) {
- 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));
- }
- //add mobile lines
- } else if (i < maxDots - 1) {
- for (int j = 0; j < maxDots - i - 1; j++) {
- //set the color as a gradient from yellow to blue
- color c = color(map(i, 0, maxDots - 1, 255, 0), map(i, 0, maxDots - 1, 255, 0), map(i, 0, maxDots - 1, 0, 255));
- 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);
- }
- //add tracing dot
- } else {
- dots.add(new Dot(dots.get(0).pos.x, dots.get(0).pos.y));
- dots.get(dots.size()-1).col = color(255, 255, 0);
- }
- }
- }
- //start the trail
- trail.addSpot(dots.get(dots.size()-1).pos.x, dots.get(dots.size()-1).pos.y);
- }
- void draw() {
- background(51);
- //draw stationary lines
- for (int i = 0; i < maxDots - 1; i++) {
- lines[0][i].show();
- }
- //draw mobile lines
- for (int i = 1; i < maxDots; i++) {
- for (int j = 0; j < maxDots - i - 1; j++) {
- 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;
- 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;
- 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;
- 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;
- lines[i][j].show();
- }
- }
- //update tracing dot, and store that location in our path
- 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;
- 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;
- trail.addSpot(dots.get(dots.size()-1).pos.x, dots.get(dots.size()-1).pos.y);
- //draw the dots
- for (Dot d : dots) {
- d.show();
- }
- //draw the trail
- trail.show();
- //increase our counter variable, and stop the simulation when we get to the end.
- if (counter == resolution) {
- noLoop();
- } else {
- counter++;
- }
- }
- class Dot {
- PVector pos = new PVector();
- color col = color(120);
- Dot(float x_, float y_) {
- pos.x = x_;
- pos.y = y_;
- }
- void show() {
- stroke(170);
- strokeWeight(2);
- fill(col);
- ellipse(pos.x, pos.y, 10, 10);
- }
- }
- class Line {
- PVector start = new PVector();
- PVector end = new PVector();
- color col = color(0, 0, 0);
- Line(PVector start_, PVector end_, color col_) {
- start = start_;
- end = end_;
- col = col_;
- }
- void show() {
- stroke(col);
- strokeWeight(3);
- line(start.x, start.y, end.x, end.y);
- }
- }
- class Path {
- ArrayList<PVector> spots = new ArrayList<PVector>();
- void addSpot(float x, float y) {
- spots.add(new PVector(x, y));
- }
- void show() {
- stroke(color(255, 0, 0));
- noFill();
- beginShape();
- for (PVector s : spots) {
- vertex(s.x, s.y);
- }
- endShape();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement