0

n00b Mathematica user here.

I wrote this code:

u[i_, j_] = 
 Polygon[{{Cos[Pi*i/3], Sin[Pi*i/3]} + 
r*{Cos[Pi*i/3 + 5 Pi/12 + Mod[j - i, 6]*Pi/6], 
  Sin[Pi*i/3 + 5 Pi/12 + Mod[j - i, 6]*Pi/6]}, {Cos[Pi*i/3], 
 Sin[Pi*i/3]} + 
r*{Cos[Pi*i/3 + 7 Pi/12 + Mod[j - i, 6]*Pi/6], 
  Sin[Pi*i/3 + 7 Pi/12 + Mod[j - i, 6]*Pi/6]}, {Cos[Pi*j/3], 
 Sin[Pi*j/3]} + 
r*{Cos[Pi*j/3 + 5 Pi/12 + Mod[i - j, 6]*Pi/6], 
  Sin[Pi*j/3 + 5 Pi/12 + Mod[i - j, 6]*Pi/6]}, {Cos[Pi*j/3], 
 Sin[Pi*j/3]} + 
r*{Cos[Pi*j/3 + 7 Pi/12 + Mod[i - j, 6]*Pi/6], 
  Sin[Pi*j/3 + 7 Pi/12 + Mod[i - j, 6]*Pi/6]}}]

Situation[edges_] := 
 Graphics[{Table[{Black, Disk[{Cos[Pi*i/3], Sin[Pi*i/3]}, r]}, {i, 0, 
  5}], Table[
 If[MemberQ[edges, Sort[{i + 1, j + 1}]], {Red, u[i, j]}, 
  Mouseover[{Black, u[i, j]}, {Red, u[i, j]}]], {i, 0, 5}, {j, 
  i + 1, 5}]}] /. r -> 0.094

and it works perfectly well. As you see, it draws a hexagon with all edges filled in, and the ones you specify are filled in as red, and the others swap from black to red when you hover over it.

But what I want to do is to have it so that when you click an edge, it stays red, that is, it calls Situation with the edge appended to the list. I tried to turn each edge into an event handler, with something like

Situation[edges_] := 
 Graphics[{Table[{Black, Disk[{Cos[Pi*i/3], Sin[Pi*i/3]}, r]}, {i, 0, 
  5}], Table[
 If[MemberQ[edges, Sort[{i + 1, j + 1}]], {Red, u[i, j]}, 
  EventHandler[
   Mouseover[{Black, u[i, j]}, {Red, u[i, j]}], {"Mouseclick" :> 
     Situation[Union[edges, {{i + 1, j + 1}}]]}]], {i, 0, 5}, {j, 
  i + 1, 5}]}] /. r -> 0.094

which didn't work. Upon further investigation, it appears that while the rectangles and mouseovers are defined within the original table, the EventHandler is not inherent to the edge and must look up the i and j when it's run. Is there a way (besides writing 15 different EventHandlers) to make this code work? Like some way to embed the i and j within the rectangles themselves?

Also, I would appreciate style tips in spacing out this code; it looks ugly but I'm not sure what the correct look should be. Thanks!

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Bob Jones
  • 131
  • 2
  • Welcome! I suggest the following:
    1. As you receive help, try to give it too, by answering questions in your area of expertise.
    2. Take the tour and check the faqs!
    3. When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
    –  Jul 24 '16 at 18:49
  • 5
    Haven't tested, edges not provided, but it looks like: http://mathematica.stackexchange.com/q/63267/5478 – Kuba Jul 24 '16 at 19:41
  • Thanks Kuba, turns out I was completely missing Dynamic, and the {k->i, l->j} trick in your link also made it work. I'm not sure how to close this question, but it can be closed. – Bob Jones Jul 25 '16 at 05:58

0 Answers0