Advertisement
Rafpast

Npendulum3

Dec 30th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. boolean stopStart = false;
  2. NPendulum nPend;
  3.  
  4.  
  5. void setup() {
  6.   size(700, 700, P2D);
  7.  
  8.   nPend = new NPendulum(5);
  9. }
  10.  
  11.  
  12. void keyPressed()
  13. {
  14.   if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
  15.   {
  16.     if (key == 'n' ||key == 'N')
  17.     {
  18.       nPend.reset();
  19.     } else if (key == 's' ||key == 'S')
  20.     {
  21.       stopStart = !stopStart;
  22.     }
  23.   }
  24. }
  25.  
  26. void draw() {
  27.   translate(width / 2, height/2);
  28.   background(200);
  29.  
  30.   if (stopStart)
  31.   {
  32.     nPend.calculations();
  33.   }
  34.   nPend.drawing();
  35. }
  36.  
  37. class NPendulum {
  38.  
  39.   int tier = 0;
  40.   FloatList mass;  
  41.   FloatList angles;
  42.   FloatList lenghts;
  43.   FloatList velocities;
  44.   FloatList accelerations;
  45.   float damping = 0.998, g = 0.981;
  46.   PVector []position;
  47.  
  48.   NPendulum(int _tier)
  49.   {
  50.     tier = _tier;
  51.     velocities = new FloatList(tier);
  52.     mass = new FloatList(tier);
  53.     lenghts = new FloatList(tier);
  54.     angles = new FloatList(tier);
  55.     accelerations = new FloatList(tier);
  56.     position = new PVector[tier];
  57.     reset();
  58.   }
  59.  
  60.   void reset()
  61.   {
  62.     mass.clear();
  63.     angles.clear();
  64.     lenghts.clear();
  65.     velocities.clear();
  66.     accelerations.clear();
  67.  
  68.     for (int i =0; i<tier; i++)
  69.     {
  70.       velocities.append(0);
  71.       accelerations.append(0);
  72.       mass.append(random(10, 50));
  73.       angles.append(random(PI));
  74.       lenghts.append(random(30, 150));
  75.       position[i] = new PVector(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)));
  76.     }
  77.   }
  78.  
  79.   void calculations()
  80.   {
  81.     float den = 0;
  82.     float num1 = 0;
  83.     float num2 = 0;
  84.     float num3 = 0;
  85.  
  86.     for (int j = 0; j < tier; j++) {
  87.       //denominator
  88.       for (int k = 0; k < tier; k++) {
  89.         den += mass.get(k) * lenghts.get(k) * lenghts.get(k) * (j <= k? 1 : 0);
  90.       }
  91.  
  92.  
  93.       for (int k = 0; k < tier; k++) {
  94.         //first numerator
  95.         num1 = g * lenghts.get(j) * sin(angles.get(j)) * mass.get(j) * (j <= k? 1 : 0);
  96.  
  97.         //second numerator
  98.         float inner_sum = 0;
  99.         // inner sum
  100.         for (int q = k+1; q < tier; q++) {
  101.           inner_sum += mass.get(q) * (j <= q? 1 : 0);
  102.         }
  103.         num2 = inner_sum * lenghts.get(j) * lenghts.get(k) * sin(angles.get(j) - angles.get(k)) * velocities.get(j) * velocities.get(k);
  104.  
  105.         //Third numerator
  106.         //The inner sum is the same as in the num2
  107.         num3 = inner_sum * lenghts.get(j) * lenghts.get(k) * (sin(angles.get(k) - angles.get(j)) * (velocities.get(j) * velocities.get(k)) * velocities.get(k) + (j != k ? 1 : 0) * cos(angles.get(j) - angles.get(k))*accelerations.get(k));
  108.       }
  109.       float result = - (num1 + num2 + num3) / den;
  110.      
  111.       accelerations.set(j,result);
  112.     }
  113.  
  114.     accToAngle();
  115.   }
  116.  
  117.   void accToAngle()
  118.   {
  119.     velocities.set(0, velocities.get(0) + accelerations.get(0));
  120.     angles.set(0, angles.get(0) + velocities.get(0));
  121.     position[0].set(lenghts.get(0) * sin(angles.get(0)), lenghts.get(0) * cos(angles.get(0)));
  122.     velocities.set(0, velocities.get(0) * damping);
  123.  
  124.     for (int i = 1; i < tier; i++)
  125.     {
  126.       velocities.set(i, velocities.get(i) + accelerations.get(i));
  127.       angles.set(i, angles.get(i) + velocities.get(i));
  128.       position[i].set(lenghts.get(i) * sin(angles.get(i)) + position[i - 1].x, lenghts.get(i) * cos(angles.get(i)) + position[i - 1].y);
  129.       velocities.set(i, velocities.get(0) * damping);
  130.     }
  131.   }
  132.  
  133.   void drawing()
  134.   {
  135.     circle(0, 0, 20);
  136.     line(0, 0, position[0].x, position[0].y);
  137.     circle(position[0].x, position[0].y, 20);
  138.  
  139.     for (int i = 1; i<tier; i++)
  140.     {
  141.       line(position[i - 1].x, position[i - 1].y, position[i].x, position[i].y);
  142.       circle(position[i].x, position[i].y, 20);
  143.     }
  144.   }
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement