Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- Random randa = new Random();
- int numberOfNpendulum = 3;
- boolean stop = true;
- NPendulum nPend;
- void setup() {
- size(1920, 1060, P2D);
- ArrayList<Float> nPendLenghts = new ArrayList<Float>(numberOfNpendulum);
- ArrayList<Float> nPendAngles = new ArrayList<Float>(numberOfNpendulum);
- //creating a random npendulum
- nPendLenghts.add(0.0);
- nPendAngles.add(0.0);
- for (int i = 0; i<numberOfNpendulum; i++)
- {
- nPendLenghts.add(random(150));
- nPendAngles.add(random(TWO_PI));
- }
- nPend = new NPendulum(numberOfNpendulum, nPendLenghts, nPendAngles);
- }
- void keyPressed()
- {
- if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
- {
- if (key == 'n' ||key == 'N')
- {
- nPend.reset(numberOfNpendulum);
- } else if(key == 's' ||key == 'S')
- {
- stop = !stop;
- }
- }
- }
- void draw() {
- background(120);
- if (stop)
- {
- nPend.calculations();
- }
- nPend.drawing();
- }
- class Function {
- float smallestItem, largestItem;
- Function(float largest, float smallest)
- {
- largestItem = largest;
- smallestItem = smallest;
- }
- void findingTheItem(float item_)
- {
- if (item_<smallestItem)
- {
- smallestItem = item_;
- } else if (item_>largestItem)
- {
- largestItem = item_;
- }
- }
- }
- class Circum {
- int max_velocity = 15;
- float radius, mass, ref = 0.5, springness = 0.9999;
- PVector point = new PVector(0, 0);
- PVector acceleration = new PVector(0, 0);
- PVector velocity = new PVector(new Random().nextInt(max_velocity) + 0.05, -1 * (new Random().nextInt(max_velocity) + 0.05));
- PVector gre = new PVector(0, 0);
- Function[] fun = new Function[6];
- Circum() {
- mass = 5 * new Random().nextFloat() + 1;
- radius = mass * 4;
- point.x = (width * new Random().nextFloat()) - radius;
- point.y = (height * new Random().nextFloat()) - radius;
- fun[0] = new Function(velocity.y - 1, velocity.y + 1);
- fun[1] = new Function(gre.y + 0.005, gre.y - 0.005);
- fun[2] = new Function(point.y - 0.05, point.y + 0.1);
- fun[3] = new Function(point.x - 0.1, point.x + 0.1);
- fun[4] = new Function(gre.x + 0.005, gre.x - 0.005);
- fun[5] = new Function(velocity.x - 0.01, max_velocity);
- }
- Circum(float x_, float y_, float radius_, float mass_) {
- this();
- point.x = x_;
- point.y = y_;
- radius = radius_;
- mass = mass_;
- fun[0] = new Function(velocity.y - 1, velocity.y + 1);
- fun[1] = new Function(gre.y + 0.005, gre.y - 0.005);
- fun[2] = new Function(point.y - 0.05, point.y + 0.1);
- fun[3] = new Function(point.x - 0.1, point.x + 0.1);
- fun[4] = new Function(gre.x + 0.005, gre.x - 0.005);
- fun[5] = new Function(velocity.x - 0.01, max_velocity);
- }
- void setSpeed(PVector force) {
- PVector f = force.copy();
- acceleration.add(PVector.div( f, mass));
- velocity.add(acceleration);
- point.add(velocity);
- gre = acceleration.copy();
- // If out of bounds
- if (point.y - radius <= 0)
- {
- velocity.y *= -springness;
- point.y = 0 + radius;
- } else if (point.y + radius >= height)
- {
- velocity.y *= -springness;
- point.y = height - radius ;
- }
- if (point.x - radius <= 0)
- {
- velocity.x *= -springness;
- point.x = 0 + radius;
- } else if (point.x + radius >= width)
- {
- velocity.x *= -springness;
- point.x = width - radius;
- }
- acceleration.mult(0);
- }
- void drawing() {
- fill((map(velocity.y, fun[0].smallestItem - 0.01, fun[0].largestItem + 0.01, 60, 255)), (map(gre.y, fun[1].smallestItem - 0.01, fun[1].largestItem + 0.01, 50, 100)), (map(point.y, fun[2].smallestItem - 0.01, fun[2].largestItem + 0.01, 1, 255)) );
- stroke((map(point.x, fun[3].smallestItem - 0.01, fun[3].largestItem + 0.01, 100, 255)), (map(gre.x, fun[4].smallestItem - 0.01, fun[4].largestItem + 0.01, 60, 110)), (map(velocity.x, fun[5].smallestItem - 0.01, fun[5].largestItem + 0.01, 50, 255)));
- circle(point.x, point.y, 2 * radius);
- strokeWeight(2);
- stroke(255, 255, 255);
- PVector dir = velocity.copy();
- dir.normalize().mult(radius);
- dir = PVector.add(dir, point);
- line(point.x, point.y, dir.x, dir.y);
- }
- }
- class NPendulum {
- ArrayList<Circum> circles = new ArrayList<Circum>();
- FloatList lenghts = new FloatList();
- FloatList angles = new FloatList();
- PVector origin = new PVector(width / 2, height/3);
- float g = 0.0004905000, damping = 0.998;
- int amount = 0;
- NPendulum(int amount_, ArrayList<Float> lenghts_, ArrayList<Float> angles_)
- {
- amount = amount_ + 1;
- float mass = 10, radius = 20;
- for (int i = 0; i <lenghts_.size(); i++)
- {
- lenghts.append(lenghts_.get(i));
- }
- for (int i = 0; i <angles_.size(); i++)
- {
- angles.append(angles_.get(i));
- }
- circles.add(new Circum(origin.x, origin.y, radius, mass));
- for (int i = 1; i <amount; i++)
- {
- mass = 20;
- radius = mass * 2;
- circles.add(new Circum(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)), radius, mass));
- circles.get(i).point.add(circles.get(i - 1).point);
- }
- }
- void reset(int number)
- {
- float mass = 20, radius = 20;
- lenghts.clear();
- angles.clear();
- for (int i = circles.size() - 1; i >= 0; i--)
- {
- circles.remove(i);
- }
- number++;
- lenghts.append(0.0);
- angles.append(0.0);
- circles.add(new Circum(origin.x, origin.y, radius, mass));
- for (int i = 1; i <number; i++)
- {
- lenghts.append(random(circles.get(i - 1).radius * 2, 150));
- angles.append(random(TWO_PI));
- mass = 8 * randa.nextFloat() + 4;
- radius = mass * 4;
- circles.add(new Circum(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)), radius, mass));
- circles.get(i).point.add(circles.get(i - 1).point);
- }
- }
- void calculations()
- {
- for (int i = 1; i <circles.size(); i++)
- {
- circles.get(i).acceleration.set(0, (-g / lenghts.get(i)) * sin(angles.get(i)));
- circles.get(i).velocity.add(circles.get(i).acceleration);
- angles.add(i, circles.get(i).velocity.mag());
- circles.get(i).point.set(lenghts.get(i)*sin(angles.get(i)), lenghts.get(i)*cos(angles.get(i)));
- circles.get(i).point.add(circles.get(i - 1).point);
- circles.get(i).velocity.mult(damping);
- }
- }
- void drawing()
- {
- circles.get(0).drawing();
- for (int i = 1; i <circles.size(); i++)
- {
- line(circles.get(i - 1).point.x, circles.get(i - 1).point.y, circles.get(i).point.x, circles.get(i).point.y);
- circles.get(i).drawing();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement