Advertisement
ahorsewithnoname

ultimaversionconmenu

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