Advertisement
ahorsewithnoname

ProyectoFinalSinMenu

May 6th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.00 KB | None | 0 0
  1. //Importación de librerías
  2. import controlP5.*;
  3.  
  4. //Objetos de clase
  5. ControlP5 cp5;
  6. Knob myKnobA;
  7. Knob myKnobB;
  8. Orb orb;
  9. Orb orb2;
  10. Orb orb3;
  11. Orb orb4;
  12. Orb orb5;
  13. Orb orb6;
  14. Orb orb7;
  15. Orb orb8;
  16. Orb orb9;
  17. Orb orb10;
  18.  
  19. //Enteros, floats y vectores
  20. PVector gravity = new PVector(0,0);
  21. int segments = 600;
  22. Ground[] ground = new Ground[segments];
  23. int radioModeThree = 170;
  24. int col = 255;
  25. int ncosas = 4;
  26. int radius = 100;
  27. int counter = 1;
  28. int sumar = 25;
  29. int tablade = 1;
  30. int prueba = 30;
  31. int modo = 1;
  32. int x = 255;
  33. float sumangle = (2*PI)/ncosas;
  34. float angle = HALF_PI + PI + sumangle;
  35. float radio = 184;
  36. float tablaDel = 0;
  37.  
  38.  
  39. //Setup
  40. void setup(){
  41.   size(400,400);
  42.   background(255);
  43.   textAlign(CENTER,CENTER);
  44.   fill(0);
  45.   stroke(0);
  46.   textSize(7);
  47.   cp5 = new ControlP5(this);
  48.  
  49.   //Las dos ruedas del modo 1
  50.   myKnobA = cp5.addKnob("Servo")
  51.                .setRange(1,120)
  52.                .setValue(1)
  53.                .setPosition(20,90)
  54.                .setRadius(30)
  55.                .setDragDirection(Knob.HORIZONTAL)
  56.                .setCaptionLabel("N")
  57.                .setColorCaptionLabel(0)
  58.                ;
  59.            
  60.   myKnobB = cp5.addKnob("TablaD")
  61.                .setRange(1,20)
  62.                .setValue(1)
  63.                .setPosition(20,220)
  64.                .setRadius(30)
  65.                .setDragDirection(Knob.HORIZONTAL)
  66.                .setCaptionLabel("Tabla de")
  67.                .setColorCaptionLabel(0)
  68.                ;
  69.  
  70.   //Modo 3
  71.   PVector circle = new PVector(0,radioModeThree);
  72.  
  73.   orb = new Orb(200, 200, random(2,8));
  74.   orb2 = new Orb(200, 200, random(2,8));
  75.   orb3 = new Orb(200, 200, random(2,8));
  76.   orb4 = new Orb(200, 200, random(2,8));
  77.   orb5 = new Orb(200, 200, random(2,8));
  78.   orb6 = new Orb(200, 200, random(2,8));
  79.   orb7 = new Orb(200, 200, random(2,8));
  80.   orb8 = new Orb(200, 200, random(2,8));
  81.   orb9 = new Orb(200, 200, random(2,8));
  82.   orb10 = new Orb(200, 200, random(2,8));
  83.  
  84.   //Creación de la forma de la esfera como objeto (modo 3)
  85.   float[] peakHeights = new float[segments+1];
  86.   float[] peakWidths = new float[segments+1];
  87.   for (int i=0; i<peakHeights.length; i++){
  88.     circle.rotate((-TWO_PI-(QUARTER_PI/4))/(peakHeights.length));
  89.     peakWidths[i] = 200+circle.x;
  90.     peakHeights[i] = 200+circle.y;
  91.    
  92.   }
  93.   for (int i=0; i<segments; i++){
  94.     ground[i]  = new Ground(peakWidths[i], peakHeights[i], peakWidths[i+1], peakHeights[i+1]);
  95.   }
  96.  
  97. }
  98.  
  99. //Función principal
  100.  
  101. void draw(){
  102.   if(modo == 1){
  103.     modo1();
  104.   }
  105.   if(modo == 2){
  106.     modo2();
  107.   }
  108.   if(modo == 3){
  109.     modo3();
  110.   }
  111. }
  112.  
  113. //Reacción al presionar teclas
  114.  
  115. void keyPressed(){
  116.   if (key == 'D' || key == 'd'){
  117.       Servo(int(myKnobA.getValue())+1);
  118.       myKnobA.setValue(int(myKnobA.getValue())+1);
  119.   }
  120.   if (key == 'A' || key == 'a'){
  121.       Servo(int(myKnobA.getValue())-1);
  122.       myKnobA.setValue(int(myKnobA.getValue())-1);
  123.   }
  124.   if (key == 'W' || key == 'w'){
  125.       TablaD(int(myKnobB.getValue())+1);
  126.       myKnobB.setValue(int(myKnobB.getValue())+1);
  127.   }
  128.   if (key == 'S' || key == 's'){
  129.       TablaD(int(myKnobB.getValue())-1);
  130.       myKnobB.setValue(int(myKnobB.getValue())-1);
  131.   }
  132.   if (key == 'Y' || key == 'y'){
  133.    
  134.     modo += 1;
  135.     if(modo > 3){
  136.       modo = 1;
  137.       colorMode(RGB,255);
  138.       background(255);
  139.       strokeWeight(1);
  140.     }
  141.     if(modo == 2){
  142.       tablaDel = 0;
  143.     }
  144.     if(modo == 3){
  145.       background(0);
  146.     }
  147.   }
  148. }
  149.  
  150. //Los 3 modos por separado
  151.  
  152. void modo1(){
  153.   myKnobA.show();
  154.   myKnobB.show();
  155.   textSize(7);
  156.   //colorMode(RGB,0);
  157.   stroke(0);
  158.   if(counter <= ncosas){
  159.     dibujarTabla();
  160.   }
  161. }
  162.  
  163. void modo2(){
  164.   myKnobA.hide();
  165.   myKnobB.hide();
  166.   colorMode(HSB, 360, 100, 100);
  167.   textAlign(CENTER, CENTER);
  168.   textSize(17);
  169.   background(0);
  170.   fill(255);
  171.   text("Tabla del\n" + nfc(tablaDel,2),45,40);
  172.   x--;
  173.   if(x==0)x=360;
  174.   println(x);
  175.  
  176.   int total = int(map(mouseX,0, width, 0, 200));
  177.  
  178.   tablaDel += 0.008;
  179.  
  180.   translate(width/2, height/2);
  181.   stroke(x, 255, 255);
  182.   noFill();
  183.   circle(0,0,radio*2);
  184.   for(int i = 0; i < total; i++){
  185.     PVector v = crearVector(i,total);
  186.     fill(255);
  187.     circle(v.x, v.y, 8);
  188.   }
  189.  
  190.   for(int i = 0; i < total; i++){
  191.     PVector a = crearVector(i,total);
  192.     PVector b = crearVector(i * tablaDel,total);
  193.     line(a.x,a.y,b.x,b.y);
  194.   }
  195. }
  196.  
  197. void modo3(){
  198.   //println("mousex: "+mouseX+" mousey: "+mouseY);
  199.   noStroke();
  200.   fill(0, 15);
  201.   rect(0, 0, width, height);
  202.  
  203.   for (int i=0; i<segments; i++){
  204.     orb.checkGroundCollision(ground[i]);
  205.     orb2.checkGroundCollision(ground[i]);
  206.     orb3.checkGroundCollision(ground[i]);
  207.     orb4.checkGroundCollision(ground[i]);
  208.     orb5.checkGroundCollision(ground[i]);
  209.     orb6.checkGroundCollision(ground[i]);
  210.     orb7.checkGroundCollision(ground[i]);
  211.     orb8.checkGroundCollision(ground[i]);
  212.     orb9.checkGroundCollision(ground[i]);
  213.     orb10.checkGroundCollision(ground[i]);
  214.   }
  215.  
  216.   beginShape();
  217.   for (int i=0; i<segments; i++){
  218.     vertex(ground[i].x1, ground[i].y1);
  219.     vertex(ground[i].x2, ground[i].y2);
  220.   }
  221.   vertex(ground[segments-1].x2, height);
  222.   vertex(ground[0].x1, height);
  223.   endShape(CLOSE);
  224.  
  225.   orb.move();
  226.   orb.display();
  227.  
  228.   orb2.move();
  229.   orb2.display();
  230.  
  231.   orb3.move();
  232.   orb3.display();
  233.  
  234.   orb4.move();
  235.   orb4.display();
  236.  
  237.   orb5.move();
  238.   orb5.display();
  239.  
  240.   orb6.move();
  241.   orb6.display();
  242.  
  243.   orb7.move();
  244.   orb7.display();
  245.  
  246.   orb8.move();
  247.   orb8.display();
  248.  
  249.   orb9.move();
  250.   orb9.display();
  251.  
  252.   orb10.move();
  253.   orb10.display();
  254.  
  255.   stroke(200);
  256.   noFill();
  257.   strokeWeight(7);
  258.   circle(width/2,height/2,radioModeThree*2);
  259. }
  260.  
  261. //Funciones utilizadas a lo largo de los modos (subordinadas)
  262.  
  263. PVector crearVector(float indice, float total){ //Para dividir un círculo en partes iguales (modo 1)
  264.   float angle = map(indice % total, 0, total, 0, TWO_PI);
  265.   PVector v = PVector.fromAngle(angle + PI);
  266.   v.mult(radio);
  267.   return v;
  268. }
  269.  
  270. void Servo(int theValue){ //Rueda del número de divisiones (modo 1)
  271.   background(255);
  272.   fill(0);
  273.   counter = 1;
  274.   ncosas = theValue;
  275.   sumangle = (2*PI)/ncosas;
  276.   angle = HALF_PI + PI + sumangle;
  277. }
  278.  
  279. void TablaD(int theValue){ //Rueda de la tabla de multiplicar (modo 1)
  280.   background(255);
  281.   counter = 1;
  282.   tablade = theValue;
  283.   angle = HALF_PI + PI + sumangle;
  284. }
  285.  
  286.  
  287. void dibujarTabla(){ //Dibuja la tabla paso a paso
  288.   pushMatrix();
  289.   translate(1.1*width/2,height/2);
  290.   circle(radius*sin(angle),radius*cos(angle),2);
  291.   //if(counter*tablade<=ncosas){
  292.   line(radius*sin(angle),radius*cos(angle),radius*sin((counter*tablade*sumangle)+(angle-counter*sumangle)),radius*cos((counter*tablade*sumangle)+(angle-counter*sumangle)));
  293.   //}
  294.   println(counter*tablade + " -> " + counter*tablade*degrees(sumangle));
  295.   text(counter,(radius+sumar)*sin(angle),(radius+sumar)*cos(angle));
  296.   angle += sumangle;
  297.   counter++;
  298.   popMatrix();
  299. }
  300.  
  301.  
  302. //Clases
  303.  
  304. class Orb {
  305.   PVector position;
  306.   PVector velocity;
  307.   float r;
  308.  
  309.   Orb(float x, float y, float r_) {
  310.     position = new PVector(x, y);
  311.     velocity = new PVector(random(1,5), random(1,5));
  312.     velocity.rotate(random(0,TWO_PI));
  313.     r = r_;
  314.   }
  315.  
  316.     void move() {
  317.       velocity.add(gravity);
  318.       position.add(velocity);
  319.       if(position.x < 25 || position.x > 375){
  320.         position.set(width/2,height/2);
  321.         velocity.rotate(random(0,TWO_PI));
  322.       }
  323.     }
  324.  
  325.     void display() {
  326.       colorMode(HSB, 360, 100, 100);
  327.    
  328.       col--;
  329.       if(col==0)col=360;
  330.       noStroke();
  331.       fill(col,255,255);
  332.       ellipse(position.x, position.y, r*2, r*2);
  333.       colorMode(RGB,255);
  334.     }
  335.  
  336.   void checkGroundCollision(Ground groundSegment) {
  337.     float deltaX = position.x - groundSegment.x;
  338.     float deltaY = position.y - groundSegment.y;
  339.     float cosine = cos(groundSegment.rot);
  340.     float sine = sin(groundSegment.rot);
  341.  
  342.     float groundXTemp = cosine * deltaX + sine * deltaY;
  343.     float groundYTemp = cosine * deltaY - sine * deltaX;
  344.     float velocityXTemp = cosine * velocity.x + sine * velocity.y;
  345.     float velocityYTemp = cosine * velocity.y - sine * velocity.x;
  346.  
  347.     if (groundYTemp > -r && position.x >= groundSegment.x1 && position.x <= groundSegment.x2 ) {
  348.       groundYTemp = -r;
  349.       velocityYTemp *= -1.0;
  350.     }
  351.     if (groundYTemp > -r && position.x <= groundSegment.x1 && position.x >= groundSegment.x2 ) {
  352.       groundYTemp = -r;
  353.       velocityYTemp *= -1.0;
  354.     }
  355.  
  356.     deltaX = cosine * groundXTemp - sine * groundYTemp;
  357.     deltaY = cosine * groundYTemp + sine * groundXTemp;
  358.     velocity.x = cosine * velocityXTemp - sine * velocityYTemp;
  359.     velocity.y = cosine * velocityYTemp + sine * velocityXTemp;
  360.     position.x = groundSegment.x + deltaX;
  361.     position.y = groundSegment.y + deltaY;
  362.   }
  363. }
  364.  
  365. class Ground {
  366.   float x1, y1, x2, y2;  
  367.   float x, y, len, rot;
  368.   Ground(float x1, float y1, float x2, float y2) {
  369.     this.x1 = x1;
  370.     this.y1 = y1;
  371.     this.x2 = x2;
  372.     this.y2 = y2;
  373.     x = (x1+x2)/2;
  374.     y = (y1+y2)/2;
  375.     len = dist(x1, y1, x2, y2);
  376.     rot = atan2((y2-y1), (x2-x1));
  377.   }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement