geogebra

Can I define my own commands in GeoGebra script?


I am brand new to GeoGebra and am trying to write a simple visualization. I have a collection of vectors and I want to change the formatting depending on the value of a dot product. I am currently doing so with the following code:

If(abs(Dot(EqmDir,Fad))<0.1,SetColor(Fad,red),SetColor(Fad, darkgreen))
If(abs(Dot(EqmDir,Fbd))<0.1,SetColor(Fbd,red),SetColor(Fbd, darkgreen))
If(abs(Dot(EqmDir,Fcd))<0.1,SetColor(Fcd,red),SetColor(Fcd, darkgreen))

but I hate having so much copy and paste. I wish I could write a command called FormatMyVector so that the above code would be replaced with:

FormatMyVector(Fad)
FormatMyVector(Fbd)
FormatMyVector(Fcd)

Does GeoGebra support that kind of programming?

I tried Googling around to figure this out, but either it isn't possible, or I'm not finding the right collection of terms to search for.


Solution

  • You can reduce the amount of copy-paste in two steps. First you can invert the order of If and SetColor

    SetColor(Fad,If(abs(Dot(EqmDir,Fad))<0.1,"red","darkgreen"))
    

    then you can extract the second argument into a custom tool, by creating an object

    vecColor=If(abs(Dot(EqmDir,Fad))<0.1,"red","darkgreen")
    

    and creating a custom tool (named VecColor) with inputs EqmDir,Fad and output vecColor.

    SetColor(Fad,VecColor(Fad,EqmDir))
    

    If you want to write more complicated scripts where you need a fully-featured programming language, you can switch from GeoGebraScript to JavaScript.