Difference between revisions of "Dependent gliders"

From JSXGraph Wiki
Jump to: navigation, search
 
(31 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() {
+
 
     B.position = A.position + 0.5;
+
brd.on('update', function() {
     B.prepareUpdate().update(true).updateRenderer();
+
     var o;
  }, 'update');
+
    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();
 +
    }
 +
  });
 
</jsxgraph>
 
</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;
 +
    }
 +
 +
    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 10: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();
    } 
 });