| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Finally, you can manage your Google Docs, uploads, and email attachments (plus Dropbox and Slack files) in one convenient place. Claim a free account, and in less than 2 minutes, Dokkio (from the makers of PBworks) can automatically organize your content for you.

View
 

ScriptingWithGeoGebra40

Page history last edited by Mike May, S.J. 9 years, 10 months ago

A First Introduction to Scripting with GeoGebra 4.0

 

One of the new features of release 4.0 is the ability to add scripts to any object. In general, one can either use GGB script, which is a collection of GeoGebra commands, or a collection of commands in JavaScript. JavaScript allows for programming techniques like loops and commands controlled by if statements. However, in this tutorial, we will only look at GGB scripting.

 

We start with a simple setup. We have a circle defined by a center and a point. We also have a point on the circle and a number called Counter.

We want to add a text to turn the animation of C on and off. We would like the text to change so when C is still it says "Turn Animation On", and when C is moving it says "Turn Animation Off". We would also like the value of Counter to be incremented whenever we click on the text and change the state of the animation.

 

We start by adding the number

AnimationState = 0

in the input line. We also add a text with

ControlText=If[AnimationState==0, "Turn Animation On", "Turn Animation Off"]

in the input line. We changed the background color of the text to green, so it looks like a button. (A comment on the syntax of the definition of ControlText. A double equals sign, ==, is the Boolean function that tests equality. It gives true when the two sides are equal and false otherwise.)

 

We are now ready for the script. We use the contextual menu to open the object properties of ControlText. We select the scripting tab. Within the scripting tab, select the onclick tab

 

AnimationState=1-AnimationState

StartAnimation[C,AnimationState==1]

Counter=Counter+1

Click "OK" for the script to take effect, then click "Close" to close the window. There are a few things to notice about our script.

  • In the first and third lines we redefine a number with a statement that includes the same number.

  • The StartAnimation command is used to both start and stop animations. If the Boolean variable is true or missing, the command starts animation. If the Boolean variable is false, the command stops animation.

  • We are using AnimationState as a number pretending to be a Boolean variable. A value of 1 corresponds to true and zero corresponds to false. With that understanding, sending AnimationState to 1-AnimationState converts the variable between true ad false and back.

  • Flipping the value of a checkbox takes a bit more care, since we only want to change the value, but not redefine Boolean variable.  If we have a checkbox named box1, we can flip it with the script command:
    SetValue(box1,If[box1==false, true, false]).

 

You may find it useful to look at the scripting example GeoGebra file.

 

The scripts in "On Click" are executed when the object is selected or clicked. The "On Update" scripts are executed when the object is updated. Updating objects includes moving them or changing whether on not the label or object is visible. Dependant objects are also considered as updated if an object they depend on is updated.

 

A second scripting example has scripts that let you see the difference between on click and updated.

Several object (named Object1, Object2, etc.) have scripts that add their number to a counter on click and 10 times their number to the counter on update.

 

There are a number of commands that got added to GeoGebra 4.0 to makes scripting work better. They let you perform actions through the script or input line what you would normally do through contextual menus and dialog boxes. I find it useful to break these commands into a small number of categories:

  • Commands to create controlling objects: Button, Checkbox, Slider, Textfield;

  • Commands to change properties of an object - Delete, Rename, SetBackgroundColor, SetCaption, SetColor, SetConditionToShowObject, SetCoords, SetDynamicColor, SetFilling, SetFixed, SetLabelMode, SetLayer, SetLineStyle, SetLineThickness, SetPointSize, SetPointStyle, SetTooltipMode, SetValue, ShowLabel, StartAnimation;

  • Commands to change aspects of a graphics window - HideLayer, Pan, SetActiveView, ShowLayer, UpdateConstruction, ZoomIn, ZoomOut

  • Commands to allow enriched scripting of commands - Execute, ParseToFunction, ParseToNumber;

  • Miscellaneous other commands - CopyFreeObject, GetTime, PlaySound, SelectObjects.

 

Someone asked for an example of a set of radio buttons.  This is done with a set of check boxes, where each box has a script that sets its value to true and the values of the other buttons to false.

 

 

© 2011, Mike May, S.J.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 license, Mike May, S.J. maymk@slu.edu

 

Comments (0)

You don't have permission to comment on this page.