Programacion - Processing

13
PRIMER EJEMPLO DE PROGRAMACION int v;/*declaro la variable v, que es de tipo int*/ v=5;//inicializo la v. println ("hola tengo") println (v); println ("años"); println ("hola tengo_"+v+"_años"); PRORAMACION TOTAL int nt; float vt = 25.5; nt=2; float cuenta =nt*vt; println ("su total es:_"+cuenta); println ("subtotal_"+cuenta); println ("i.v.a_"+(cuenta*.16)); cuenta+=cuenta*0.16; println("total:_"+cuenta); EJERCICIO AREA TRIANGULO float a; a= 5; float resultado = sqrt(3)/2*a; println ("resultado:_"+ resultado); float area = sqrt(3)/4*(a*a); println ("area:_" + area); EJERCICIO AREA CIRCULO float r; r= 5; float resultado = PI*(r*r); println ("resultado:_"+ resultado); TRABAJO 3 VARIABLES int A=30, B=20, C=16, mayor; String mensaje = ""; mayor=0; if (A > B && A> C){ mayor = A; mensaje = "A"; } if (B > A && B> C){ mayor = B; mensaje = "B"; } if (C > A && C> B){ mayor = C; mensaje = "C"; } println ("El Mayor Valor es :" + " " + mayor); println ("La Letra del Mayor Valor es :" + " " + mensaje);

Transcript of Programacion - Processing

Page 1: Programacion - Processing

PRIMER EJEMPLO DE PROGRAMACIONint v;/*declaro la variable v, que es de tipo int*/v=5;//inicializo la v.println ("hola tengo")println (v);println ("años");println ("hola tengo_"+v+"_años");

PRORAMACION TOTALint nt; float vt = 25.5;nt=2;float cuenta =nt*vt;println ("su total es:_"+cuenta);println ("subtotal_"+cuenta);println ("i.v.a_"+(cuenta*.16));cuenta+=cuenta*0.16;println("total:_"+cuenta);

EJERCICIO AREA TRIANGULOfloat a;a= 5;float resultado = sqrt(3)/2*a;println ("resultado:_"+ resultado);float area = sqrt(3)/4*(a*a);println ("area:_" + area);

EJERCICIO AREA CIRCULOfloat r;r= 5;float resultado = PI*(r*r);println ("resultado:_"+ resultado);

TRABAJO 3 VARIABLESint A=30, B=20, C=16, mayor;String mensaje = "";mayor=0;if (A > B && A> C){ mayor = A; mensaje = "A";}if (B > A && B> C){ mayor = B; mensaje = "B";}if (C > A && C> B){ mayor = C; mensaje = "C";}println ("El Mayor Valor es           :" + " " + mayor);println ("La Letra del Mayor Valor es :" + " " + mensaje);

EJERCICIO #2 FORMAS Y COLORESvoid setup (){size (500,500);background (25,126,15);smooth ();fill (162);//noStroke();stroke (25,30,30);strokeWeight (10);

Page 2: Programacion - Processing

}

void draw (){fill (250); noStroke ();rect (140,250,300,100);noFill();stroke (20,20,40);strokeWeight (10);ellipse (140,250,80,80);}

void setup (){size (500,500);background (25,126,15);smooth ();}

void draw (){ dibuja();}  void dibuja (){fill (250); noStroke ();rect (140,250,300,100);noFill();stroke (20,20,40);strokeWeight (10);ellipse (140,250,80,80);}

EJERCICIO MOVIMIENTO PELOTAvoid setup (){size (320,240);background (0);smooth ();}

void draw(){ dibuja ();}

int pelotax =155;int pelotay= 115;int pelotadx =1;int pelotady= 1;void dibuja(){fill (255); noStroke ();rect (155,155,10,50);float rojo = random (256);float azul = random (100) +155;noFill ();stroke (rojo,0,azul);strokeWeight (2);ellipse (pelotax,pelotay,30,30);mueve ();}

void mueve (){ if (pelotax <= 15 || pelotax >= 305) pelotadx*=-1; pelotax+= pelotadx; if (pelotay <=15 || pelotay >= 225){ pelotady*=-1; if (pelotady >0){ pelotady =int (random (5)+1); }else{    pelotady=int (random (5)+1)*-1; }}pelotay += pelotady;

Page 3: Programacion - Processing

}

MUERTEvoid setup (){size (320,240);background (0);smooth ();}void draw(){ dibuja ();}

int pelotax =155;int pelotay= 115;int pelotadx =1;int pelotady= 1;void dibuja(){fill (255); noStroke ();rect (155,155,10,50);float rojo = random (256);float azul = random (100) +155;fill(25,20);stroke (rojo,0,azul);strokeWeight (2);ellipse (pelotax,pelotay,30,30);mueve ();}

void mueve (){ if (pelotax <= 15 || pelotax >= 305){ pelotadx*=-1; ciclos++; println(ciclos); muerte();}

 pelotax+= pelotadx; if (pelotay <=15 || pelotay >= 225){ pelotady*=-1; if (pelotady >0){ pelotady =int (random (5)+1); }else{    pelotady=int (random (5)+1)*-1; }}pelotay += pelotady;}int ciclos = 0;void muerte (){ if (ciclos==3){    saveFrame("coss.tif");    exit(); }}

EJERCICIO PLANO CARTESIANOint xone=0;int yone=0;

void setup(){size (200,200);background (255);stroke(0);}

Page 4: Programacion - Processing

void draw (){xone = xone+ 10;line (0,xone,200,xone);line (xone,0,xone,200);fill (255,0,0);ellipse (30,40,10,10);yone = yone+ 10;line (0,yone,200,yone);line (yone,0,yone,500);fill (250,0,0);ellipse (40,30,10,10);}

REBOTE PELOTAint pelotax= 100, pelotay =100,pelotady=1,pelotat=50;void setup (){size (320,240);smooth ();background (0);}

void draw (){pelota();rect(pelotax,pelotay,pelotat,pelotat);}

void pelota(){ if(pelotay<=0||pelotay>=height-pelotat)pelotady*=-1; pelotay+=pelotady;}

2 PELOTAS CON REBOTEint pelotax= 100, pelotay =100,pelotady=1,pelotat=10;int p2x= 100, p2y =200,p2dy=1,p2t=10;void setup (){size (320,240);smooth ();background (0);}

void draw (){pelota();colision();rect(pelotax,pelotay,pelotat,pelotat);rect(p2x,p2y,p2t,p2t);}

void pelota(){ if(pelotay<=0||pelotay>=height-pelotat)pelotady*=-1; pelotay+=pelotady; if(p2y<=0||p2y>=height-pelotat)p2dy*=-1; p2y += p2dy;}

void colision(){int d = int(dist(pelotax,pelotay,p2x,p2y));println(d);if (d <=10){pelotady*=-1;p2dy*=-1;}}

Page 5: Programacion - Processing

PROGRAMACION PING PONGint pelotax= 100, pelotay =100,pelotady=1,pelotadx=1,pelotat=10;int paletax = 10,paletay=95,paletatx=10,paletaty =45;int py = 50,px=305,ptx=10,pty =45;int paletad=0;int pd=0;int cuenta;PFont f;

void setup (){size(320,240);noSmooth();background(165);noStroke();f = createFont("Arial",16,true); }

void draw (){pelota();rect (pelotax,pelotay,pelotat,pelotat);paleta ();rect (paletax,paletay,paletatx,paletaty);paleta2 ();rect (px,py,ptx,pty); line(width/2,0,width/2,height); textFont(f);        textAlign(CENTER); text("This text is centered.",width/2,20); }

void pelota(){ if(pelotay<=0||pelotay>=height-pelotat)pelotady*=-1; pelotay+=pelotady; if (pelotax >= width-pelotat) pelotadx = -1;if (pelotax <= 0){pelotadx=1;cuenta++;println (cuenta);}pelotax += pelotadx;}

void paleta(){ if (keyPressed == true){    if (key =='a'){    if (paletay >0) paletay = -1; }    if (key =='z'){    if (paletay < height-paletay)paletad = 1;    }}if (paletay < 0){paletay= 0;paletad= 0;}

if (paletay >height-paletay){paletay = height -paletay;paletad=0;}paletay += paletad;}

Page 6: Programacion - Processing

void paleta2(){ if (keyPressed == true){    if (key =='p'){    if (py >0) py = -1;    }    if (key =='l'){    if (py < height-py)pd = 1;    }}if (py < 0){py= 0;pd= 0;}if (py >height-py){py = height -py;pd=0;}py += pd;}

void dibujar(){background (0);}

inlets =1;outlets=1;var pelotas=new Array();var cuantas=10;function bang(){outlet (0, "clear");for(var i=0; i < cuantas; i++){if (pelotas [i].y <= 0 || pelotas [i].y >= 390)pelotas[i].vy*=-1if (pelotas [i].x <= 0 || pelotas [i].x >= 390)pelotas[i].vx*=-1outlet(0, "frameoval " + pelotas[i].x +" "+ pelotas[i].y + " "+(pelotas[i].x+10)+ " " + (pelotas[i].y+10)+ " " + pelotas[i].c);pelotas[i].x +=pelotas[i].vxpelotas[i].y +=pelotas[i].vy}}

function crea(){for(var i=0; i < cuantas; i++){pelotas[i]= new pelota(Math.random()*250+10,Math.random()*200+10,5,1,Math.random()*256);}}

function pelota(x, y, vx, vy, c){ this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.c = c;}

*6TO CUATRIMESTRE*size (1000,510);//tamaño del cuadrobackground(255);//color de fondostrokeWeight(6);//grosor del contornostroke(0); //color de contorno           smooth();

Page 7: Programacion - Processing

fill (255,0,0); // color de relleno de las figurasrectMode(CENTER);stroke(0);fill(88,193,155);rect(500,250,950,450);line(480,30,480, 470);ellipse(480, 250, 80, 80);arc(30, 30, 60, 70, 0, PI/2);arc(30, 470, 60, 70, TWO_PI-PI/2, TWO_PI);arc(970,470, 60, 70, PI, TWO_PI-PI/2);arc(970,30, 60, 70, PI/2, PI);rect(55,250,60,95);rect(40,250,30,45);rect(945,250,60,95);rect(960,250,30,45);

DIBUJO1size (500,500);//tamaño del cuadrobackground(255);//color de fondostrokeWeight(5);//grosor del contornostroke(0); //color de contorno           smooth();noFill(); // color de relleno de las figurasrectMode(CENTER);stroke(0);ellipse(250, 250, 150, 150);ellipse(250, 180, 150, 150);ellipse(170, 250, 150, 150);ellipse(320, 250, 150, 150);ellipse(250, 330, 150, 150);

DIBUJO 2size (500,500);//tamaño del cuadrobackground(255);//color de fondostrokeWeight(15);//grosor del contornostroke(0); //color de contorno           smooth();fill (255,0,0); // color de relleno de las figurasrectMode(CENTER);stroke(0);noFill();ellipse(250, 250, 300, 300);ellipse(250, 325, 100, 100);line(250,370,250, 100);line(200,250,250, 250);line(310,250,250, 250);line(200,250,200, 110);line(310,250,310, 112);

DIBUJO 3    size (500,500);//tamaño del cuadrobackground(255);//color de fondostrokeWeight(15);//grosor del contornostroke(0); //color de contorno           smooth();fill (255,0,0); // color de relleno de las figurasrectMode(CENTER);stroke(0);noFill();ellipse(250, 400, 100, 100);

Page 8: Programacion - Processing

line(250,350,250, 100);line(310,250,310, 112);line(310,250,310, 112);line(190,250,190, 112);line(310,150,190, 150);

BUCLE WHILEsize(200,200);background(255);int y=80;int x=50;int espacio=10;int largo=20;int fin=150;stroke(255,0,0);while (x<=fin){ line (x,y,x,y+largo); x=x+espacio;}

