Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creating variables
- let tX=400, tY=300, myX=100, myY=100, ugul=Math.PI/2, natisnato = false, dX=1,dY=1, index=-1;
- let centersX=[], centersY=[], r=[], n=5;
- for(let i=0;i<n;i++)
- {
- let presichat = true;
- while(presichat)
- {
- presichat =false;
- centersX[i] = Math.random()*800;
- centersY[i] = Math.random()*600;
- r[i]=Math.random()*90+20;
- for(let j=0;j<i;j++)
- {
- if(distance(centersX[i], centersY[i], centersX[j], centersY[j])<r[i]+r[j])
- {
- presichat = true;
- break;
- }
- }
- }
- }
- function distance(aX, aY, bX, bY)
- {
- return Math.sqrt((aX-bX)*(aX-bX)+(aY-bY)*(aY-bY));
- }
- function update() {
- if(index==-1)
- {
- myX+=dX;
- myY+=dY;
- }
- else
- {
- ugul += 0.02;
- myX= centersX[index]+Math.cos(ugul)*r[index];
- myY= centersY[index]+Math.sin(ugul)*r[index];
- }
- for(let i=0;i<n;i++)
- {
- if(distance(myX,myY,centersX[i], centersY[i])<=r[i])
- {
- index=i;
- break;
- }
- }
- /*if(!natisnato)
- {
- ugul += 0.02;
- myX= tX+Math.cos(ugul)*100;
- myY= tY+Math.sin(ugul)*100;
- }
- else
- {
- myX += Math.cos(ugul)*10;
- myY += Math.sin(ugul)*10;
- }*/
- }
- function draw() {
- for(let i=0;i<n;i++)
- {
- context.beginPath();
- context.arc(centersX[i], centersY[i], r[i], 0, Math.PI*2);
- context.stroke();
- }
- context.beginPath();
- context.arc(myX, myY, 4, 0, Math.PI*2);
- context.fill();
- // context.fillRect(myX, myY, 20,20);
- // context.fillRect(tX, tY, 10,10);
- };
- function keyup(key) {
- index =-1;
- dX=Math.cos(ugul)*3;
- dY=Math.sin(ugul)*3;
- }
- function mouseup() {
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement