Dependent gliders: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
 
(21 intermediate revisions by the same user not shown)
Line 4: Line 4:
var P1 = brd.create('point', [4,-1], {name:'', face:'[]'});
var P1 = brd.create('point', [4,-1], {name:'', face:'[]'});
var li = brd.create('line', [P0,P1]);
var li = brd.create('line', [P0,P1]);
var A = brd.create('glider', [0,-1,li]);
var A = brd.create('glider', [0,-1,li], {name:'A'});
var B = brd.create('glider', [-3,-1,li]);
var B = brd.create('glider', [-3,-1,li], {name:'B'});
var C = brd.create('glider', [ 3,-1,li]);
var C = brd.create('glider', [ 3,-1,li], {name:'C'});
brd.addHook(function() {
 
     if (brd.mouse.obj==A) {
brd.on('update', function() {
     var o;
    if (typeof brd.touches != 'undefined' && brd.touches.length>0) {
      o = brd.touches[0].obj;
    } else if (brd.mouse!=null && typeof brd.mouse.obj != 'undefined') {
      o = brd.mouse.obj;
    } else {
      return;
    }
    if (o==A) {
       B.position = A.position - 0.3;
       B.position = A.position - 0.3;
       B.prepareUpdate().update(true).updateRenderer();
       B.prepareUpdate().update(true).updateRenderer();
       C.setPositionDirectly(JXG.COORDS_BY_USER, A.X()+1, -1);  
 
       C.setPositionDirectly(JXG.COORDS_BY_USER, [A.X()+3, C.Y()]);  
       C.prepareUpdate().update().updateRenderer();
       C.prepareUpdate().update().updateRenderer();
    } else if (o==B) {
      A.position = B.position + 0.3;
      A.prepareUpdate().update(true).updateRenderer();
      C.setPositionDirectly(JXG.COORDS_BY_USER, [A.X()+3, C.Y()]);
      C.prepareUpdate().update().updateRenderer();
    } else if (o==C) {
      A.setPositionDirectly(JXG.COORDS_BY_USER, [C.X()-3, A.Y()]);
      A.prepareUpdate().update().updateRenderer();
      B.position = A.position - 0.3;
      B.prepareUpdate().update(true).updateRenderer();
    }
});
</jsxgraph>
===The underlying JavaScript code===
<source lang="javascript">
var brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,5,5,-5], axis:true});
var P0 = brd.create('point', [-4,-1], {name:'', face:'[]'});
var P1 = brd.create('point', [4,-1], {name:'', face:'[]'});
var li = brd.create('line', [P0,P1]);
var A = brd.create('glider', [0,-1,li], {name:'A'});
var B = brd.create('glider', [-3,-1,li], {name:'B'});
var C = brd.create('glider', [ 3,-1,li], {name:'C'});
brd.on('update', function() {
    var o;
    if (typeof brd.touches != 'undefined' && brd.touches.length>0) {
      o = brd.touches[0].obj;
    } else if (brd.mouse!=null && typeof brd.mouse.obj != 'undefined') {
      o = brd.mouse.obj;
    } else {
      return;
     }
     }
  }, 'update');
   
</jsxgraph>
    if (o==A) {
      B.position = A.position - 0.3;
      B.prepareUpdate().update(true).updateRenderer();
 
      C.setPositionDirectly(JXG.COORDS_BY_USER, [A.X()+3, C.Y()]);
      C.prepareUpdate().update().updateRenderer();
    } else if (o==B) {
      A.position = B.position + 0.3;
      A.prepareUpdate().update(true).updateRenderer();
 
      C.setPositionDirectly(JXG.COORDS_BY_USER, [A.X()+3, C.Y()]);
      C.prepareUpdate().update().updateRenderer();
    } else if (o==C) {
      A.setPositionDirectly(JXG.COORDS_BY_USER, [C.X()-3, A.Y()]);
      A.prepareUpdate().update().updateRenderer();
 
      B.position = A.position - 0.3;
      B.prepareUpdate().update(true).updateRenderer();
    }
});
</source>
 
[[Category:Examples]]

Latest revision as of 09:35, 18 June 2013

The underlying JavaScript code

var brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,5,5,-5], axis:true});
var P0 = brd.create('point', [-4,-1], {name:'', face:'[]'});
var P1 = brd.create('point', [4,-1], {name:'', face:'[]'});
var li = brd.create('line', [P0,P1]);
var A = brd.create('glider', [0,-1,li], {name:'A'});
var B = brd.create('glider', [-3,-1,li], {name:'B'});
var C = brd.create('glider', [ 3,-1,li], {name:'C'});

brd.on('update', function() {
    var o;
    if (typeof brd.touches != 'undefined' && brd.touches.length>0) {
      o = brd.touches[0].obj;
    } else if (brd.mouse!=null && typeof brd.mouse.obj != 'undefined') {
      o = brd.mouse.obj;
    } else {
      return;
    }
 
    if (o==A) {
      B.position = A.position - 0.3;
      B.prepareUpdate().update(true).updateRenderer();

      C.setPositionDirectly(JXG.COORDS_BY_USER, [A.X()+3, C.Y()]); 
      C.prepareUpdate().update().updateRenderer();
    } else if (o==B) {
      A.position = B.position + 0.3;
      A.prepareUpdate().update(true).updateRenderer();

      C.setPositionDirectly(JXG.COORDS_BY_USER, [A.X()+3, C.Y()]); 
      C.prepareUpdate().update().updateRenderer();
    } else if (o==C) {
      A.setPositionDirectly(JXG.COORDS_BY_USER, [C.X()-3, A.Y()]); 
      A.prepareUpdate().update().updateRenderer();

      B.position = A.position - 0.3;
      B.prepareUpdate().update(true).updateRenderer();
    } 
 });