Rafpast

Pendulum

Jul 30th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1. void singlePendulum()
  2. {
  3.  
  4.   for (int i = 0; i< pend.size(); i++)
  5.   {
  6.     if (singleAction)
  7.     {
  8.       pend.get(i).update();
  9.     }
  10.     pend.get(i).drawing();
  11.   }
  12.   circle(0, 0, 10);
  13.  
  14. }
  15.  
  16. void dublePendulum()
  17. {
  18.  
  19.   for (int i = 0; i< doublePend.size(); i++)
  20.   {
  21.     doublePend.get(i).drowing(i, ile);
  22.   }
  23.   //drawing center ball
  24.   fill(82, 0, 14);
  25.   stroke(153, 0, 26);
  26.   circle(0, 0, 20);
  27.  
  28. }
  29.  
  30. class pendulum {
  31.  
  32.   PVector origin = new PVector(width / 2, height/3);
  33.   PVector position; // position of pendulum ball
  34.   float a1_a, a1_vel, angle =PI;
  35.   float lengh = 250, radius = 24;
  36.   float damping = 0.998;       // Arbitary damping amount
  37.   Function[] fun = new Function[3];
  38.   ControlP5 cp5;
  39.  
  40.  
  41.   pendulum(float a1_,PVector origin_) {
  42.  
  43.     angle = a1_;
  44.     position = new PVector(lengh*sin(angle), lengh*cos(angle));
  45.     position.add(origin_);
  46.  
  47.     fun[0] = new Function(angle, angle);
  48.     fun[1] = new Function(a1_vel, a1_vel);
  49.     fun[2] = new Function(a1_a, a1_a);
  50.   }
  51.  
  52.  
  53.   void update()
  54.   {
  55.  
  56.     float gravity = -0.4905;
  57.  
  58.     a1_a = (gravity / lengh) * sin(angle);
  59.  
  60.     a1_vel += a1_a;
  61.     angle += a1_vel;
  62.  
  63.     position.set(lengh*sin(angle), lengh*cos(angle));
  64.     position.add(origin);  
  65.     a1_vel *= damping;
  66.   }
  67.  
  68.   void showingData()
  69.   {
  70.   }
  71.  
  72.   void drawing()
  73.   {
  74.  
  75.     //finding the smallest and largest limit values used to change the color of objects
  76.     fun[0].findingTheItem(angle);
  77.     fun[1].findingTheItem(a1_vel);
  78.     fun[2].findingTheItem(a1_a);
  79.  
  80.     stroke(255);
  81.     fill((map(angle, fun[0].smallestItem, fun[0].largestItem, 10, 255)), (map(a1_vel, fun[1].smallestItem, fun[1].largestItem, 10, 255)), (map(a1_a, fun[2].smallestItem, fun[2].largestItem, 10, 255)));
  82.     line(origin.x, origin.y, position.x, position.y);
  83.     circle( position.x, position.y, 2 * radius);
  84.   }
  85. }
  86.  
  87. class double_pendulum {
  88.  
  89.   PVector[]  point = new PVector[500];
  90.   PVector[]  position = new PVector[2];
  91.   PVector origin = new PVector(width / 2, height/3);
  92.   float g = 0.4905, delta_t = 1;
  93.   float length1, length2; // lenght of pendulums
  94.   float mas1, mas2; //mass of pendulum
  95.   float a1_vel = 0, a2_vel = 0; // angular velocity
  96.   float a1_a = 0, a2_a = 0; //angular acceleration
  97.   float a1, a2; //angle
  98.   float radius1 = 30, radius2 = 20; //angle
  99.   float counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, den = 0;
  100.   float counter5 = 0, counter6 = 0, counter7 = 0, counter8 = 0, den2 = 0;
  101.   float cx = 0, cy = 0;
  102.   float px2 = -1, py2 = -1;
  103.   int i = 0, current = i, iko = 0;
  104.   Function[] fun = new Function[6];
  105.   ControlP5 cp5;
  106.  
  107.   double_pendulum(float a1_, float a2_, float length1_, float length2_, float mas1_, float mas2_)
  108.   {
  109.     a1 = a1_;
  110.     a2 = a2_;
  111.     length1 = length1_;
  112.     length2 = length2_;
  113.     mas1 = mas1_;
  114.     mas2 = mas2_;
  115.  
  116.     position[0] = new PVector(0, 0);
  117.     position[1] = new PVector(0, 0);
  118.  
  119.     for (int i = 0; i<point.length; i++)
  120.     {
  121.       point[i] = new PVector(position[1].x, position[1].y);
  122.     }
  123.  
  124.     fun[0] = new Function(a1_vel, a1_vel);
  125.     fun[1] = new Function(a1_a, a1_a);
  126.     fun[2] = new Function(a1, a1);
  127.     fun[3] = new Function(a2_vel, a2_vel);
  128.     fun[4] = new Function(a2_a, a2_a);
  129.     fun[5] = new Function(a2, a2);
  130.   }
  131.  
  132.  
  133.   void set0()
  134.   {
  135.     position[0].set(origin.x, origin.y);
  136.     position[1].set(position[0].x, position[0].y);
  137.  
  138.     a1_vel = 0;
  139.     a2_vel = 0;
  140.     a1_a = 0;
  141.     a2_a = 0;
  142.     a1 = PI / (randa.nextInt(5) + 1);
  143.     a2 = PI / (randa.nextInt(5) + 1);
  144.  
  145.     if ( a1==PI||a2 == PI)
  146.     {
  147.       a1+=0.01;
  148.       a2+=randa.nextFloat();
  149.     }
  150.     double_pen();
  151.  
  152.     for (int j = 0; j<point.length; j++)
  153.     {
  154.       point[j].x = position[1].x;
  155.       point[j].y = position[1].y;
  156.     }
  157.  
  158.     a1_vel = 0;
  159.     a2_vel = 0;
  160.     a1_a = 0;
  161.     a2_a = 0;
  162.   }
  163.  
  164.  
  165.   void double_pen()
  166.   {
  167.     //calculation of acceleration, velocity and position of double pendulum
  168.     counter1 = -(g) * (2 * mas1 + mas2) * sin(a1);
  169.     counter2 = -mas2 * (g)*sin(a1 - 2 * a2);
  170.     counter3 = -2 * sin(a1 - a2) * mas2;
  171.     counter4 = a2_vel * a2_vel * length2 + a1_vel * a1_vel * length1 * cos(a1 - a2);
  172.     den = length1 * (2 * mas1 + mas2 - mas2 * cos(2 * a1 - 2 * a2));
  173.     a1_a = (counter1 + counter2 + counter3 * counter4) / den;
  174.  
  175.     counter5 = 2 * sin(a1 - a2);
  176.     counter6 = (a1_vel * a1_vel * length1 * (mas1 + mas2));
  177.     counter7 = (g) * (mas1 + mas2) * cos(a1);
  178.     counter8 = a2_vel * a2_vel * length2 * mas2 * cos(a1 - a2);
  179.     den2 = length2 * (2 * mas1 + mas2 - mas2 * cos(2 * a1 - 2 * a2));
  180.     a2_a = (counter5 * (counter6 + counter7 + counter8)) / den2;
  181.  
  182.     position[0].x = length1 * sin(a1);
  183.     position[0].y = length1 * cos(a1);
  184.     position[0].add(origin);
  185.     position[1].x = position[0].x + length2 * sin(a2);
  186.     position[1].y = position[0].y + length2 * cos(a2);
  187.     //position[1].add(origin);
  188.  
  189.     a1_vel += a1_a;
  190.     a2_vel += a2_a;
  191.     a1 += a1_vel;
  192.     a2 += a2_vel;
  193.  
  194.     a1_vel *= 0.999;
  195.     a2_vel *= 0.999;
  196.     px2 = position[1].x;
  197.     py2 = position[1].y;
  198.   }
  199.  
  200.   void trace(int k, int ile)
  201.   {
  202.     iko = k;
  203.     double_pen();
  204.     point[i].x = position[1].x;
  205.     point[i].y = position[1].y;
  206.  
  207.     i++;
  208.  
  209.     if (i==point.length)
  210.     {
  211.       i = 0;
  212.     }
  213.  
  214.     stroke(100, 0, 100);
  215.     strokeWeight(6);
  216.     noFill();
  217.  
  218.     current = i;
  219.  
  220.     //drawing a trace
  221.     beginShape();
  222.     for (int j = 0; j<point.length; j++)
  223.     {
  224.       current++;
  225.  
  226.       if (current == point.length)
  227.       {
  228.         current = 0;
  229.       }
  230.  
  231.       stroke((map(j, 0, point.length, 50, 255)), (map(iko, 0, ile, 50, 255)), (map(current, 0, point.length, 50, 255)));
  232.  
  233.       curveVertex(point[current].x, point[current].y);
  234.     }
  235.     endShape();
  236.   }
  237.  
  238.   void showingData( int whichPart)
  239.   {
  240.     if(whichPart == 3)
  241.     {
  242.      
  243.     }else if(whichPart == 4)
  244.     {
  245.      
  246.     }
  247.    
  248.    
  249.   }
  250.  
  251.  
  252.   void drowing(int k, int ile)
  253.   {
  254.     //calculation of trace
  255.     if (doubleAction)
  256.     {
  257.       trace(k, ile);
  258.     }
  259.  
  260.     //finding the smallest and largest limit values used to change the color of objects
  261.     fun[0].findingTheItem(a1_vel);
  262.     fun[1].findingTheItem(a1_a);
  263.     fun[2].findingTheItem(a1);
  264.     fun[3].findingTheItem(a2_vel);
  265.     fun[4].findingTheItem(a2_a);
  266.     fun[5].findingTheItem(a2);
  267.  
  268.     //drawing double pendulum
  269.     strokeWeight(2);
  270.     stroke(255, 255, 0);
  271.     //drawing line conecting center to first ball, and to second
  272.     line(origin.x, origin.y, position[0].x, position[0].y);
  273.     line(position[0].x, position[0].y, position[1].x, position[1].y);
  274.     //drawing first ball
  275.     stroke(255, 0, 0);
  276.     fill((map( a1_vel, fun[0].smallestItem, fun[0].largestItem, 10, 255)), (map( a1_a, fun[1].smallestItem, fun[1].largestItem, 10, 255)), (map(a1, fun[2].smallestItem, fun[2].largestItem, 10, 255)));
  277.     circle(position[0].x, position[0].y, radius1 * 2);
  278.     //drawing second ball
  279.     stroke(0, 0, 255);
  280.     fill( (map(a2_vel, fun[3].smallestItem, fun[3].largestItem, 10, 255)), (map( a2_a, fun[4].smallestItem, fun[4].largestItem, 10, 255)), (map(a2, fun[5].smallestItem, fun[5].largestItem, 10, 255)));
  281.     circle(position[1].x, position[1].y, radius2 * 2);
  282.     stroke(0, 0, 255);
  283.     fill(255, 0, 0);
  284.     circle(origin.x, origin.y, 10);
  285.   }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment