Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int posx[] = new int [50];
- int posy[] = new int [50];
- int sign[] = new int [50];
- int counter = 0;
- int moveit = 0;
- float move[] = new float[2];
- void setup(){
- size(500,500);
- textAlign(CENTER, CENTER);
- circle(width/2,height/2,20);
- move[0] = 250;
- move[1] = 250;
- stroke(0);
- }
- PVector crearVector(float x, float y){
- PVector v = new PVector(0,0);
- for(int i = 0; i < counter; i++){
- PVector ind = vectorCampo(i,x,y);
- v.add(ind);
- }
- return v;
- }
- PVector vectorCampo(int i,float x, float y){
- float multiplicador = int(sqrt(pow(posx[i]-x,2)+pow(posy[i]-y,2)));
- multiplicador = map(multiplicador,0,500,sqrt(50),0);
- multiplicador *= -multiplicador * sign[i];
- PVector campo = new PVector(posx[i]-x,posy[i]-y);
- campo.normalize();
- campo.mult(multiplicador);
- strokeWeight(1);
- stroke(255,0,0);
- if (sign[i] == -1) stroke (50,50,200);
- arrow(x,y,x+campo.x,y+campo.y);
- stroke(0);
- return campo;
- }
- void draw(){
- background(255);
- PVector v = crearVector(mouseX,mouseY);
- stroke(0);
- textSize(10);
- text("Controles:\nW para que se mueva la partícula central (positiva)\nS para que deje de moverse la partícula central\nD para que regrese al centro",width/2,40);
- textSize(20);
- strokeWeight(3);
- if(counter > 0) arrow(mouseX,mouseY,mouseX+v.x,mouseY+v.y);
- v = crearVector(move[0],move[1]);
- //v.normalize();
- float mult = map(v.mag(),0,1,0,0.001/counter);
- v.mult(mult);
- stroke(0);
- //arrow(move[0],move[1],move[0]+v.x,move[1]+v.y);
- //println(v.mag()+ " vx: " + v.x + " vy: " + v.y);
- if(moveit == 1){
- move[0] += v.x;
- move[1] += v.y;
- }
- circle(move[0],move[1],10);
- strokeWeight(1);
- for(int i = 0; i < counter; i++){
- fill(255);
- if(sign[i] == 1){
- fill(255,0,0);
- circle(posx[i],posy[i],20);
- fill(0);
- text("+",posx[i],posy[i]-3);
- }
- else{
- fill(50,50,200);
- circle(posx[i],posy[i],20);
- fill(0);
- text("-",posx[i],posy[i]-3);
- }
- }
- }
- void mousePressed(){
- posx[counter] = mouseX;
- posy[counter] = mouseY;
- if(mouseButton == LEFT){
- sign[counter] = 1;
- }
- else if(mouseButton == RIGHT){
- sign[counter] = -1;
- }
- }
- void mouseReleased(){
- counter++;
- }
- void arrow(float x1, float y1, float x2, float y2) {
- line(x1, y1, x2, y2);
- pushMatrix();
- translate(x2, y2);
- float a = atan2(x1-x2, y2-y1);
- rotate(a);
- line(0, 0, -3, -3);
- line(0, 0, 3, -3);
- popMatrix();
- }
- void keyPressed(){
- if(key == 'W' || key == 'w') moveit = 1;
- if(key == 'S' || key == 's') moveit = 0;
- if(key == 'D' || key == 'd'){
- move[0] = 250;
- move[1] = 250;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement