Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Importación de librerías
- import controlP5.*;
- //Objetos de clase
- ControlP5 cp5;
- Knob myKnobA;
- Knob myKnobB;
- Orb orb;
- Orb orb2;
- Orb orb3;
- Orb orb4;
- Orb orb5;
- Orb orb6;
- Orb orb7;
- Orb orb8;
- Orb orb9;
- Orb orb10;
- //Enteros, floats y vectores
- PVector gravity = new PVector(0,0);
- int segments = 600;
- Ground[] ground = new Ground[segments];
- int radioModeThree = 170;
- int col = 255;
- int ncosas = 4;
- int radius = 100;
- int counter = 1;
- int sumar = 25;
- int tablade = 1;
- int prueba = 30;
- int modo = 0;
- int x = 255;
- float sumangle = (2*PI)/ncosas;
- float angle = HALF_PI + PI + sumangle;
- float radio = 184;
- float tablaDel = 0;
- boolean overRect[] = new boolean[4];
- boolean pulsado[] = new boolean[4];
- //Setup
- void setup(){
- size(400,400);
- background(255);
- textAlign(CENTER,CENTER);
- fill(0);
- stroke(0);
- textSize(7);
- cp5 = new ControlP5(this);
- //Las dos ruedas del modo 1
- myKnobA = cp5.addKnob("Servo")
- .setRange(1,120)
- .setValue(1)
- .setPosition(20,90)
- .setRadius(30)
- .setDragDirection(Knob.HORIZONTAL)
- .setCaptionLabel("N")
- .setColorCaptionLabel(0)
- ;
- myKnobB = cp5.addKnob("TablaD")
- .setRange(1,20)
- .setValue(1)
- .setPosition(20,220)
- .setRadius(30)
- .setDragDirection(Knob.HORIZONTAL)
- .setCaptionLabel("Tabla de")
- .setColorCaptionLabel(0)
- ;
- //Modo 3
- PVector circle = new PVector(0,radioModeThree);
- orb = new Orb(200, 200, random(2,8));
- orb2 = new Orb(200, 200, random(2,8));
- orb3 = new Orb(200, 200, random(2,8));
- orb4 = new Orb(200, 200, random(2,8));
- orb5 = new Orb(200, 200, random(2,8));
- orb6 = new Orb(200, 200, random(2,8));
- orb7 = new Orb(200, 200, random(2,8));
- orb8 = new Orb(200, 200, random(2,8));
- orb9 = new Orb(200, 200, random(2,8));
- orb10 = new Orb(200, 200, random(2,8));
- //Creación de la forma de la esfera como objeto (modo 3)
- float[] peakHeights = new float[segments+1];
- float[] peakWidths = new float[segments+1];
- for (int i=0; i<peakHeights.length; i++){
- circle.rotate((-TWO_PI-(QUARTER_PI/4))/(peakHeights.length));
- peakWidths[i] = 200+circle.x;
- peakHeights[i] = 200+circle.y;
- }
- for (int i=0; i<segments; i++){
- ground[i] = new Ground(peakWidths[i], peakHeights[i], peakWidths[i+1], peakHeights[i+1]);
- }
- }
- //Función principal
- void draw(){
- println("mouseX: " + mouseX + " mouseY: " + mouseY);
- for(int i = 0; i < 4; i++){
- if(pulsado[i] == true){
- modo = i+1;
- background(255);
- }
- }
- if(modo == 0){
- menu();
- }
- if(modo == 1){
- modo1();
- }
- if(modo == 2){
- modo2();
- }
- if(modo == 3){
- modo3();
- }
- }
- //Reacción al presionar teclas
- void keyPressed(){
- if (key == 'D' || key == 'd'){
- Servo(int(myKnobA.getValue())+1);
- myKnobA.setValue(int(myKnobA.getValue())+1);
- }
- if (key == 'A' || key == 'a'){
- Servo(int(myKnobA.getValue())-1);
- myKnobA.setValue(int(myKnobA.getValue())-1);
- }
- if (key == 'W' || key == 'w'){
- TablaD(int(myKnobB.getValue())+1);
- myKnobB.setValue(int(myKnobB.getValue())+1);
- }
- if (key == 'S' || key == 's'){
- TablaD(int(myKnobB.getValue())-1);
- myKnobB.setValue(int(myKnobB.getValue())-1);
- }
- if(key == '4'){
- modo = 0;
- }
- if (key == 'Y' || key == 'y'){
- modo += 1;
- if(modo > 3){
- modo = 1;
- colorMode(RGB,255);
- background(255);
- strokeWeight(1);
- }
- if(modo == 2){
- tablaDel = 0;
- }
- if(modo == 3){
- background(0);
- }
- }
- }
- //Los 3 modos por separado
- void menu(){
- myKnobA.hide();
- myKnobB.hide();
- fill(0);
- boton(width/4+30,height/2-30,45,0);
- boton(3*width/4-30,height/2-30,45,1);
- boton(width/4+30,3*height/4,45,2);
- boton(3*width/4-30,3*height/4,45,3);
- }
- void boton(float x, float y, float radius, int index){
- rectMode(RADIUS);
- fill(0);
- square(x+3,y+3,radius);
- if(mouseX > x-45 && mouseX < x+45 && mouseY < y + 45 && mouseY > y-45){
- overRect[index] = true;
- fill(0,0,255);
- }
- else{
- overRect[index] = false;
- fill(0,70,180);
- }
- if (pulsado[index] == true){
- noFill();
- noStroke();
- }
- else{
- fill(0,70,180);
- stroke(0);
- }
- square(x,y,radius);
- fill(0);
- textSize(16);
- text("Módulo\n"+ (index+1),x,y);
- }
- void mousePressed(){
- for(int i = 0; i < 4; i++){
- if(overRect[i]) pulsado[i] = true;
- }
- }
- void mouseReleased(){
- for(int i = 0; i < 4; i++){
- pulsado[i] = false;
- }
- }
- void modo1(){
- myKnobA.show();
- myKnobB.show();
- textSize(7);
- //colorMode(RGB,0);
- stroke(0);
- if(counter <= ncosas){
- dibujarTabla();
- }
- }
- void modo2(){
- myKnobA.hide();
- myKnobB.hide();
- colorMode(HSB, 360, 100, 100);
- textAlign(CENTER, CENTER);
- textSize(17);
- background(0);
- fill(255);
- text("Tabla del\n" + nfc(tablaDel,2),45,40);
- x--;
- if(x==0)x=360;
- println(x);
- int total = int(map(mouseX,0, width, 0, 200));
- tablaDel += 0.008;
- translate(width/2, height/2);
- stroke(x, 255, 255);
- noFill();
- circle(0,0,radio*2);
- for(int i = 0; i < total; i++){
- PVector v = crearVector(i,total);
- fill(255);
- circle(v.x, v.y, 8);
- }
- for(int i = 0; i < total; i++){
- PVector a = crearVector(i,total);
- PVector b = crearVector(i * tablaDel,total);
- line(a.x,a.y,b.x,b.y);
- }
- }
- void modo3(){
- myKnobA.hide();
- myKnobB.hide();
- //println("mousex: "+mouseX+" mousey: "+mouseY);
- noStroke();
- fill(0, 15);
- rect(0, 0, width, height);
- for (int i=0; i<segments; i++){
- orb.checkGroundCollision(ground[i]);
- orb2.checkGroundCollision(ground[i]);
- orb3.checkGroundCollision(ground[i]);
- orb4.checkGroundCollision(ground[i]);
- orb5.checkGroundCollision(ground[i]);
- orb6.checkGroundCollision(ground[i]);
- orb7.checkGroundCollision(ground[i]);
- orb8.checkGroundCollision(ground[i]);
- orb9.checkGroundCollision(ground[i]);
- orb10.checkGroundCollision(ground[i]);
- }
- beginShape();
- for (int i=0; i<segments; i++){
- vertex(ground[i].x1, ground[i].y1);
- vertex(ground[i].x2, ground[i].y2);
- }
- vertex(ground[segments-1].x2, height);
- vertex(ground[0].x1, height);
- endShape(CLOSE);
- orb.move();
- orb.display();
- orb2.move();
- orb2.display();
- orb3.move();
- orb3.display();
- orb4.move();
- orb4.display();
- orb5.move();
- orb5.display();
- orb6.move();
- orb6.display();
- orb7.move();
- orb7.display();
- orb8.move();
- orb8.display();
- orb9.move();
- orb9.display();
- orb10.move();
- orb10.display();
- stroke(200);
- noFill();
- strokeWeight(7);
- circle(width/2,height/2,radioModeThree*2);
- }
- //Funciones utilizadas a lo largo de los modos (subordinadas)
- PVector crearVector(float indice, float total){ //Para dividir un círculo en partes iguales (modo 1)
- float angle = map(indice % total, 0, total, 0, TWO_PI);
- PVector v = PVector.fromAngle(angle + PI);
- v.mult(radio);
- return v;
- }
- void Servo(int theValue){ //Rueda del número de divisiones (modo 1)
- background(255);
- fill(0);
- counter = 1;
- ncosas = theValue;
- sumangle = (2*PI)/ncosas;
- angle = HALF_PI + PI + sumangle;
- }
- void TablaD(int theValue){ //Rueda de la tabla de multiplicar (modo 1)
- background(255);
- counter = 1;
- tablade = theValue;
- angle = HALF_PI + PI + sumangle;
- }
- void dibujarTabla(){ //Dibuja la tabla paso a paso
- pushMatrix();
- translate(1.1*width/2,height/2);
- circle(radius*sin(angle),radius*cos(angle),2);
- //if(counter*tablade<=ncosas){
- line(radius*sin(angle),radius*cos(angle),radius*sin((counter*tablade*sumangle)+(angle-counter*sumangle)),radius*cos((counter*tablade*sumangle)+(angle-counter*sumangle)));
- //}
- println(counter*tablade + " -> " + counter*tablade*degrees(sumangle));
- text(counter,(radius+sumar)*sin(angle),(radius+sumar)*cos(angle));
- angle += sumangle;
- counter++;
- popMatrix();
- }
- //Clases
- class Orb {
- PVector position;
- PVector velocity;
- float r;
- Orb(float x, float y, float r_) {
- position = new PVector(x, y);
- velocity = new PVector(random(1,5), random(1,5));
- velocity.rotate(random(0,TWO_PI));
- r = r_;
- }
- void move() {
- velocity.add(gravity);
- position.add(velocity);
- if(position.x < 25 || position.x > 375){
- position.set(width/2,height/2);
- velocity.rotate(random(0,TWO_PI));
- }
- }
- void display() {
- colorMode(HSB, 360, 100, 100);
- col--;
- if(col==0)col=360;
- noStroke();
- fill(col,255,255);
- ellipse(position.x, position.y, r*2, r*2);
- colorMode(RGB,255);
- }
- void checkGroundCollision(Ground groundSegment) {
- float deltaX = position.x - groundSegment.x;
- float deltaY = position.y - groundSegment.y;
- float cosine = cos(groundSegment.rot);
- float sine = sin(groundSegment.rot);
- float groundXTemp = cosine * deltaX + sine * deltaY;
- float groundYTemp = cosine * deltaY - sine * deltaX;
- float velocityXTemp = cosine * velocity.x + sine * velocity.y;
- float velocityYTemp = cosine * velocity.y - sine * velocity.x;
- if (groundYTemp > -r && position.x >= groundSegment.x1 && position.x <= groundSegment.x2 ) {
- groundYTemp = -r;
- velocityYTemp *= -1.0;
- }
- if (groundYTemp > -r && position.x <= groundSegment.x1 && position.x >= groundSegment.x2 ) {
- groundYTemp = -r;
- velocityYTemp *= -1.0;
- }
- deltaX = cosine * groundXTemp - sine * groundYTemp;
- deltaY = cosine * groundYTemp + sine * groundXTemp;
- velocity.x = cosine * velocityXTemp - sine * velocityYTemp;
- velocity.y = cosine * velocityYTemp + sine * velocityXTemp;
- position.x = groundSegment.x + deltaX;
- position.y = groundSegment.y + deltaY;
- }
- }
- class Ground {
- float x1, y1, x2, y2;
- float x, y, len, rot;
- Ground(float x1, float y1, float x2, float y2) {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- x = (x1+x2)/2;
- y = (y1+y2)/2;
- len = dist(x1, y1, x2, y2);
- rot = atan2((y2-y1), (x2-x1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement