Advertisement
ahorsewithnoname

Versión final

May 9th, 2021
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.62 KB | None | 0 0
  1. //Importación de librerías
  2. import controlP5.*;
  3. import static javax.swing.JOptionPane.*;
  4.  
  5. //Objetos de clase
  6. ControlP5 cp5;
  7. Knob myKnobA;
  8. Knob myKnobB;
  9. Orb orb;
  10. Orb orb2;
  11. Orb orb3;
  12. Orb orb4;
  13. Orb orb5;
  14. Orb orb6;
  15. Orb orb7;
  16. Orb orb8;
  17. Orb orb9;
  18. Orb orb10;
  19.  
  20. //Enteros, floats y vectores
  21. PVector gravity = new PVector(0,0);
  22. int segments = 600;
  23. Ground[] ground = new Ground[segments];
  24. int radioModeThree = 170;
  25. int col = 255;
  26. int ncosas = 4;
  27. int radius = 100;
  28. int counter = 1;
  29. int sumar = 25;
  30. int tablade = 1;
  31. int prueba = 30;
  32. int modo = 5;
  33. int xa = 255;
  34. float sumangle = (2*PI)/ncosas;
  35. float angle = HALF_PI + PI + sumangle;
  36. float radio = 184;
  37. float tablaDel = 0;
  38. boolean overRect[] = new boolean[4];
  39. boolean pulsado[] = new boolean[4];
  40. boolean overButton = false;
  41. boolean overit = false;
  42. int x, y;
  43. float size = 1;
  44. int etapa = 0;
  45. int cont = 0;
  46. int contarmenu = 0;
  47.  
  48.  
  49. //Setup
  50.  
  51. void setup(){
  52.   size(400,400);
  53.   background(204,255,255);
  54.   textAlign(CENTER,CENTER);
  55.   fill(0);
  56.   stroke(0);
  57.   textSize(7);
  58.   cp5 = new ControlP5(this);
  59.  
  60.   //Las dos ruedas del modo 1
  61.   myKnobA = cp5.addKnob("Servo")
  62.                .setRange(1,120)
  63.                .setValue(1)
  64.                .setPosition(20,90)
  65.                .setRadius(30)
  66.                .setDragDirection(Knob.HORIZONTAL)
  67.                .setCaptionLabel("Divisiones\ndel circulo")
  68.                .setColorCaptionLabel(0)
  69.                ;
  70.            
  71.   myKnobB = cp5.addKnob("TablaD")
  72.                .setRange(1,20)
  73.                .setValue(1)
  74.                .setPosition(20,220)
  75.                .setRadius(30)
  76.                .setDragDirection(Knob.HORIZONTAL)
  77.                .setCaptionLabel("Tabla de")
  78.                .setColorCaptionLabel(0)
  79.                ;
  80.  
  81.   //Modo 3
  82.   PVector circle = new PVector(0,radioModeThree);
  83.  
  84.   orb = new Orb(200, 200, random(2,8));
  85.   orb2 = new Orb(200, 200, random(2,8));
  86.   orb3 = new Orb(200, 200, random(2,8));
  87.   orb4 = new Orb(200, 200, random(2,8));
  88.   orb5 = new Orb(200, 200, random(2,8));
  89.   orb6 = new Orb(200, 200, random(2,8));
  90.   orb7 = new Orb(200, 200, random(2,8));
  91.   orb8 = new Orb(200, 200, random(2,8));
  92.   orb9 = new Orb(200, 200, random(2,8));
  93.   orb10 = new Orb(200, 200, random(2,8));
  94.  
  95.   //Creación de la forma de la esfera como objeto (modo 3)
  96.   float[] peakHeights = new float[segments+1];
  97.   float[] peakWidths = new float[segments+1];
  98.   for (int i=0; i<peakHeights.length; i++){
  99.     circle.rotate((-TWO_PI-(QUARTER_PI/4))/(peakHeights.length));
  100.     peakWidths[i] = 200+circle.x;
  101.     peakHeights[i] = 200+circle.y;
  102.   }
  103.   for (int i=0; i<segments; i++){
  104.     ground[i]  = new Ground(peakWidths[i], peakHeights[i], peakWidths[i+1], peakHeights[i+1]);
  105.   }
  106. }
  107.  
  108. //Función principal
  109.  
  110. void draw(){
  111.   //println("mouseX: " + mouseX + " mouseY: " + mouseY);
  112.   for(int i = 0; i < 4; i++){
  113.     if(pulsado[i] == true){
  114.       modo = i+1;
  115.     if(modo == 1){
  116.       colorMode(RGB,255);
  117.       background(204,255,255);
  118.       strokeWeight(1);
  119.     }
  120.     if(modo == 2){
  121.       tablaDel = 0;
  122.       strokeWeight(1);
  123.     }
  124.     if(modo == 3){
  125.       background(0);
  126.     }
  127.     if(modo == 4){
  128.       rectMode(RADIUS);
  129.       textSize(20);
  130.       stroke(0);
  131.       textAlign(CENTER, CENTER);
  132.       x = 3*width/4;
  133.       y = 3*height/4;
  134.     }
  135.       background(204,255,255);
  136.     }
  137.   }
  138.   if(modo == 0){
  139.     menu();
  140.   }
  141.   if(modo == 1){
  142.     modo1();
  143.   }
  144.   if(modo == 2){
  145.     modo2();  
  146.     textSize(9);
  147.     fill(255);
  148.     textAlign(CENTER,CENTER);
  149.     text("Pulsa B en cualquier momento para regresar al menu",200,390);
  150.   }
  151.   if(modo == 3){
  152.     modo3();
  153.     textSize(9);
  154.     text("Pulsa B en cualquier momento para regresar al menu",200,390);
  155.   }
  156.   if(modo == 4){
  157.     modo4();
  158.     textSize(9);
  159.     text("Pulsa B en cualquier momento para regresar al menu",200,390);
  160.   }
  161.   if(modo == 5){
  162.     animacion();
  163.   }
  164. }
  165.  
  166. //Reacción al presionar teclas
  167.  
  168. void keyPressed(){
  169.   if (key == 'D' || key == 'd'){
  170.       Servo(int(myKnobA.getValue())+1);
  171.       myKnobA.setValue(int(myKnobA.getValue())+1);
  172.   }
  173.   if (key == 'A' || key == 'a'){
  174.       Servo(int(myKnobA.getValue())-1);
  175.       myKnobA.setValue(int(myKnobA.getValue())-1);
  176.   }
  177.   if (key == 'W' || key == 'w'){
  178.       TablaD(int(myKnobB.getValue())+1);
  179.       myKnobB.setValue(int(myKnobB.getValue())+1);
  180.   }
  181.   if (key == 'S' || key == 's'){
  182.       TablaD(int(myKnobB.getValue())-1);
  183.       myKnobB.setValue(int(myKnobB.getValue())-1);
  184.   }
  185.   if(key == 'B' || key == 'b'){
  186.     modo = 0;
  187.     colorMode(RGB,255);
  188.     background(204,255,255);
  189.    
  190.     strokeWeight(1);
  191.     stroke(0);
  192.    
  193.   }
  194.   if(key == 'i' || key == 'I'){
  195.     modo = 5;
  196.     size = 1;
  197.     etapa = 0;
  198.     cont = 0;
  199.     contarmenu = -1;
  200.   }
  201.   if(key == ' ' && modo == 5){
  202.     modo = 0;
  203.     size = 1;
  204.     etapa = 0;
  205.     cont = 0;
  206.     contarmenu = 0;
  207.   }
  208.   if(modo == 5) contarmenu += 1;
  209.  
  210. }
  211.  
  212. //Los 5 modos por separado
  213.  
  214. void animacion(){
  215.     myKnobA.hide();
  216.     myKnobB.hide();
  217.     if(etapa == 0){
  218.     fill(204,255,255);
  219.     noStroke();
  220.     rect(0,0,width,height);
  221.     stroke(0);
  222.     textAlign(CENTER,CENTER);
  223.     fill(0);
  224.     textSize(9);
  225.     text("Pulsa SPACE en cualquier momento para saltar la animación",200,390);
  226.     textSize(size);
  227.     text("Fractals, multiplication \ntables and light rays:\na Mandelbrot's tale",200,200);
  228.     if(size < 30) size+=0.5;
  229.     if(size == 30){
  230.       etapa = 1;
  231.       delay(4000);
  232.     }
  233.   }
  234.   if(etapa == 1){
  235.     fill(255,15);
  236.     noStroke();
  237.     rect(0,0,width,height);
  238.     stroke(0);
  239.     cont += 1;
  240.     if(cont == 130){
  241.       etapa = 2;
  242.       //delay(1000);
  243.     }
  244.   }
  245.   if(etapa == 2){
  246.     fill(204,255,255);
  247.     noStroke();
  248.     rect(0,0,width,height);
  249.     stroke(0);
  250.     fill(0);
  251.     textSize(30);
  252.     text("¿Qué es un fractal?",width/2,50);
  253.     textSize(10);
  254.     text("Un fractal es un objeto geométrico cuya estructura básica,\nfragmentada o aparentemente irregular,se repite\n a diferentes escalas.\n\nEl propio término fue acuñado por\nBenoît Mandelbrot, cuyo fractal\nveremos en este programa.",width/2,170);
  255.     textSize(12);
  256.     text("En el caso del Conjunto de Mandelbrot,\nse define fácilmente con una fórmula:\nz(n-1) = z(n)² + C,\ndonde C es el número complejo que queremos conocer\nsi pertenece al conjunto o no.",width/2,300);
  257.     textSize(9);
  258.     text("(Pulsa cualquier tecla para continuar)",width/2,390);
  259.     if(contarmenu == 1){
  260.       background(204,255,255);
  261.       textSize(12);
  262.       text("Existen infinitos conjuntos de Mandelbrot\ndependiendo del exponente que uses a la hora de calcularlo.\nAunque suele ser con exponente 2,\nse puede generalizar la fórmula.",width/2,height/4);
  263.       textSize(13);
  264.       text("Si buscas en Google podrás encontrar fácilmente\nimágenes de cualquier conjunto general\n de Mandelbrot.",width/2,250);
  265.       textSize(9);
  266.       text("(Pulsa cualquier tecla para continuar)",width/2,390);
  267.     }
  268.     if(contarmenu == 2){
  269.       etapa = 3;
  270.       contarmenu = 0;
  271.       delay(200);
  272.      
  273.     }
  274.   }
  275.   if(etapa == 3){
  276.     fill(204,255,255);
  277.     noStroke();
  278.     rect(0,0,width,height);
  279.     stroke(0);
  280.     fill(0);
  281.     textSize(30);
  282.     text("¿Qué son las tablas\nde multiplicar?",width/2,100);
  283.     textSize(13);
  284.     text("Es broma...\nimagino que a estas las conocerás.\nPero, ¿alguna vez te has preguntado\nqué pasaría si las representáramos\ngráficamente?",width/2,230);
  285.     textSize(9);
  286.     text("(Pulsa cualquier tecla para continuar)",width/2,390);
  287.     if(contarmenu == 1){
  288.       background(204,255,255);
  289.       textSize(20);
  290.       text("Para ello, dividiremos en varias partes\niguales un círculo como este:",width/2,60);
  291.       noFill();
  292.       circle(200,150,80);
  293.       text("Por ejemplo:",width/2,230);
  294.      
  295.       circle(200,300,80);
  296.       fill(0);
  297.       circle(200,260,4);
  298.       circle(200,340,4);
  299.       circle(240,300,4);
  300.       circle(160,300,4);
  301.       textSize(9);
  302.       text("(Pulsa cualquier tecla para continuar)",width/2,390);
  303.     }
  304.     if(contarmenu == 2){
  305.       background(204,255,255);
  306.       textSize(12);
  307.       text("Y luego uniremos cada punto\n con su correspondiente según\nla tabla que elijamos.",width/2,100);
  308.       text("Por ejemplo:\nSi hiciéramos la tabla del 2 con 4 nodos\nel 1 se uniría con el 2,\nel 2 se uniría con el 4,\nel 3 con el 6\n y el 4 con el 8.\n\nAunque solo se divida en 4 nodos,\npodremos calcular dónde caerían el 6 y el 8\npor geometría.",width/2,250);
  309.       textSize(9);
  310.       text("(Pulsa cualquier tecla para continuar)",width/2,390);
  311.     }
  312.     if(contarmenu == 3){
  313.       background(204,255,255);
  314.       textSize(13);
  315.       text("Tranquilo si no estás entendiendo nada...\nPronto lo verás en acción y entenderás todo.",width/2,height/2);
  316.       textSize(9);
  317.       text("(Pulsa cualquier tecla para continuar)",width/2,390);
  318.     }
  319.     if(contarmenu == 4){
  320.       etapa = 4;
  321.       contarmenu = 0;
  322.     }
  323.   }
  324.   if(etapa == 4){
  325.     fill(204,255,255);
  326.     noStroke();
  327.     rect(0,0,width,height);
  328.     stroke(0);
  329.     fill(0);
  330.     textSize(30);
  331.     text("Por último...\nRayos y reflexión.",width/2,80);
  332.     textSize(13);
  333.     text("Como tal vez conozcas si has estudiado\nalgo de física, cuando un rayo incide en\n una superficie se refleja con el mismo\nángulo con el que ha incidido\nrespecto a la normal, tal que así:",width/2,200);
  334.     pushMatrix();
  335.     translate(200,320);
  336.     strokeWeight(2);
  337.     line(-50,0,50,0);
  338.     strokeWeight(1);
  339.     stroke(0,0,255);
  340.     arrow(-30,-40,0,0);
  341.     stroke(255,0,0);
  342.     arrow(0,0,30,-40);
  343.     stroke(0);
  344.    
  345.     popMatrix();
  346.     textSize(9);
  347.     text("(Pulsa cualquier tecla para continuar)",width/2,390);
  348.     if(contarmenu == 1){
  349.       background(204,255,255);
  350.       textSize(13);
  351.       text("Pues bien, resulta sorprendente\nlo que pasa si lanzamos varios rayos\nen una superficie circular desde el centro...",width/2,200);
  352.       textSize(9);
  353.       text("(Pulsa cualquier tecla para continuar)",width/2,390);
  354.     }
  355.     if(contarmenu == 2){
  356.       background(204,255,255);
  357.       textSize(17);
  358.       text("Aunque parezca increíble,\nla figura que forman coincide\ncon el nodo principal\ndel conjunto de Mandelbrot!", 200,200);
  359.       textSize(9);
  360.       text("(Pulsa cualquier tecla para continuar)",width/2,390);
  361.     }
  362.     if(contarmenu == 3){
  363.       background(204,255,255);
  364.       textSize(20);
  365.       textAlign(CENTER,CENTER);
  366.       text("¿Dónde está la gracia\no la relación aquí?",200, 60);
  367.       textAlign(LEFT,CENTER);
  368.       textSize(14);
  369.       text("-No solo el conjunto de Mandelbrot con exponente 2\naparece en la tabla del 2, sino que TODOS\nlos conjuntos de Mandelbrot aparecen.\nEs decir, si buscas el conjunto de Mandelbrot\ncon exponente 3 y haces la tabla del 3\ncon el modo 1, verás que el módulo central coincide.\n\n-Aparecen, así, relacionados, tres\nfenómenos de ramas diferentes, como son\nlos fractales y las tablas de multiplicar (matemáticas)\ny la reflexión (óptica física).",20,230);
  370.       textAlign(CENTER,CENTER);
  371.       textSize(9);
  372.       text("(Pulsa cualquier tecla para continuar)",width/2,390);
  373.     }
  374.     if(contarmenu == 4){
  375.       background(204,255,255);
  376.       textSize(20);
  377.       textAlign(CENTER,CENTER);
  378.       text("A partir de aquí, podrás\nexperimentar por tí mismo...",200, 60);
  379.       textAlign(LEFT,CENTER);
  380.       textSize(14);
  381.       text("-En el modo 1 podrás crear tus propias\ntablas de multiplicar y jugar con ellas.\n\n-En el modo 2 podrás ver cómo cambian las tablas\nen forma de animación.\n\n-En el modo 3 podrás ver, de forma colorida,\nla reflexión de rayos de luz en una superficie circular.\n\n-En el modo 4 podrás calificar el trabajo.",20,230);
  382.       textAlign(CENTER,CENTER);
  383.       textSize(9);
  384.       text("(Pulsa cualquier tecla para continuar)",width/2,390);
  385.     }
  386.     if(contarmenu == 5){
  387.       modo = 0;
  388.     }
  389.   }
  390.   //println(etapa);
  391. }
  392.  
  393. void menu(){
  394.   colorMode(RGB);
  395.   myKnobA.hide();
  396.   myKnobB.hide();
  397.   background(204,255,255);
  398.   fill(0);
  399.   boton(width/4+30,height/2-30,45,0);
  400.   boton(3*width/4-30,height/2-30,45,3);
  401.   boton(width/4+30,3*height/4,45,1);
  402.   boton(3*width/4-30,3*height/4,45,2);
  403.   textAlign(CENTER,CENTER);
  404.   fill(0);
  405.   textSize(12);
  406.   text("Pulsa 'i' para volver a ver el tutorial",201,391);
  407.   fill(0, 102, 204);
  408.   text("Pulsa 'i' para volver a ver el tutorial",200,390);
  409.   textSize(35);
  410.   fill(0);
  411.   text("Selecciona un modo:",201,51);
  412.   fill(0, 102, 204);
  413.   text("Selecciona un modo:",200,50);
  414. }
  415.  
  416. void boton(float x, float y, float radius, int index){
  417.   rectMode(RADIUS);
  418.   fill(0);
  419.   square(x+3,y+3,radius);
  420.   if(mouseX > x-45 && mouseX < x+45 && mouseY < y + 45 && mouseY > y-45){
  421.     overRect[index] = true;
  422.     fill(0,128,255);
  423.   }
  424.   else{
  425.     overRect[index] = false;
  426.     fill(51, 102, 255);
  427.   }
  428.   if (pulsado[index] == true){
  429.     noFill();
  430.     noStroke();
  431.   }
  432.  
  433.   square(x,y,radius);
  434.   fill(0);
  435.   textSize(16);
  436.   text("Modo\n"+ (index+1),x,y);
  437.  
  438. }
  439.  
  440. void mousePressed(){
  441.   if(modo == 0){
  442.     for(int i = 0; i < 4; i++){
  443.       if(overRect[i]) pulsado[i] = true;
  444.     }
  445.   }
  446.   if(modo == 4) if(overButton == true) showMessageDialog(null, "Sabía que dirías que sí :D", "Info", INFORMATION_MESSAGE);
  447. }
  448.  
  449. void mouseReleased(){
  450.   for(int i = 0; i < 4; i++){
  451.     pulsado[i] = false;
  452.   }
  453. }
  454.  
  455.  
  456. void modo1(){
  457.   myKnobA.show();
  458.   myKnobB.show();
  459.   textSize(7);
  460.   //colorMode(RGB,0);
  461.   stroke(0);
  462.   if(counter <= ncosas){
  463.     dibujarTabla();
  464.   }
  465.   textSize(10);
  466.   text("Usa WASD para controlar las ruedas sin usar el ratón",200,20);
  467.   textSize(9);
  468.   strokeWeight(1);
  469.   fill(0);
  470.  
  471.   text("Pulsa B en cualquier momento para regresar al menu",200,390);
  472. }
  473.  
  474. void modo2(){
  475.   myKnobA.hide();
  476.   myKnobB.hide();
  477.   colorMode(HSB, 360, 100, 100);
  478.   textAlign(CENTER, CENTER);
  479.   textSize(17);
  480.   background(0);
  481.   fill(255);
  482.   text("Tabla del\n" + nfc(tablaDel,2),45,40);
  483.   xa--;
  484.   if(xa==0)xa=360;
  485.   //println(xa);
  486.  
  487.   int total = int(map(mouseX,0, width, 0, 200));
  488.  
  489.   tablaDel += 0.008;
  490.   pushMatrix();
  491.   translate(width/2, height/2);
  492.   stroke(xa, 255, 255);
  493.   noFill();
  494.   circle(0,0,radio*2);
  495.   for(int i = 0; i < total; i++){
  496.     PVector v = crearVector(i,total);
  497.     fill(255);
  498.     circle(v.x, v.y, 8);
  499.   }
  500.  
  501.   for(int i = 0; i < total; i++){
  502.     PVector a = crearVector(i,total);
  503.     PVector b = crearVector(i * tablaDel,total);
  504.     line(a.x,a.y,b.x,b.y);
  505.   }
  506.   popMatrix();
  507. }
  508.  
  509. void modo3(){
  510.   myKnobA.hide();
  511.   myKnobB.hide();
  512.   ////println("mousex: "+mouseX+" mousey: "+mouseY);
  513.   noStroke();
  514.   fill(0, 15);
  515.   rect(0, 0, width, height);
  516.  
  517.   for (int i=0; i<segments; i++){
  518.     orb.checkGroundCollision(ground[i]);
  519.     orb2.checkGroundCollision(ground[i]);
  520.     orb3.checkGroundCollision(ground[i]);
  521.     orb4.checkGroundCollision(ground[i]);
  522.     orb5.checkGroundCollision(ground[i]);
  523.     orb6.checkGroundCollision(ground[i]);
  524.     orb7.checkGroundCollision(ground[i]);
  525.     orb8.checkGroundCollision(ground[i]);
  526.     orb9.checkGroundCollision(ground[i]);
  527.     orb10.checkGroundCollision(ground[i]);
  528.   }
  529.  
  530.   beginShape();
  531.   for (int i=0; i<segments; i++){
  532.     vertex(ground[i].x1, ground[i].y1);
  533.     vertex(ground[i].x2, ground[i].y2);
  534.   }
  535.   vertex(ground[segments-1].x2, height);
  536.   vertex(ground[0].x1, height);
  537.   endShape(CLOSE);
  538.  
  539.   orb.move();
  540.   orb.display();
  541.  
  542.   orb2.move();
  543.   orb2.display();
  544.  
  545.   orb3.move();
  546.   orb3.display();
  547.  
  548.   orb4.move();
  549.   orb4.display();
  550.  
  551.   orb5.move();
  552.   orb5.display();
  553.  
  554.   orb6.move();
  555.   orb6.display();
  556.  
  557.   orb7.move();
  558.   orb7.display();
  559.  
  560.   orb8.move();
  561.   orb8.display();
  562.  
  563.   orb9.move();
  564.   orb9.display();
  565.  
  566.   orb10.move();
  567.   orb10.display();
  568.  
  569.   stroke(200);
  570.   noFill();
  571.   strokeWeight(7);
  572.   circle(width/2,height/2,radioModeThree*2);
  573. }
  574.  
  575. void modo4(){
  576.   background(255);
  577.   //println(mouseY);
  578.   textSize(20);
  579.   //isitover(mouseX,mouseY);
  580.   if(mouseX > x-90 && mouseX < x+90 && mouseY > y-20 && mouseY < y+20){
  581.     do{
  582.         x = int(random(110,width-110));
  583.         y = int(random(210,height-20));
  584.         isitover(x,y);
  585.     }while(overit == true);
  586.     ////println("x: " + x + " y: " + y);
  587.   }
  588.   boton(x,y,"No");
  589.   boton(width/4,3*height/4,"Si");
  590. }
  591.  
  592. void boton(int posx, int posy, String texto){
  593.   fill(255);
  594.   if(mouseX > posx-90 && mouseX < posx+90 && mouseY > posy-20 && mouseY < posy+20){
  595.     stroke(0,0,255);
  596.     overButton = true;
  597.   }
  598.   else{
  599.     overButton = false;
  600.     stroke(0);
  601.   }
  602.   rect(posx,posy,90,20);
  603.   fill(0);
  604.   text(texto,posx,posy);
  605.   textSize(40);
  606.   text("¿Pondrías un 10\na este trabajo?",width/2,120);
  607.   textSize(20);
  608. }
  609.  
  610. //Funciones utilizadas a lo largo de los modos (subordinadas)
  611.  
  612. void isitover(int x, int y){
  613.   //rect(x,y,90,20);
  614.   if((x-89 > (width/4)-90 && x-89 < (width/4)+90 && y-19 > (3*height/4)-20 && y-19 < (3*height/4)+20)||(x+89 > (width/4)-90 && x+89 < (width/4)+90 && y-19 > (3*height/4)-20 && y-19 < (3*height/4)+20)||(x+89 > (width/4)-90 && x+89 < (width/4)+90 && y+19 > (3*height/4)-20 && y+19 < (3*height/4)+20)||(x-89 > (width/4)-90 && x-89 < (width/4)+90 && y+19 > (3*height/4)-20 && y+19 < (3*height/4)+20)){
  615.     overit = true;
  616.    
  617.   }
  618.   else overit = false;
  619.   //println(overit);
  620. }
  621.  
  622. PVector crearVector(float indice, float total){ //Para dividir un círculo en partes iguales (modo 1)
  623.   float angle = map(indice % total, 0, total, 0, TWO_PI);
  624.   PVector v = PVector.fromAngle(angle + PI);
  625.   v.mult(radio);
  626.   return v;
  627. }
  628.  
  629. void Servo(int theValue){ //Rueda del número de divisiones (modo 1)
  630.   background(204,255,255);
  631.   fill(0);
  632.   counter = 1;
  633.   ncosas = theValue;
  634.   sumangle = (2*PI)/ncosas;
  635.   angle = HALF_PI + PI + sumangle;
  636. }
  637.  
  638. void TablaD(int theValue){ //Rueda de la tabla de multiplicar (modo 1)
  639.   background(204,255,255);
  640.   fill(0);
  641.   counter = 1;
  642.   tablade = theValue;
  643.   angle = HALF_PI + PI + sumangle;
  644. }
  645.  
  646.  
  647. void dibujarTabla(){ //Dibuja la tabla paso a paso
  648.   pushMatrix();
  649.   translate(1.1*width/2,height/2);
  650.   circle(radius*sin(angle),radius*cos(angle),2);
  651.   //if(counter*tablade<=ncosas){
  652.   line(radius*sin(angle),radius*cos(angle),radius*sin((counter*tablade*sumangle)+(angle-counter*sumangle)),radius*cos((counter*tablade*sumangle)+(angle-counter*sumangle)));
  653.   //}
  654.   //println(counter*tablade + " -> " + counter*tablade*degrees(sumangle));
  655.   text(counter,(radius+sumar)*sin(angle),(radius+sumar)*cos(angle));
  656.   angle += sumangle;
  657.   counter++;
  658.   popMatrix();
  659. }
  660.  
  661. void arrow(float x1, float y1, float x2, float y2) {
  662.   line(x1, y1, x2, y2);
  663.   pushMatrix();
  664.   translate(x2, y2);
  665.   float a = atan2(x1-x2, y2-y1);
  666.   rotate(a);
  667.   line(0, 0, -3, -3);
  668.   line(0, 0, 3, -3);
  669.   popMatrix();
  670. }
  671.  
  672. //Clases
  673.  
  674. class Orb {
  675.   PVector position;
  676.   PVector velocity;
  677.   float r;
  678.  
  679.   Orb(float x, float y, float r_) {
  680.     position = new PVector(x, y);
  681.     velocity = new PVector(random(1,5), random(1,5));
  682.     velocity.rotate(random(0,TWO_PI));
  683.     r = r_;
  684.   }
  685.  
  686.     void move() {
  687.       velocity.add(gravity);
  688.       position.add(velocity);
  689.       if(position.x < 25 || position.x > 375){
  690.         position.set(width/2,height/2);
  691.         velocity.rotate(random(0,TWO_PI));
  692.       }
  693.     }
  694.  
  695.     void display() {
  696.       colorMode(HSB, 360, 100, 100);
  697.    
  698.       col--;
  699.       if(col==0)col=360;
  700.       noStroke();
  701.       fill(col,255,255);
  702.       ellipse(position.x, position.y, r*2, r*2);
  703.       colorMode(RGB,255);
  704.     }
  705.  
  706.   void checkGroundCollision(Ground groundSegment) {
  707.     float deltaX = position.x - groundSegment.x;
  708.     float deltaY = position.y - groundSegment.y;
  709.     float cosine = cos(groundSegment.rot);
  710.     float sine = sin(groundSegment.rot);
  711.  
  712.     float groundXTemp = cosine * deltaX + sine * deltaY;
  713.     float groundYTemp = cosine * deltaY - sine * deltaX;
  714.     float velocityXTemp = cosine * velocity.x + sine * velocity.y;
  715.     float velocityYTemp = cosine * velocity.y - sine * velocity.x;
  716.  
  717.     if (groundYTemp > -r && position.x >= groundSegment.x1 && position.x <= groundSegment.x2 ) {
  718.       groundYTemp = -r;
  719.       velocityYTemp *= -1.0;
  720.     }
  721.     if (groundYTemp > -r && position.x <= groundSegment.x1 && position.x >= groundSegment.x2 ) {
  722.       groundYTemp = -r;
  723.       velocityYTemp *= -1.0;
  724.     }
  725.  
  726.     deltaX = cosine * groundXTemp - sine * groundYTemp;
  727.     deltaY = cosine * groundYTemp + sine * groundXTemp;
  728.     velocity.x = cosine * velocityXTemp - sine * velocityYTemp;
  729.     velocity.y = cosine * velocityYTemp + sine * velocityXTemp;
  730.     position.x = groundSegment.x + deltaX;
  731.     position.y = groundSegment.y + deltaY;
  732.   }
  733. }
  734.  
  735. class Ground {
  736.   float x1, y1, x2, y2;  
  737.   float x, y, len, rot;
  738.   Ground(float x1, float y1, float x2, float y2) {
  739.     this.x1 = x1;
  740.     this.y1 = y1;
  741.     this.x2 = x2;
  742.     this.y2 = y2;
  743.     x = (x1+x2)/2;
  744.     y = (y1+y2)/2;
  745.     len = dist(x1, y1, x2, y2);
  746.     rot = atan2((y2-y1), (x2-x1));
  747.   }
  748. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement