Tutorial:Introduction to GeoGebraScript
Sometimes the ways students can interact with your worksheets using the basic features provided by GeoGebra are not powerful enough or too difficult to be used by your students, especially younger ones. GeoGebraScript is there to help you creating simple-to-use constructions with more sophisticated interactivity. It is a simple way to write scripts (small programs) in GeoGebra.
- Checking student's answers by letting them enter their answers into textfields and using scripts to check if they are correct.
- Creating new tasks / challenges after the student pressed a button, either randomly generated or using a set of challenges defined by you.
- Starting animations or revealing new parts of the construction after the student pressed a button or finished his task.
- (and so on..)
There are not only tools to create an object in GeoGebra. You can do the same with commands and the input-bar.
Circle[(1,2),3]you'll get a circle with the center at (1,2) with a radius 3.
Even if you use tools to create an object GeoGebra uses commands in the background to actually create the object. To see the command used to create an object can be found in the Properties Dialog under Definition. So tools are more or less only a help to enter those commands with the mouse. A list of all commands to create an object can be found here.
We've only spoken about commands that create objects so far. The majority of commands create objects, but there are some commands intended to be used in scripts, something we will talk about in more detail later (for the curious, see Scripting Commands).
Basics of Scripting
Scripts are, at heart, just a sequence of GeoGebra commands executed in the following two situations:
- Click: A script is executed after the user clicked on the object.
- Update: A script is executed when the value or properties of the object changed. This may happen if the user moves elements of the construction around, changes sliders, etc.
Now it's time to actually create a script, we will create a silly example using an Update script first:
- First of all, open a blank GeoGebra 4 window
- Now create a point "A" and a line "a". Try out the Line[..] command if you're not familiar with creating objects from the Input Bar.
- Open the On Update tab of the scripting panel as described in the paragraph above.
- Now enter
SetLineThickness[a, Distance[A, a]*2] SetPointSize[A, Distance[A,y=0]]
as a script, click the OK button (very easy to miss!) and close the dialog.
- Try to figure out what the script does, then check your assumption by moving the point A around!
As you may have noticed we assigned the update script to the point, not e.g. the line. This means that the script is executed every time you move the point. It doesn't mean, however, that the script must modify any attribute of the point itself.
We've been using the SetLineThickness and SetPointSize commands, two of those commands intended for scripting we've talked about earlier. At some point you should at least skim the scripting command list to get an idea what's possible with these commands. You're not restricted to this kind of command though, you can e.g. use
f(x) = x^2 or
c = Circle[(0,0), 5] in your scripts as well.
Apart from some special features, described in more detail below, executing a GeoGebraScript has the same effect as entering the single lines of the script into the input bar. You now basically know how to write scripts in GeoGebra, the rest of this tutorial will be about some special objects for scripting like textfields or buttons. Apart from the scripting commands the most useful one is probably If[...], you should look at the other logic and list commands as well if you did not already.
SetLineThickness[a, Distance[A, a]*2]will just copy the current value of
Distance[A,a]*2at the time of the execution, afterwards that value will not change unless you execute the command again or change the thickness manually. Our example may make the impression that there is some kind of connection, but that's just because the script is executed every time the point is moved. You can see that by moving the line instead. The line thickness will not change unless you move the point again.
Buttons are great because all your students know that they can click them and will try to do that. Apart from that buttons don't have any special behavior, so a click script of a button is as good as a click script of any other object. You can create a button using the Button Tool.
Input boxes (also called textfields), see the Input Box Tool, are maybe the most powerful objects in GeoGebra. They allow your students to enter text, numbers, functions and every other input you can (manually) convert from text to some GeoGebra objects.
There are multiple ways how you can use input boxes in your construction, you can either link them with objects, associate a click script to them or process them later using another object (e.g. a button). Don't worry, everything will be explained in detail right now.
Linked Input Boxes
Linked input boxes are input boxes which are linked to an existing GeoGebra object: That means that the input box shows the current value of the object and if you change the value in the input box the original object will change its value as well. You can use that to let your students change function definitions or numbers without using sliders. Basically they are input boxes which can just change a single object and thus much easier to use and without any danger of students damaging their worksheets.
The following example illustrates how you can create linked input boxes in GeoGebra.
- Open a blank GeoGebra window
- Enter a function, e.g.
f(x) = x^2, into the input bar
- Select the Input Box Tool in the toolbar (in the second tool group from the right).
- Now click on some spot in your worksheet, a dialog will pop out. For the caption enter "f(x) =" and as linked object select your function f.
- You're done. Move the graph of f to see how the textfield will automatically display the new definition of your function. You can also change the textfield and press enter to see how you can change the function's definition using the textfield.
Input Boxes with Click Scripts
In contrast to the first example we will now not link the input box to a specific object but use its Click script to apply its value to other objects (the reason why this is called click script is unknown to the author ;-)). The script will be very similar to the one in the first example, the only new question is how to use the value of the textfield in the script. The answer is
- Open a blank GeoGebra window
- Create a point
Aand a line
- Select the input box tool and click onto the canvas (as above), but this time use the label "Size:" and leave the "Linked Object" field empty.
- Go to the Click script tab of the new input box (instructions how to do that way above).
- Now add the following script (note the
SetPointSize[A, %0/2] SetLineThickness[a, %0]
- Click "Ok" again and close the dialog.
- Now enter a new value into the input box and press enter.
As you can see the placeholder
%0 indeed contains the value of the input box.
If you know something (a little) about scripting: Your help and participation is very welcome! You could add something for the following topics or think of something else:
- More step-by-step instructions for beginners
- More concrete examples (also links to GeoGebraTube files using scripting)
Buttons: Increment/Decrement Buttons
Instead of using a showing a slider to go through a step-by-step explanation, you can use a button.
- Create an integer slider, called
- Create a Button using Button Tool
- Enter for caption the visible label on the button
- Enter the script
Steps=Steps + 1
To decrement change "+" to "-". To change the size of the steps change 1 to your value.
Steps=Steps + 1is only allowed for free numbers. For dependent numbers you'll get an error circular definition