Advertisement
Rafpast

N-pendulum1

Dec 21st, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.72 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. Random randa = new Random();
  4. int numberOfNpendulum = 3;
  5. boolean stop = true;
  6. NPendulum nPend;
  7.  
  8. void setup() {  
  9.   size(1920, 1060, P2D);
  10.  
  11.   ArrayList<Float> nPendLenghts = new ArrayList<Float>(numberOfNpendulum);
  12.   ArrayList<Float> nPendAngles  = new ArrayList<Float>(numberOfNpendulum);
  13.  
  14.   //creating a random npendulum
  15.   nPendLenghts.add(0.0);
  16.   nPendAngles.add(0.0);
  17.  
  18.   for (int i = 0; i<numberOfNpendulum; i++)
  19.   {
  20.     nPendLenghts.add(random(150));
  21.     nPendAngles.add(random(TWO_PI));
  22.   }
  23.  
  24.   nPend = new NPendulum(numberOfNpendulum, nPendLenghts, nPendAngles);
  25. }
  26.  
  27. void keyPressed()
  28. {
  29.   if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
  30.   {
  31.     if (key == 'n' ||key == 'N')
  32.     {
  33.       nPend.reset(numberOfNpendulum);
  34.     } else if(key == 's' ||key == 'S')
  35.     {
  36.       stop = !stop;
  37.     }
  38.   }
  39. }
  40.  
  41. void draw() {
  42.  
  43.   background(120);
  44.   if (stop)
  45.   {
  46.     nPend.calculations();
  47.   }
  48.   nPend.drawing();
  49. }
  50.  
  51.  
  52. class Function {
  53.   float smallestItem, largestItem;
  54.  
  55.  
  56.   Function(float largest, float smallest)
  57.   {
  58.     largestItem = largest;
  59.     smallestItem = smallest;
  60.   }
  61.  
  62.    void findingTheItem(float item_)
  63.   {
  64.     if (item_<smallestItem)
  65.     {
  66.       smallestItem = item_;
  67.     } else if (item_>largestItem)
  68.     {
  69.       largestItem = item_;
  70.     }
  71.   }
  72. }
  73.  
  74. class Circum {
  75.   int max_velocity = 15;
  76.   float radius, mass, ref = 0.5, springness = 0.9999;
  77.   PVector  point = new PVector(0, 0);
  78.   PVector  acceleration = new PVector(0, 0);
  79.   PVector  velocity = new PVector(new Random().nextInt(max_velocity) + 0.05, -1 * (new Random().nextInt(max_velocity) + 0.05));
  80.   PVector gre = new PVector(0, 0);
  81.   Function[] fun = new Function[6];
  82.  
  83.   Circum() {
  84.     mass = 5 * new Random().nextFloat() + 1;
  85.     radius = mass * 4;
  86.     point.x = (width * new Random().nextFloat()) - radius;
  87.     point.y =  (height * new Random().nextFloat()) - radius;
  88.  
  89.     fun[0] = new Function(velocity.y - 1, velocity.y + 1);
  90.     fun[1] = new Function(gre.y + 0.005, gre.y - 0.005);
  91.     fun[2] = new Function(point.y - 0.05, point.y + 0.1);
  92.     fun[3] = new Function(point.x - 0.1, point.x + 0.1);
  93.     fun[4] = new Function(gre.x + 0.005, gre.x - 0.005);
  94.     fun[5] = new Function(velocity.x - 0.01, max_velocity);
  95.    
  96.   }
  97.  
  98.   Circum(float x_, float y_, float radius_, float mass_) {
  99.     this();
  100.     point.x = x_;
  101.     point.y = y_;
  102.     radius = radius_;
  103.     mass = mass_;
  104.    
  105.     fun[0] = new Function(velocity.y - 1, velocity.y + 1);
  106.     fun[1] = new Function(gre.y + 0.005, gre.y - 0.005);
  107.     fun[2] = new Function(point.y - 0.05, point.y + 0.1);
  108.     fun[3] = new Function(point.x - 0.1, point.x + 0.1);
  109.     fun[4] = new Function(gre.x + 0.005, gre.x - 0.005);
  110.     fun[5] = new Function(velocity.x - 0.01, max_velocity);
  111.   }
  112.  
  113.   void setSpeed(PVector force) {
  114.     PVector f = force.copy();
  115.     acceleration.add(PVector.div( f, mass));
  116.     velocity.add(acceleration);
  117.     point.add(velocity);
  118.     gre = acceleration.copy();
  119.  
  120.     // If out of bounds
  121.     if (point.y - radius <= 0)
  122.     {
  123.       velocity.y *= -springness;
  124.       point.y = 0 + radius;
  125.     } else if (point.y + radius >= height)
  126.     {
  127.       velocity.y *= -springness;
  128.       point.y = height - radius ;
  129.     }
  130.     if (point.x - radius <= 0)
  131.     {
  132.       velocity.x *= -springness;
  133.       point.x = 0 + radius;
  134.     } else if (point.x + radius >= width)
  135.     {
  136.       velocity.x *= -springness;
  137.       point.x = width - radius;
  138.     }
  139.     acceleration.mult(0);
  140.   }
  141.  
  142.   void drawing() {
  143.  
  144.  
  145.     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)) );
  146.     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)));
  147.     circle(point.x, point.y, 2 * radius);
  148.  
  149.     strokeWeight(2);
  150.     stroke(255, 255, 255);
  151.     PVector dir = velocity.copy();
  152.     dir.normalize().mult(radius);
  153.     dir = PVector.add(dir, point);
  154.     line(point.x, point.y, dir.x, dir.y);
  155.   }
  156. }
  157.  
  158. class NPendulum {
  159.   ArrayList<Circum> circles = new ArrayList<Circum>();
  160.   FloatList lenghts = new FloatList();
  161.   FloatList angles = new FloatList();
  162.   PVector origin = new PVector(width / 2, height/3);
  163.   float g = 0.0004905000, damping = 0.998;
  164.   int amount = 0;
  165.  
  166.   NPendulum(int amount_, ArrayList<Float> lenghts_, ArrayList<Float> angles_)
  167.   {
  168.     amount = amount_ + 1;
  169.     float mass = 10, radius = 20;
  170.  
  171.     for (int i = 0; i <lenghts_.size(); i++)
  172.     {
  173.       lenghts.append(lenghts_.get(i));
  174.     }  
  175.  
  176.     for (int i = 0; i <angles_.size(); i++)
  177.     {
  178.       angles.append(angles_.get(i));
  179.     }  
  180.    
  181.     circles.add(new Circum(origin.x, origin.y, radius, mass));
  182.    
  183.     for (int i = 1; i <amount; i++)
  184.     {
  185.       mass = 20;
  186.       radius = mass * 2;
  187.       circles.add(new Circum(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)), radius, mass));
  188.       circles.get(i).point.add(circles.get(i - 1).point);
  189.     }
  190.   }
  191.  
  192.   void reset(int number)
  193.   {
  194.     float mass = 20, radius = 20;
  195.  
  196.     lenghts.clear();
  197.     angles.clear();
  198.  
  199.     for (int i = circles.size() - 1; i >= 0; i--)
  200.     {
  201.       circles.remove(i);
  202.     }
  203.  
  204.     number++;
  205.     lenghts.append(0.0);
  206.     angles.append(0.0);
  207.  
  208.     circles.add(new Circum(origin.x, origin.y, radius, mass));
  209.  
  210.     for (int i = 1; i <number; i++)
  211.     {      
  212.       lenghts.append(random(circles.get(i - 1).radius * 2, 150));
  213.       angles.append(random(TWO_PI));
  214.       mass = 8 * randa.nextFloat() + 4;
  215.       radius = mass  * 4;
  216.  
  217.       circles.add(new Circum(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)), radius, mass));
  218.       circles.get(i).point.add(circles.get(i - 1).point);
  219.     }
  220.   }
  221.  
  222.   void calculations()
  223.   {
  224.  
  225.     for (int i = 1; i <circles.size(); i++)
  226.     {    
  227.       circles.get(i).acceleration.set(0, (-g / lenghts.get(i)) * sin(angles.get(i)));
  228.  
  229.       circles.get(i).velocity.add(circles.get(i).acceleration);
  230.       angles.add(i, circles.get(i).velocity.mag());
  231.  
  232.       circles.get(i).point.set(lenghts.get(i)*sin(angles.get(i)), lenghts.get(i)*cos(angles.get(i)));
  233.       circles.get(i).point.add(circles.get(i - 1).point);  
  234.       circles.get(i).velocity.mult(damping);
  235.     }
  236.   }
  237.  
  238.   void drawing()
  239.   {
  240.     circles.get(0).drawing();
  241.  
  242.     for (int i = 1; i <circles.size(); i++)
  243.     {
  244.       line(circles.get(i - 1).point.x, circles.get(i - 1).point.y, circles.get(i).point.x, circles.get(i).point.y);
  245.       circles.get(i).drawing();
  246.     }
  247.   }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement