Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. // Creating variables
  2. let tX=400, tY=300, myX=100, myY=100, ugul=Math.PI/2, natisnato = false, dX=1,dY=1, index=-1;
  3. let centersX=[], centersY=[], r=[], n=5;
  4. for(let i=0;i<n;i++)
  5. {
  6. let presichat = true;
  7. while(presichat)
  8. {
  9. presichat =false;
  10. centersX[i] = Math.random()*800;
  11. centersY[i] = Math.random()*600;
  12. r[i]=Math.random()*90+20;
  13. for(let j=0;j<i;j++)
  14. {
  15. if(distance(centersX[i], centersY[i], centersX[j], centersY[j])<r[i]+r[j])
  16. {
  17. presichat = true;
  18. break;
  19. }
  20. }
  21. }
  22.  
  23. }
  24. function distance(aX, aY, bX, bY)
  25. {
  26. return Math.sqrt((aX-bX)*(aX-bX)+(aY-bY)*(aY-bY));
  27. }
  28. function update() {
  29. if(index==-1)
  30. {
  31. myX+=dX;
  32. myY+=dY;
  33. }
  34. else
  35. {
  36. ugul += 0.02;
  37. myX= centersX[index]+Math.cos(ugul)*r[index];
  38. myY= centersY[index]+Math.sin(ugul)*r[index];
  39. }
  40. for(let i=0;i<n;i++)
  41. {
  42. if(distance(myX,myY,centersX[i], centersY[i])<=r[i])
  43. {
  44. index=i;
  45. break;
  46. }
  47. }
  48.  
  49. /*if(!natisnato)
  50. {
  51. ugul += 0.02;
  52. myX= tX+Math.cos(ugul)*100;
  53. myY= tY+Math.sin(ugul)*100;
  54. }
  55. else
  56. {
  57. myX += Math.cos(ugul)*10;
  58. myY += Math.sin(ugul)*10;
  59. }*/
  60.  
  61.  
  62. }
  63.  
  64. function draw() {
  65. for(let i=0;i<n;i++)
  66. {
  67. context.beginPath();
  68. context.arc(centersX[i], centersY[i], r[i], 0, Math.PI*2);
  69. context.stroke();
  70. }
  71.  
  72. context.beginPath();
  73. context.arc(myX, myY, 4, 0, Math.PI*2);
  74. context.fill();
  75. // context.fillRect(myX, myY, 20,20);
  76. // context.fillRect(tX, tY, 10,10);
  77. };
  78.  
  79. function keyup(key) {
  80. index =-1;
  81. dX=Math.cos(ugul)*3;
  82. dY=Math.sin(ugul)*3;
  83. }
  84.  
  85. function mouseup() {
  86.  
  87. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement