How to change the color of the geometric object in the list?

amvukovic shared this question 3 years ago
Answered

I have several independent polygons in graphic view: mnogokut1 to mnogokut6 (polygons' names).

I need to change color of some of those polygons later so I've created a list:

plocice={mnogokut1,mnogokut2,mnogokut3,mnogokut4,mnogokut5,mnogokut6}

Let's say I have a variable with the index of the clicked polygon... e.g. clicked = 4

The problem is that I'm using next code for changing color which doesn't work:

SetColor(Element(plocice,clicked),160/255,160/255,160/255)
when I want to change the color of mnogokut4 ...


I'm creating a memory game and have a var clicked which stores the index of the currently clicked polygon... after checking if the right pair of polygons is clicked, I want to change the color of those polygons...

I've even tried to create the list "plocice" with the names of polygons as textual variables, like:

plocice={"mnogokut1" etc.}

and this also doesn't work since the <Object> in the SetColor() command's syntax isn't supposed to be a textual variable...

Any help will be more than appreciated...

Thanks in advance...

Alex

Comments (6)

photo
2

There have been likewise questions about elements of a list, like SetValue(...) that didn't work either.

The problem of creating lists containing existing elements is that in reality, the lists don't contain the objects itselves, but a copy of them.

This implicates that you can't modify the original element by referring to the list. You only can modify them itselves, working with lists is not the best option.

An alternative you can find in this file on the 4 colors theorem, where clicking on whatever polygon activates the same script, so you can color with just one central scripting command, using the Execute command.

chris

photo
2

Another option is to use Dynamic Color, then you can just change the value of a number or boolean to change the color of each polygon.https://wiki.geogebra.org/e...

photo
2

A option more is to use Execute(). This use a list of strings and each string is interpreted as a command (string must be in US-english).

The strings can be connected-strings with constants and variables.

Example (n is the name of a variable with a value=5): "ABC"+n --> "ABC5"

--------

plocice={"mnogokut1" etc.}

Execute({"SetColor("+(Element(plocice,clicked))+",160/255,160/255,160/255)"})

------------

Attached a sample with points instead of polygons


photo
1

Dear all, THANK YOU FOR ALL YOUR SUGGESTIONS AND HELP from the bottom of my heart! I'm always thrilled with the help I get on this forum! <3

Since I wanted to solve this yesterday and the changing of the color was just one of the procedures that should be done when user properly pair the tiles (polygons), I've used Execute command with the Translate command... TO BE TOTALLY HONEST, after I've realized that you obviously can't change properties of the list's objects this way, I'VE COME TO THE EXECUTE() COMMAND (which I use more and more lately and it made me use JavaScript less and less which is great because my new applets are made completely within GeoGebra <3 ) BUT THE BIGGEST PROBLEM I'VE COME TO WAS HOW THE F... YOU WRITE ALL THE QUOTATION MARKS WITHIN EXECUTE() COMMAND?!? ;-) E.g. when you combine it with the Rename() command in which you have a new object's name argument as a text variable...

That's probably the main reason why I've switched to Translate() command until the same problem with the Execute() command and quotation marks emerged ;-)

So, this is the code that I've used in my open Memory game template:

If(a>0,If(check==true,Execute({"SetValue(bodovi,bodovi+10)","SetValue(brUparenih,brUparenih+1)","SetValue(feedb,Text(provj1))","trokutA=Translate(okvir3,Vector(M,Vertex(Element(plocice,CopyFreeObject(brKliknKartePrve)),1)))","Rename(trokutA,"+"trokut"+brKliknKartePrve+")","trokutB=Translate(okvir3,Vector(M,Vertex(Element(plocice,CopyFreeObject(brKliknKarteDruge)),1)))","Rename(trokutB,"+"trokut"+brKliknKarteDruge+")"}),Execute({"SetValue(bodovi,bodovi-5)","SetValue(feedb,Text(provj2))","SetValue(kliknuto,brKliknKartePrve,false)","SetValue(kliknuto,brKliknKarteDruge,false)"})))

(the procedure above is a part of the Check() procedure... )

Of course, now when I've come up with the idea how to use Execute() command and quotation marks it's not hard to see that it would be much easier if I have combined it with the SetColor() command ;-)

ALL OF YOU ONCE AGAIN THANK YOU VERY VERY MUCH! I really appreciate your help as well as your will to spare your free time!

Alex

photo
1

for me it's not really clear what's the problem (what you want, what you do, what the wrong result)

but maybe try this syntax:


"Rename(trokutA,"+("trokut"+brKliknKartePrve)+")"

Note the brackets:: +(<string>+<variable>)+

alternative


"Rename(trokutA, trokut"+brKliknKartePrve+")"

photo
1

Dear rami, I obviously wasn't clear enough in my previous reply, sorry...


I've solved the problem with the Execute() command combined with Translate() command (and was just explaining that the main problem was where to put quotation marks but I figured it out eventually) and that's why I've posted my solution... but as always I was too long and too extensive in my reply ;-) and that's why you've probably thought that I still have problems... Once again sorry for that!

The moment I saw your suggestion with Execute() command combined with SetColor() command I've realized that it would be much easier and more elegant way... and that was the real point of my reply :-)

Once again thanks for your answer and I hope that the combination of the Execute() command and quotation marks will no longer be the problem in my future solutions ;-)

Alex

photo
© 2021 International GeoGebra Institute