Dependent gliders

From JSXGraph Wiki
Jump to: navigation, search

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();
    } 
 });