Difference between revisions of "Aiming"

From CodeStuff
Jump to: navigation, search
(Created page with "<edcode> var t=0.0; var speed=0.02; var ax=100; var ay=320; var bx=400; var by=200; var annotation = ""; var dataNote = ""; var dataNote2 = ""; function loop() { if (t>=...")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
Run this program to see an animated diagram shoing how to find a direction towards another point.
 +
 +
 
<edcode>
 
<edcode>
  
Line 102: Line 105:
 
   drawLine(ax,ay,bx,by);   
 
   drawLine(ax,ay,bx,by);   
 
    
 
    
   annotation="Adding the straight line to the target makes a right angled triangle";
+
   annotation="The straight line to the target makes a right angled triangle";
  
 
   triangleNote();   
 
   triangleNote();   
Line 126: Line 129:
 
   vector(transition(0,30));
 
   vector(transition(0,30));
 
   annotation="We want to move a fixed distance along this line";
 
   annotation="We want to move a fixed distance along this line";
   dataNote+=", we want to move a distance of 30";
+
   dataNote+=". We want to move 30";
 
}
 
}
  
Line 138: Line 141:
 
   speed=0.003;
 
   speed=0.003;
 
   loop();
 
   loop();
   annotation="When the points are different the distance should stay the same";
+
   annotation="The movement distance should always be the same ";
   dataNote+=", we want to move a distance of 30";
+
   dataNote+=". We want to move 30 ";
 
}
 
}
  
Line 145: Line 148:
 
   triangle();
 
   triangle();
 
   vector(30);
 
   vector(30);
   annotation="To find x and y movement we reduce both values by the same amount";
+
   annotation="To find our movement we scale all values by the same amount";
   dataNote+=", we want to move a distance of 30";
+
   dataNote+=". We want to move 30";
 
}
 
}
  
Line 169: Line 172:
 
   speed=0.001;
 
   speed=0.001;
  
   annotation="The angles stay the same if the sides are divided by the same amount";
+
   annotation="The angles stay the same when all sides are scaled evenly";
 
   triangleNote();   
 
   triangleNote();   
   dataNote+=", we want to move a distance of 30";
+
   dataNote+=". We want to move 30";
  
 
   var newdx=dx*scale;
 
   var newdx=dx*scale;
Line 177: Line 180:
 
   var newd = Math.sqrt(newdx*newdx+newdy*newdy);
 
   var newd = Math.sqrt(newdx*newdx+newdy*newdy);
 
   var div = d/newd*30;  
 
   var div = d/newd*30;  
   dataNote2=" "+d.toFixed(1)+ " / " +div.toFixed(1) +" * 30 = "  +(newd).toFixed(1);
+
    
 +
  setColour("magenta");
 +
  print("Distance to Move  = "+d.toFixed(1)+ " / " +div.toFixed(1) +" * 30 = "  +(newd).toFixed(1), 100,50);
 +
  setColour("blue");
 +
  print("X Movement value = "+(bx-ax).toFixed(1)+ " / " +div.toFixed(1) +" * 30 = "  +(newdx).toFixed(1),100,80);
 +
  setColour("green");
 +
  print("Y Movement value = "+(by-ay).toFixed(1)+ " / " +div.toFixed(1) +" * 30 = "  +(newdy).toFixed(1),100,110);
 +
 
 
}
 
}
  
Line 190: Line 200:
  
 
   setColour("black");
 
   setColour("black");
   print(annotation);
+
   print(annotation,4,22);
  
 
   setColour("grey");
 
   setColour("grey");
 
   print("ax = "+ax.toFixed(0)+"    bx = "+bx.toFixed(0)+"            dx = bx-ax = " + (bx-ax).toFixed(0) ,100,370);
 
   print("ax = "+ax.toFixed(0)+"    bx = "+bx.toFixed(0)+"            dx = bx-ax = " + (bx-ax).toFixed(0) ,100,370);
   print("ay = "+ay.toFixed(0)+"    by = "+by.toFixed(0)+"            dy = by-ay = " + (bx-ax).toFixed(0) ,100,400);
+
   print("ay = "+ay.toFixed(0)+"    by = "+by.toFixed(0)+"            dy = by-ay = " + (by-ay).toFixed(0) ,100,400);
 
   print(dataNote,4,430);
 
   print(dataNote,4,430);
 
   print(dataNote2,4,460);
 
   print(dataNote2,4,460);
    
+
 
 +
   if (part+1<parts.length ) {
 +
    setColour("black");
 +
    print("Press Space to continue.",220,470);
 +
 
 +
  }
 
   t+=speed;
 
   t+=speed;
 
   if (t>1) {
 
   if (t>1) {
Line 216: Line 231:
  
 
run(update);
 
run(update);
 +
 +
 +
 
</edcode>
 
</edcode>

Latest revision as of 20:53, 27 June 2017

Run this program to see an animated diagram shoing how to find a direction towards another point.