Difference between revisions of "Bearing"

From JSXGraph Wiki
Jump to: navigation, search
(Version 2)
Line 55: Line 55:
 
<jsxgraph box="box2" width="600" height="500">
 
<jsxgraph box="box2" width="600" height="500">
 
var brd2 = JXG.JSXGraph.initBoard('box2',{axis:true,boundingbox:[-2,1.5,2,-1.5],keepaspectratio:true});
 
var brd2 = JXG.JSXGraph.initBoard('box2',{axis:true,boundingbox:[-2,1.5,2,-1.5],keepaspectratio:true});
var p1 = brd2.create('point',[0,0]);
+
var p1 = brd2.create('point',[0,0], {visible:false, fixed:true});
 
var p2 = brd2.create('point',[1,0]);
 
var p2 = brd2.create('point',[1,0]);
 
var p3 = brd2.create('point',[0,1]);
 
var p3 = brd2.create('point',[0,1]);
Line 61: Line 61:
  
 
var q = brd2.create('glider',[0.5,0.5,a],{name:'drag me'}); // global variable
 
var q = brd2.create('glider',[0.5,0.5,a],{name:'drag me'}); // global variable
 +
brd2.create('segment',[q,p1]);
 +
 
brd2.addHook(function(){
 
brd2.addHook(function(){
 
                   document.getElementById('degrees2').value = (Math.atan2(q.Y(),q.X())*180/Math.PI).toFixed(0);
 
                   document.getElementById('degrees2').value = (Math.atan2(q.Y(),q.X())*180/Math.PI).toFixed(0);
Line 83: Line 85:
 
<source lang="javascript">
 
<source lang="javascript">
 
var brd2 = JXG.JSXGraph.initBoard('box2',{axis:true,boundingbox:[-2,1.5,2,-1.5],keepaspectratio:true});
 
var brd2 = JXG.JSXGraph.initBoard('box2',{axis:true,boundingbox:[-2,1.5,2,-1.5],keepaspectratio:true});
var p1 = brd2.create('point',[0,0]);
+
var p1 = brd2.create('point',[0,0], {visible:false, fixed:true});
 
var p2 = brd2.create('point',[1,0]);
 
var p2 = brd2.create('point',[1,0]);
 
var p3 = brd2.create('point',[0,1]);
 
var p3 = brd2.create('point',[0,1]);
Line 89: Line 91:
  
 
var q = brd2.create('glider',[0.5,0.5,a],{name:'drag me'}); // global variable
 
var q = brd2.create('glider',[0.5,0.5,a],{name:'drag me'}); // global variable
 +
brd2.create('segment',[q,p1]);
 +
 
brd2.addHook(function(){
 
brd2.addHook(function(){
 
                   document.getElementById('degrees2').value = (Math.atan2(q.Y(),q.X())*180/Math.PI).toFixed(0);
 
                   document.getElementById('degrees2').value = (Math.atan2(q.Y(),q.X())*180/Math.PI).toFixed(0);

Revision as of 18:35, 16 June 2010

The underlying JavaScript code

<form>
<input type="text" id="degrees">
<input type="button" value="set direction" onclick="setDirection()">
</form>
var brd = JXG.JSXGraph.initBoard('jxgbox',{axis:true,boundingbox:[-2,1.5,2,-1.5],keepaspectratio:true});
var c = brd.create('circle',[[0,0],1]);
var p = brd.create('glider',[-1,0.5,c],{name:'drag me'}); // global variable
brd.addHook(function(){
                  document.getElementById('degrees').value = (Math.atan2(p.Y(),p.X())*180/Math.PI).toFixed(0);
            });

var setDirection = function() {
   var phi = 1*document.getElementById('degrees').value*Math.PI/180.0;
   var r = c.Radius();
   p.moveTo([r*Math.cos(phi),r*Math.sin(phi)]);

}

Version 2

The underlying JavaScript code

<form>
<input type="text" id="degrees">
<input type="button" value="set direction" onclick="setDirection2()">
</form>
var brd2 = JXG.JSXGraph.initBoard('box2',{axis:true,boundingbox:[-2,1.5,2,-1.5],keepaspectratio:true});
var p1 = brd2.create('point',[0,0], {visible:false, fixed:true});
var p2 = brd2.create('point',[1,0]);
var p3 = brd2.create('point',[0,1]);
var a = brd2.create('arc',[p1,p2,p3]);

var q = brd2.create('glider',[0.5,0.5,a],{name:'drag me'}); // global variable
brd2.create('segment',[q,p1]);

brd2.addHook(function(){
                  document.getElementById('degrees2').value = (Math.atan2(q.Y(),q.X())*180/Math.PI).toFixed(0);
            });

var setDirection2 = function() {
   var phi = 1*document.getElementById('degrees2').value*Math.PI/180.0;
   var r = a.Radius();
   q.moveTo([r*Math.cos(phi),r*Math.sin(phi)]);

}