Difference between revisions of "Tutorial:Introduction to GeoGebraScript"

From GeoGebra Manual
Jump to: navigation, search
Line 35: Line 35:
  
 
Now it's time to actually create a script, we will create a silly example using an ''Update'' script first:
 
Now it's time to actually create a script, we will create a silly example using an ''Update'' script first:
{{example|1=(Update script)
+
{{example|1=(Update script, you can also find the whole construction in [http://www.geogebra.org/material/show/id/1519 GeoGebra])
 
# First of all, open a blank GeoGebra 4 window
 
# First of all, open a blank GeoGebra 4 window
 
# Now create a point "A" and a line "a". Try out the [[Line Command|Line[..] command]] if you're not familiar with creating objects from the [[Input Bar]].
 
# Now create a point "A" and a line "a". Try out the [[Line Command|Line[..] command]] if you're not familiar with creating objects from the [[Input Bar]].
Line 45: Line 45:
 
<ol><li value="5">Try to figure out what the script does, then check your assumption by moving the point A around!</li></ol>
 
<ol><li value="5">Try to figure out what the script does, then check your assumption by moving the point A around!</li></ol>
 
}}
 
}}
We've been using the [[SetLineThickness Command|SetLineThickness[]]] and [[SetPointSize Command|SetPointSize[]]] commands to achieve this (unfortunately rather useless) effect, two of those commands intended for scripting we've talked about earlier. At some point you should at least skim the [[Scripting Commands|scripting command list]] to get an idea what's possible with these commands.
+
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 have something to do with the point itself.
  
You can also find the whole construction in [http://www.geogebra.org/material/show/id/1519 GeoGebra].
+
We've been using the [[SetLineThickness Command|SetLineThickness[]]] and [[SetPointSize Command|SetPointSize[]]] commands, two of those commands intended for scripting we've talked about earlier. At some point you should at least skim the [[Scripting Commands|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 <code>f(x) = x^2</code> or <code>c = Circle[(0,0), 5]</code> 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.
 +
 
 +
{{note|1=Many scripting commands just perform an action once and do not form a permanent connection as you may know them from [[Free,_Dependent_and_Auxiliary_Objects|dependent objects]], so e.g. <code>SetLineThickness[a, Distance[A, a]*2]</code> will just copy the current value of <code>Distance[A,a]*2</code> at 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.}}
 +
 
 +
{{note|If you're familiar with other programming languages you may have noticed that there is no need for a semicolon at the end of a line in GeoGebraScript.}}
  
 
== ToDo ==
 
== ToDo ==
Line 55: Line 61:
 
* More concrete examples (also links to GeoGebra files using scripting)
 
* More concrete examples (also links to GeoGebra files using scripting)
  
==Buttons==
+
==Example Scripts==
  
=== Increment/Decrement Buttons ===
+
=== Buttons: Increment/Decrement Buttons ===
 
Instead of using a showing a slider to go through a step-by-step explanation, you can use a button.
 
Instead of using a showing a slider to go through a step-by-step explanation, you can use a button.
  

Revision as of 22:39, 6 November 2011

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.

Common uses of scripting are:
  • 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 two scripting languages in GeoGebra, GeoGebraScript and JavaScript, but this introduction focuses on GeoGebraScript as it uses the GeoGebra command syntax and is thus much easier to understand and learn for GeoGebra users. There are some differences between the scripting languages feature-wise, but GeoGebraScript is powerful enough for most situations.

Note: Scripting is a topic intended for experienced users of GeoGebra, we recommend you to take a look at the other tutorials before you dive into this topic if you're not that familiar with GeoGebra at the moment. (The basics about how scripting works and how you can add scripts to your constructions are explained here of course.)

Background: Commands

There are not only tools to create an object in GeoGebra. You can do the same with commands and the input-bar.

Example: With the command 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.

Scripting in GeoGebra is object-centered, which means that you can define scripts for each object –one script for clicks, one for updates–, but no script can exist without an object (there is one exception for scripting using JavaScript).

To add scripts to an object just right-click the object in either the Algebra or Graphics View, click the Object Properties… item and select the Scripting tab. Three more tabs will appear, On Click, On Update and Global JavaScript. The third one won't be of interest in this tutorial, but the two others can be used to add Click and Update scripts to the object you selected.

Click to see larger version

Now it's time to actually create a script, we will create a silly example using an Update script first:

Example: (Update script, you can also find the whole construction in GeoGebra)
  1. First of all, open a blank GeoGebra 4 window
  2. 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.
  3. Open the On Update tab of the scripting panel as described in the paragraph above.
  4. Now enter
SetLineThickness[a, Distance[A, a]*2]
SetPointSize[A, Distance[A,xAxis]]

as a script, click the OK button (very easy to miss!) and close the dialog.

  1. 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 have something to do with 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.

Note: Many scripting commands just perform an action once and do not form a permanent connection as you may know them from dependent objects, so e.g. SetLineThickness[a, Distance[A, a]*2] will just copy the current value of Distance[A,a]*2 at 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.
Note: If you're familiar with other programming languages you may have noticed that there is no need for a semicolon at the end of a line in GeoGebraScript.

ToDo

If you know something (a little) about scripting: You're 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
  • Explanation of "scripting objects": Textfields (!!), buttons
  • More concrete examples (also links to GeoGebra files using scripting)

Example Scripts

Buttons: Increment/Decrement Buttons

Instead of using a showing a slider to go through a step-by-step explanation, you can use a button.

Example:
  • Create an integer slider, called Steps.
  • Create a Button using Button Tool
  • Enter for caption the visible label on the button
  • Enter the script SetValue[Steps,Steps+1] or simply Steps=Steps + 1


To decrement change "+" to "-". To change the size of the steps change 1 to your value.

Note: The command Steps=Steps + 1 is only available in scripting. If you enter a command like that in the input bar, you'll get an error circular definition
© 2022 International GeoGebra Institute