Rafpast

Every class

Dec 3rd, 2020 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.56 KB | None | 0 0
  1. class Circum {
  2.   int max_velocity = 15;
  3.   float radius, mass, ref = 0.5, springness = 0.9999;
  4.   PVector  point = new PVector(0, 0);
  5.   PVector  acceleration = new PVector(0, 0);
  6.   PVector  velocity = new PVector(new Random().nextInt(max_velocity) + 0.05, -1 * (new Random().nextInt(max_velocity) + 0.05));
  7.   PVector gre = new PVector(0, 0);
  8.   String[] fieldName = new String[3];
  9.   float[] fieldValue = new float[3];
  10.   Function[] fun = new Function[6];
  11.  
  12.  
  13.   Circum() {
  14.     mass = 5 * new Random().nextFloat() + 1;
  15.     radius = mass * 4;
  16.     point.x = (width * new Random().nextFloat()) - radius;
  17.     point.y =  (height * new Random().nextFloat()) - radius;
  18.  
  19.     fun[0] = new Function(velocity.y - 1, velocity.y + 1);
  20.     fun[1] = new Function(gre.y + 0.005, gre.y - 0.005);
  21.     fun[2] = new Function(point.y - 0.005, point.y + 0.01);
  22.     fun[3] = new Function(point.x - 0.01, point.x + 0.01);
  23.     fun[4] = new Function(gre.x + 0.005, gre.x - 0.005);
  24.     fun[5] = new Function(velocity.x - 0.01, max_velocity);
  25.   }
  26.  
  27.   Circum(float x_, float y_, float r_, float m_) {
  28.     this();
  29.     point.x = x_;
  30.     point.y = y_;
  31.     radius = r_;
  32.     mass = m_;
  33.  
  34.     fun[0] = new Function(velocity.y - 1, velocity.y + 1);
  35.     fun[1] = new Function(gre.y + 0.005, gre.y - 0.005);
  36.     fun[2] = new Function(point.y - 0.05, point.y + 0.1);
  37.     fun[3] = new Function(point.x - 0.1, point.x + 0.1);
  38.     fun[4] = new Function(gre.x + 0.005, gre.x - 0.005);
  39.     fun[5] = new Function(velocity.x - 0.01, max_velocity);
  40.   }
  41.  
  42.   void setSpeed(PVector force) {
  43.     PVector f = force.copy();
  44.     acceleration.add(PVector.div( f, mass));
  45.     velocity.add(acceleration);
  46.     point.add(velocity);
  47.     gre = acceleration.copy();
  48.  
  49.       // If out of bounds
  50.     if (point.y - radius <= 0)
  51.     {
  52.       velocity.y *= -springness;
  53.       point.y = 0 + radius;
  54.     } else if (point.y + radius >= height)
  55.     {
  56.       velocity.y *= -springness;
  57.       point.y = height - radius ;
  58.     }
  59.     if (point.x - radius <= 0)
  60.     {
  61.       velocity.x *= -springness;
  62.       point.x = 0 + radius;
  63.     } else if (point.x + radius >= width)
  64.     {
  65.       velocity.x *= -springness;
  66.       point.x = width - radius;
  67.     }
  68.     acceleration.mult(0);
  69.   }
  70.  
  71.   int showingData()
  72.   {
  73.     fieldName[0]  = "radius";
  74.     fieldValue[0] = radius;
  75.     fieldName[1]  = "mass";
  76.     fieldValue[1] = mass;
  77.     fieldName[2]  = "springiness";
  78.     fieldValue[2] = springness;
  79.  
  80.     return 3;
  81.   }
  82.  
  83.   void drawing() {
  84.  
  85.     //finding the smallest and largest limit values used to change the color of objects
  86.     fun[0].findingTheItem(velocity.y);
  87.     fun[1].findingTheItem(gre.y);
  88.     fun[2].findingTheItem(point.y);
  89.     fun[3].findingTheItem(point.x);
  90.     fun[4].findingTheItem(gre.x);
  91.     fun[5].findingTheItem(velocity.x);
  92.  
  93.     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)) );
  94.     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)));
  95.     circle(point.x, point.y, 2 * radius);
  96.  
  97.     strokeWeight(2);
  98.     stroke(255, 255, 255);
  99.     PVector dir = velocity.copy();
  100.     dir.normalize().mult(radius);
  101.     dir = PVector.add(dir, point);
  102.     line(point.x, point.y, dir.x, dir.y);
  103.   }
  104. }
  105.  
  106. class pendulum {
  107.  
  108.   PVector origin = new PVector(width / 2, height/3);
  109.   PVector position; // position of pendulum ball
  110.   float a1_a, a1_vel, angle =PI;
  111.   float lengh = 250, radius = 24;
  112.   float gravity = 0.4905, damping = 0.998;       // Arbitary damping amount
  113.   Function[] fun = new Function[3];
  114.   String[] fieldName = new String[3];
  115.   float[] fieldValue = new float[3];
  116.   ControlP5 cp5;
  117.  
  118.  
  119.   pendulum(float a1_, PVector origin_) {
  120.  
  121.     angle = a1_;
  122.     position = new PVector(lengh*sin(angle), lengh*cos(angle));
  123.     position.add(origin_);
  124.  
  125.     fun[0] = new Function(angle, angle);
  126.     fun[1] = new Function(a1_vel, a1_vel);
  127.     fun[2] = new Function(a1_a, a1_a);
  128.   }
  129.  
  130.  
  131.   void update()
  132.   {
  133.  
  134.     a1_a = (-gravity / lengh) * sin(angle);
  135.  
  136.     a1_vel += a1_a;
  137.     angle += a1_vel;
  138.  
  139.     position.set(lengh*sin(angle), lengh*cos(angle));
  140.     position.add(origin);  
  141.     a1_vel *= damping;
  142.   }
  143.  
  144. void setSpeed(PVector force) {
  145.     PVector f = force.copy();
  146.     a1_a = mag(PVector.div(f, mass).x, PVector.div(f, mass).y);
  147.     a1_vel += a1_a;
  148.     position.add(a1_vel,a1_vel);
  149.     //a1_a = 0;
  150. }
  151.  
  152.   int showingData()
  153.   {  
  154.     fieldName[0]  = "radius";
  155.     fieldValue[0] = radius;
  156.     fieldName[1]  = "gravity";
  157.     fieldValue[1] = gravity;
  158.     fieldName[2]  = "damping";
  159.     fieldValue[2] = damping;
  160.  
  161.     return 3;
  162.   }
  163.  
  164.   void drawing()
  165.   {
  166.  
  167.     //finding the smallest and largest limit values used to change the color of objects
  168.     fun[0].findingTheItem(angle);
  169.     fun[1].findingTheItem(a1_vel);
  170.     fun[2].findingTheItem(a1_a);
  171.  
  172.     stroke(255);
  173.     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)));
  174.     line(origin.x, origin.y, position.x, position.y);
  175.     circle( position.x, position.y, 2 * radius);
  176.   }
  177. }
  178.  
  179. class double_pendulum {
  180.  
  181.   PVector[]  point = new PVector[500];
  182.   PVector[]  position = new PVector[2];
  183.   PVector origin = new PVector(width / 2, height/3);
  184.   float g = 0.4905, delta_t = 1;
  185.   float length1, length2; // lenght of pendulums
  186.   float mas1, mas2; //mass of pendulum
  187.   float a1_vel = 0, a2_vel = 0; // angular velocity
  188.   float a1_a = 0, a2_a = 0; //angular acceleration
  189.   float a1, a2; //angle
  190.   float radius1 = 30, radius2 = 20; //radius
  191.   float counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, den = 0;
  192.   float counter5 = 0, counter6 = 0, counter7 = 0, counter8 = 0, den2 = 0;
  193.   float cx = 0, cy = 0;
  194.   float px2 = -1, py2 = -1;
  195.   int i = 0, current = i, iko = 0;
  196.   String[] fieldName = new String[3];
  197.   float[] fieldValue = new float[3];
  198.   Function[] fun = new Function[6];
  199.   ControlP5 cp5;
  200.  
  201.   double_pendulum(float a1_, float a2_, float length1_, float length2_, float mas1_, float mas2_)
  202.   {
  203.     a1 = a1_;
  204.     a2 = a2_;
  205.     length1 = length1_;
  206.     length2 = length2_;
  207.     mas1 = mas1_;
  208.     mas2 = mas2_;
  209.  
  210.     position[0] = new PVector(0, 0);
  211.     position[1] = new PVector(0, 0);
  212.  
  213.     for (int i = 0; i<point.length; i++)
  214.     {
  215.       point[i] = new PVector(position[1].x, position[1].y);
  216.     }
  217.  
  218.     fun[0] = new Function(a1_vel, a1_vel);
  219.     fun[1] = new Function(a1_a, a1_a);
  220.     fun[2] = new Function(a1, a1);
  221.     fun[3] = new Function(a2_vel, a2_vel);
  222.     fun[4] = new Function(a2_a, a2_a);
  223.     fun[5] = new Function(a2, a2);
  224.   }
  225.  
  226.  
  227.   void set0()
  228.   {
  229.     position[0].set(origin.x, origin.y);
  230.     position[1].set(position[0].x, position[0].y);
  231.  
  232.     a1_vel = 0;
  233.     a2_vel = 0;
  234.     a1_a = 0;
  235.     a2_a = 0;
  236.     a1 = PI / (randa.nextInt(5) + 1);
  237.     a2 = PI / (randa.nextInt(5) + 1);
  238.  
  239.     if ( a1==PI||a2 == PI)
  240.     {
  241.       a1+=0.01;
  242.       a2+=randa.nextFloat();
  243.     }
  244.     double_pen();
  245.  
  246.     for (int j = 0; j<point.length; j++)
  247.     {
  248.       point[j].x = position[1].x;
  249.       point[j].y = position[1].y;
  250.     }
  251.  
  252.     a1_vel = 0;
  253.     a2_vel = 0;
  254.     a1_a = 0;
  255.     a2_a = 0;
  256.   }
  257.  
  258.  
  259.   void double_pen()
  260.   {
  261.     //calculation of acceleration, velocity and position of double pendulum
  262.     counter1 = -(g) * (2 * mas1 + mas2) * sin(a1);
  263.     counter2 = -mas2 * (g)*sin(a1 - 2 * a2);
  264.     counter3 = -2 * sin(a1 - a2) * mas2;
  265.     counter4 = a2_vel * a2_vel * length2 + a1_vel * a1_vel * length1 * cos(a1 - a2);
  266.     den = length1 * (2 * mas1 + mas2 - mas2 * cos(2 * a1 - 2 * a2));
  267.     a1_a = (counter1 + counter2 + counter3 * counter4) / den;
  268.  
  269.     counter5 = 2 * sin(a1 - a2);
  270.     counter6 = (a1_vel * a1_vel * length1 * (mas1 + mas2));
  271.     counter7 = (g) * (mas1 + mas2) * cos(a1);
  272.     counter8 = a2_vel * a2_vel * length2 * mas2 * cos(a1 - a2);
  273.     den2 = length2 * (2 * mas1 + mas2 - mas2 * cos(2 * a1 - 2 * a2));
  274.     a2_a = (counter5 * (counter6 + counter7 + counter8)) / den2;
  275.  
  276.     position[0].x = length1 * sin(a1);
  277.     position[0].y = length1 * cos(a1);
  278.     position[0].add(origin);
  279.     position[1].x = position[0].x + length2 * sin(a2);
  280.     position[1].y = position[0].y + length2 * cos(a2);
  281.  
  282.     a1_vel += a1_a;
  283.     a2_vel += a2_a;
  284.     a1 += a1_vel;
  285.     a2 += a2_vel;
  286.  
  287.     a1_vel *= 0.999;
  288.     a2_vel *= 0.999;
  289.     px2 = position[1].x;
  290.     py2 = position[1].y;
  291.   }
  292.  
  293.   void setSpeed(PVector force, int number) {
  294.     PVector f = force.copy();
  295.  
  296.     switch(number) {
  297.  
  298.     case 0:
  299.       a1_a = mag(PVector.div(f, mass).x, PVector.div(f, mass).y);
  300.       a1_vel += a1_a;
  301.       position[0].add(a1_vel, a1_vel);
  302.  
  303.     case 1:
  304.       a2_a = mag(PVector.div(f, mass).x, PVector.div(f, mass).y);
  305.       a2_vel += a2_a;
  306.       position[1].add(a2_vel, a2_vel);
  307.     }
  308.   }
  309.  
  310.   void trace(int k, int ile)
  311.   {
  312.     iko = k;
  313.     double_pen();
  314.     point[i].x = position[1].x;
  315.     point[i].y = position[1].y;
  316.  
  317.     i++;
  318.  
  319.     if (i==point.length)
  320.     {
  321.       i = 0;
  322.     }
  323.  
  324.     stroke(100, 0, 100);
  325.     strokeWeight(6);
  326.     noFill();
  327.  
  328.     current = i;
  329.  
  330.     //drawing a trace
  331.     beginShape();
  332.     for (int j = 0; j<point.length; j++)
  333.     {
  334.       current++;
  335.  
  336.       if (current == point.length)
  337.       {
  338.         current = 0;
  339.       }
  340.  
  341.       stroke((map(j, 0, point.length, 50, 255)), (map(iko, 0, ile, 50, 255)), (map(current, 0, point.length, 50, 255)));
  342.  
  343.       curveVertex(point[current].x, point[current].y);
  344.     }
  345.     endShape();
  346.   }
  347.  
  348.   int showingData(int whichPart)
  349.   {
  350.     if (whichPart == 3)
  351.     {
  352.       fieldName[0]  = "radius";
  353.       fieldValue[0] = radius1;
  354.       fieldName[1]  = "mass";
  355.       fieldValue[1] = mas1;
  356.       fieldName[2]  = "gravity";
  357.       fieldValue[2] = g;
  358.     } else if (whichPart == 4)
  359.     {
  360.       fieldName[0]  = "radius";
  361.       fieldValue[0] = radius2;
  362.       fieldName[1]  = "mass";
  363.       fieldValue[1] = mas2;
  364.       fieldName[2]  = "gravity";
  365.       fieldValue[2] = g;
  366.     }
  367.  
  368.     return 3;
  369.   }
  370.  
  371.  
  372.   void drowing(int k, int ile)
  373.   {
  374.     //calculation of trace
  375.     if (doubleAction)
  376.     {
  377.       trace(k, ile);
  378.     }
  379.  
  380.     //finding the smallest and largest limit values used to change the color of objects
  381.     fun[0].findingTheItem(a1_vel);
  382.     fun[1].findingTheItem(a1_a);
  383.     fun[2].findingTheItem(a1);
  384.     fun[3].findingTheItem(a2_vel);
  385.     fun[4].findingTheItem(a2_a);
  386.     fun[5].findingTheItem(a2);
  387.  
  388.     //drawing double pendulum
  389.     strokeWeight(2);
  390.     stroke(255, 255, 0);
  391.     //drawing line conecting center to first ball, and to second
  392.     line(origin.x, origin.y, position[0].x, position[0].y);
  393.     line(position[0].x, position[0].y, position[1].x, position[1].y);
  394.     //drawing first ball
  395.     stroke(255, 0, 0);
  396.     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)));
  397.     circle(position[0].x, position[0].y, radius1 * 2);
  398.     //drawing second ball
  399.     stroke(0, 0, 255);
  400.     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)));
  401.     circle(position[1].x, position[1].y, radius2 * 2);
  402.     stroke(0, 0, 255);
  403.     fill(255, 0, 0);
  404.     circle(origin.x, origin.y, 10);
  405.   }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment