2
$\begingroup$

I am trying to understand the use of Dynamic, DynamicModule, EventHandler, etc. I created an example which I am trying to work with, but I have been struggling to figure things out by just looking at the relevant examples from the documentation. Here goes the example:

edges[n_Integer]:=Join[Table[{i,i+1},{i,n-1}],{{1,n}}]

triangulation[n_Integer,d_List]:=Module[{pts,external=edges[n]},
 pts=CirclePoints[n];
  Graphics[Map[Line[{pts[[#[[1]]]],pts[[#[[2]]]]}]&,Join[d,external,{{1,n}}]]]]

triangulation[8,{{1,3},{1,4},{1,5},{1,6},{1,7}}]

enter image description here

Now, when I click at one of the diagonals, I would like that diagonal to, say, change a color, or rotate, or whatever I will ask her to do in the future. What would be the best way to achieve something of that sort?

$\endgroup$

2 Answers 2

2
$\begingroup$

An alternative way to implement triangulation by diagonal lines:

ClearAll[triangulate, flipDiagonalColors]
triangulate[n_, pts_] := Graphics[GraphicsComplex[
   CirclePoints[n], {FaceForm[], EdgeForm[Black], Polygon[Range[n]], Green, Line /@ pts}]]

An alternative way to use FlipView to cycle over a list of colors:

flipDiagonalColors[n_, pts_, colors_] := Deploy[triangulate[n, pts] /. l_Line :> 
   FlipView[Style[l, # /. {Green -> Thin, _ -> AbsoluteThickness[5]}, 
      CapForm["Round"], #] & /@ 
         Prepend[colors, Green]]]

Example:

flipDiagonalColors[8, {{1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}}, RandomColor[5]]

enter image description here

$\endgroup$
2
$\begingroup$
Graphics[
 {EdgeForm[Red], FaceForm[None], Polygon[CirclePoints[6]],
  FlipView[
   {
    {Green, Line[{CirclePoints[6][[1]], CirclePoints[6][[3]]}]},
    {Blue, Line[{CirclePoints[6][[1]], CirclePoints[6][[3]]}]}}]
  }]

Strong recommendation: provide the minimum example that illustrates your problem.

  • Do you really need all that triangulation code? Of course not.
  • Do you really need to define your form using arbitrary $n$ for CirclePoints? Of course not.
  • Do you really need to define a function edges? Of course not.
  • Do you really need to use best-practices for a Module? Of course not.

What a waste of your and your solvers' time?! It just adds opportunities for error, etc. What if someone knew precisely how to answer the (FlipView) aspect of your question but knew nothing about CirclePoints. That person would read your question and conclude it wasn't worth wasting time to learn it.

You could ask your question far more simply, and thus get more help with the following:

Here are two lines. I want to click on the left and rotate it by 90 degrees (and then back) and click on the right one and change its color (and back).

Graphics[
 {Line[{{0, 0}, {0, 1}}],
  Red, Line[{{2, 0}, {3, 0}}]}]

Can't you see why that is much better way to ask your question?

Answer:

Graphics[
 {FlipView[{Line[{{0, 0}, {0, 1}}], 
            Line[{{-1/2, 0}, {1/2, 0}}]}],
  FlipView[{
     {Red, Line[{{2, 0}, {3, 0}}]}, 
     {Green, Line[{{2, 0}, {3, 0}}]}}]}]
$\endgroup$
5
  • 3
    $\begingroup$ or Deploy[triangulation[8, {{1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}}] /. l_Line -> FlipView[{{Green, l}, {Blue, Thick, l}}]]? (+1) $\endgroup$
    – kglr
    Feb 5, 2020 at 2:59
  • $\begingroup$ Thank you for you post. The answers to the questions you asked are actually not at all obvious, even though, based on the information I provided, they may seem to be. I do agree that the examples one provides should be as simple as possible. On the other hand, the person asking the question is probably attempting to make sure that the answer which that person is hoping to receive, will be compatible with some other stuff, likely more complicatd, which they might currently be working on. So, yeah, sure, I could have made my example simpler, but that's not the point. $\endgroup$
    – amator2357
    Feb 5, 2020 at 11:00
  • $\begingroup$ And trying to judge how likely it is for the potential reader to be familiar with a certain command is, well, pointless? If I assume that reader is not familiar with CirclePoints, why I would then not assume that reader is not familiar with Line, and then with Graphics, etc.? And then I end up assuming that reader doesn't know anything and so what's is the point in asking the question? $\endgroup$
    – amator2357
    Feb 5, 2020 at 11:09
  • $\begingroup$ You miss the point entirely. The elementary fact is that if a potential solver knows k facts or functions, then s/he knows all the the subset facts. But if that if a potential solver knows k, there's no guarantee s/he knows more than that. If a question needs involves a small sets of facts or concepts, you cannot increase the chance the solver can help if he thinks the problem requires facts that it does not. Imagine a question text that involved dozens of advanced functions but ended "how do I save my answer?" Draw Venn diagrams if you still have difficulty with this. elementary fact. $\endgroup$ Feb 5, 2020 at 16:39
  • $\begingroup$ ....but it is worse than that. Even if you're positive every potential solver knows every function, you shouldn't include irrelevant functions, facts, or issues. Such irrelevances waste everyone's time, especially those coming later searching for a different topic than the core one at hand. So in that hypothetical example above, a later questioner interested in HyperGeometricFunctions will get erroneous and anomalous search hits from the poorly worded question that contains that function irrelevant to the question at hand. Clear now? $\endgroup$ Feb 5, 2020 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.