Advertisement
ahorsewithnoname

Untitled

Apr 25th, 2021
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. int posx[] = new int [50];
  2. int posy[] = new int [50];
  3. int sign[] = new int [50];
  4. int counter = 0;
  5. int moveit = 0;
  6. float move[] = new float[2];
  7.  
  8. void setup(){
  9. size(500,500);
  10. textAlign(CENTER, CENTER);
  11. circle(width/2,height/2,20);
  12. move[0] = 250;
  13. move[1] = 250;
  14. stroke(0);
  15.  
  16. }
  17.  
  18. PVector crearVector(float x, float y){
  19. PVector v = new PVector(0,0);
  20. for(int i = 0; i < counter; i++){
  21. PVector ind = vectorCampo(i,x,y);
  22. v.add(ind);
  23. }
  24. return v;
  25. }
  26.  
  27. PVector vectorCampo(int i,float x, float y){
  28. float multiplicador = int(sqrt(pow(posx[i]-x,2)+pow(posy[i]-y,2)));
  29. multiplicador = map(multiplicador,0,500,sqrt(50),0);
  30. multiplicador *= -multiplicador * sign[i];
  31. PVector campo = new PVector(posx[i]-x,posy[i]-y);
  32. campo.normalize();
  33. campo.mult(multiplicador);
  34. strokeWeight(1);
  35. stroke(255,0,0);
  36. if (sign[i] == -1) stroke (50,50,200);
  37. arrow(x,y,x+campo.x,y+campo.y);
  38. stroke(0);
  39. return campo;
  40. }
  41.  
  42. void draw(){
  43. background(255);
  44. PVector v = crearVector(mouseX,mouseY);
  45. stroke(0);
  46. textSize(10);
  47. 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);
  48. textSize(20);
  49. strokeWeight(3);
  50. if(counter > 0) arrow(mouseX,mouseY,mouseX+v.x,mouseY+v.y);
  51.  
  52. v = crearVector(move[0],move[1]);
  53. //v.normalize();
  54. float mult = map(v.mag(),0,1,0,0.001/counter);
  55. v.mult(mult);
  56. stroke(0);
  57. //arrow(move[0],move[1],move[0]+v.x,move[1]+v.y);
  58. //println(v.mag()+ " vx: " + v.x + " vy: " + v.y);
  59. if(moveit == 1){
  60. move[0] += v.x;
  61. move[1] += v.y;
  62. }
  63. circle(move[0],move[1],10);
  64. strokeWeight(1);
  65. for(int i = 0; i < counter; i++){
  66. fill(255);
  67. if(sign[i] == 1){
  68. fill(255,0,0);
  69. circle(posx[i],posy[i],20);
  70. fill(0);
  71. text("+",posx[i],posy[i]-3);
  72. }
  73. else{
  74. fill(50,50,200);
  75. circle(posx[i],posy[i],20);
  76. fill(0);
  77. text("-",posx[i],posy[i]-3);
  78. }
  79. }
  80. }
  81.  
  82. void mousePressed(){
  83. posx[counter] = mouseX;
  84. posy[counter] = mouseY;
  85. if(mouseButton == LEFT){
  86. sign[counter] = 1;
  87. }
  88. else if(mouseButton == RIGHT){
  89. sign[counter] = -1;
  90. }
  91. }
  92.  
  93. void mouseReleased(){
  94. counter++;
  95. }
  96.  
  97. void arrow(float x1, float y1, float x2, float y2) {
  98. line(x1, y1, x2, y2);
  99. pushMatrix();
  100. translate(x2, y2);
  101. float a = atan2(x1-x2, y2-y1);
  102. rotate(a);
  103. line(0, 0, -3, -3);
  104. line(0, 0, 3, -3);
  105. popMatrix();
  106. }
  107.  
  108. void keyPressed(){
  109. if(key == 'W' || key == 'w') moveit = 1;
  110. if(key == 'S' || key == 's') moveit = 0;
  111. if(key == 'D' || key == 'd'){
  112. move[0] = 250;
  113. move[1] = 250;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement