Tutorial:Introduzione a GeoGebraScript

Da GeoGebra Manual.
Versione del 7 nov 2011 alle 11:18 di Mathmum (discussione | contributi) (Creata pagina con 'A volte le modalità di base offerte da GeoGebra per interagire con i fogli di lavoro non sono sufficientemente potenti, oppure sono al contrario di difficile applicazione, speci...')
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

A volte le modalità di base offerte da GeoGebra per interagire con i fogli di lavoro non sono sufficientemente potenti, oppure sono al contrario di difficile applicazione, specialmente per gli utenti più giovani.. GeoGebraScript consente la creazione di costruzioni semplici da utilizzare, ma dotate di interattività a livello particolarmente sofisticato: è lo strumento per scrivere script, cioè programmare in GeoGebra.

Tra gli utilizzi più comuni dello scripting possiamo citare:
  • La verifica delle risposte degli studenti, immesse in campi testo.
  • La generazione automatica di nuove attività in seguito alla pressione di un pulsante, sia in ordine casuale che utilizzando un insieme predeterminato definito dal creatore del foglio di lavoro.
  • L'avvio di animazioni o la visualizzazione di parti nascoste della costruzione in seguito alla pressione di un pulsante o al completamento dell'attività proposta all'utente.
  • (e molte altre ...)

In GeoGebra sono disponibili due linguaggi per lo scripting, GeoGebraScript e JavaScript: questo tutorial si occuperà di GeoGebraScript, in quanto questo utilizza la sintassi dei comandi di GeoGebra e quindi è più semplice e intuitivo per gli utenti, oltre ad offrire funzionalità applicabili alla gran parte delle applicazioni.

Note: Lo scripting è un'attività di solito riservata agli utenti esperti di GeoGebra, quindi consigliamo agli utenti non esperti di dare un'occhiata agli altri tutorial prima di affrontare questa caratteristica avanzata di GeoGebra.

Il punto di partenza: i comandi

Per creare un oggetto in GeoGebra è possibile utilizzare gli strumenti, i comandi e la barra di inserimento.

Esempio: Il comando Circonferenza[(1,2),3] genera una circonferenza di centro (1,2) e raggio 3.

Anche se si utilizza uno strumento per creare un oggetto, in realtà GeoGebra utilizza un comando per la creazione dello stesso. Se si desidera conoscere il comando utilizzato per generare un oggetto, aprire la finestra di dialogo Proprietà e fare riferimento al campo Definizione. In pratica, gli strumenti sono solo una "scelta rapida" per inserire i comandi con il mouse. L'elenco completo dei comandi è disponibile qui, mentre l'elenco completo dei comandi utilizzabili negli scripting è disponibile qui).

Informazioni di base sullo scripting

Uno script è una sequenza di comandi di GeoGebra che vengono eseguiti nelle seguenti due situazioni:

  • Al clic: lo script viene eseguito dopo un clic del mouse dell'utente sull'oggetto.
  • All'aggiornamento: lo script viene eseguito quando il valore o le proprietà dell'oggetto si modificano, come ad es. dopo il movimento di un punto nella Vista Grafica, o la modifica del valore assunto da uno slider.

In GeoGebra lo scripting è orientato agli oggetti, cioè è possibile attribuire uno script a ciascun oggetto della costruzione, (sia di tipo "Al clic" che "All'aggiornamento"), ma non è possibile definire uno script senza associazione ad un oggetto (diversamente da JavaScript).

Per associare uno script a un oggetto fare clic con il tasto destro del mouse sull'oggetto, indifferentemente nella Vista Algebra o nella Vista Grafica, selezionare l'opzione Proprietà…, quindi la scheda Scripting. Tale scheda ne contiene altre tre: Al clic, All'aggiornamento e Globali 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:

Esempio: (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 barra di inserimento.
  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]
Setpuntiize[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 ad es. 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 Setpuntiize[] 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 ad es. 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 barra di inserimento. 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.

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 ad es. 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.

Buttons

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 Tool Insert Button.gif Insert Button Tool.

Input Boxes

Input boxes (also called textfields), see the Tool Insert Textfield.gif Insert 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 (ad es. 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.

Esempio: (Linked input boxes, the construction can be found at GeoGebra again)
  1. Open a blank GeoGebra window
  2. Enter a function, ad es. f(x) = x^2, into the barra di inserimento
  3. Select the Tool Insert Textfield.gif Insert Input Box Tool in the toolbar (in the second tool group from the right).
  4. 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.
  5. 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.
Note: As mentioned above you can not only link functions, but also numbers and basically ever other object, just try it out.

Input Boxes with Click Scripts

On 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 new issue now is how to access the value of the input box in the script. The answer is %0. Seriously.

Esempio: (Input box with click script, see GeoGebra)
  1. Open a blank GeoGebra window
  2. Create a point A and a line a.
  3. 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.
  4. Go to the Click script tab of the new input box (instructions how to do that way above).
  5. Now add the following script (note the %0):
Setpuntiize[A, %0/2]
SetLineThickness[a, %0]
  1. Click "Ok" again and close the dialog.
  2. Now enter a new value into the input box and press enter.

As you can see the placeholder %0 indeed carries the value of the input box.

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
  • 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.

Esempio:
  • 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 barra di inserimento, you'll get an error circular definition

en:Introduction to GeoGebraScript

© 2024 International GeoGebra Institute