Bucle Forvoid setup (){ size(400,400); background (0);}

void draw(){ for(int i=20; i<380 ; i+=10){   stroke(255);   line (i, 20, i+15, 380); }}

Mandala usando el ratonvoid setup (){ size (200,200);}

void draw (){ background (255); noFill(); for(int x=0; x<width; x+=30) for(int y=0; y<height; y+=30) ellipse(x,y,mouseX,mouseY);}

IMAGEN TIPO RADARint x= 0;void setup (){ size (600,600); background (0); smooth();}

void draw (){ //background (0); stroke (0,0,255); noFill(); ellipse(width*0.5,height*0.5, x,x); //ellipse(width*0.5, height*0.5, width*0.66,height*0.66);

Page 9: Programacion - Processing

 line(width*0.5,0,width*0.5,height); line(0,height*0.5,width,height*0.5); x= x+40; if (x >= 600){   x=0; }}

RANDOMint x= 0;

void setup (){ size (600,600); background (0); smooth(); frameRate(12);}

void draw (){ //background (0); stroke (random (255),random (255),random (255)); noFill(); ellipse(width*0.5,height*0.5, x,x); //ellipse(width*0.5, height*0.5, width*0.66,height*0.66); line(width*0.5,0,width*0.5,height); line(0,height*0.5,width,height*0.5); x= x+10; if (x >= 600){   x=0; }}

Gravedad de la pelotafloat x=100;float y=0;float velocidad=0;float gravedad=0.1;

void setup (){ size (200,200);}

void draw (){ background(255); //pelota fill (250,89,30); stroke (0); ellipse (x,y,30,30); y= y + velocidad; velocidad = velocidad+gravedad; //invertir velocidad if (y > height){   velocidad = velocidad* -0.95; }}

3 ESFERAS CON DIFUMINACION BLANCAsize (800,300);background(255);

Page 10: Programacion - Processing

for (int i=255; i > 0 ; i--) {noStroke(); fill(255,i,i); ellipse(125, height/2,i,i); fill(255, 255,i); ellipse(width/2,height/2,i,i); fill(i,255,i); ellipse(675, height/2,i,i);}

Celdassize(200,200);background(0,0,255);for(int x=0; x< width; x++)for(int y=0; y< height; y++){ rectMode (CENTER); if(y%2==0)continue; if(x%2==0) rect (x*10,y*10,10,10); else rect(x*10,y*10,4,10);}

Ajedrez size(300,300);background(0,0,0);for(int x=0; x< width; x++)for(int y=0; y< height; y++){ rectMode (CENTER); if(y%2==0)continue; if(x%2==0) rect (x*37,y*37,37,37); else rect(x*37,y*74,37,37);   if(y%2==0)continue; if(x%2==0) rect (x*37,y*37,37,37); else rect(x*37,y*148,37,37);}

ANIMACION CON FUNCIONESint x=0;int y=100;int speed=1;

//setup does not changevoid setup() { size (200,200); smooth ();}

void draw (){ //background (255); move(); //instead of writing out all the code about all //the code about the ball is draw(), //we simply call three functions. //How do we know //we made them up 

Page 11: Programacion - Processing

 bounce(); display();}

//where should functions be placed?//you can define your function anywhere//In the code outside os setup () and draw().//however, the convention is to place//your function definiting below draw().//A function to move the ballvoid move (){ //change the x location by speed x=x+speed;}

//A function to bounce the ballvoid bounce(){ //if we ve reached an edge, reversed speed if ((x > width) || (x <0)) {   speed = speed + -1; }}

//A function to display the ballvoid display(){ background(0); ellipseMode(CENTER); fill (random (255), random (255), random (255)); stroke(255); ellipse (x,y,50,32); fill (random (255), random (255), random (255)); rect(x-4,y-4,4,4); rect(x+4,y-4,4,4); line(x-4,y+4,x+4,y+4);}Animacion de trabajo