1
$\begingroup$

Say I have a triangulation of an octagon:

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 I have a function which rotates the diagonals anticlockwise when clicked on.

RotateAnticlockwise[L_Line, n_Integer] := 
 Module[{line, vpair , pts = CirclePoints[n]}, 
  line = First@L /. Line -> List ; 
   vpair = Flatten[Position[pts, #] & /@ line] + {1, 1} /. n + 1 -> 1; 
    Line[{pts[[vpair[[1]]]], pts[[vpair[[2]]]]}]
]


triangulation[8, {{1,3},{1,4},{1,5},{1,6},{1,7}}] /. 
 l_Line -> FlipView[{{Green, l}, {Blue, Thick, RotateAnticlockwise[l, 8]}}]

Now, how can I do it so that I can keep rotating a diagonal as many times as I want, instead of just going back in forth? Equivalently, is there away of somehow saving the output we get after clicking and then using that output as our new base case? Is FlipView a bad choice here?

$\endgroup$
1
$\begingroup$
ClearAll[triangulate, colors, rotateDiags]
triangulate[n_, pts_] :=  Graphics[GraphicsComplex[CirclePoints[n], 
  {FaceForm[], EdgeForm[Black], Polygon[Range[n]], Green, Line /@ pts}]]

colors[n_, index_: 97] := ColorData[index] /@ Range[n]

rotateDiags[n_, pts_, index_: 97] := Deploy[triangulate[n, pts] /. l_Line :> 
    FlipView[{{Green, l}, 
       ## & @@ ({Thick, colors[n, index][[#]], l /. {i_, j_} :> Mod[# + {i, j}, 8, 1]} & /@ 
         Range[n - 1])}]]

Example:

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

enter image description here

$\endgroup$
5
  • $\begingroup$ First of all, thank you for your answer! IUnfortunately, I think I have oversimplified my question. What I am really after is the following, in correspondence with you code: take your initial pts, pass it to rotateDiags, run it; when you click on one of the diagonals, you get a new set of diagonals, call it S; when I then click on another diagonal, rotateDiags is ran again, but this time the argument is S. So basically, I want to sort of re-run rotateDiags, every time I press on of the diagonals, passing a different pts_ argument each time. Does that make sense? $\endgroup$ – amator2357 Feb 5 '20 at 16:20
  • $\begingroup$ I know that for rotating I would be getting exactly the same output anyway, but I might be doing something else to the diagonals later, for which I'm going to need the updating thing $\endgroup$ – amator2357 Feb 5 '20 at 16:22
  • $\begingroup$ @amator2357, maybe rotateDiags2[s_][n_, index_: 97] := Deploy[s /. l_Line :> FlipView[{{Green, l}, ## & @@ ({Thick, colors[n, index][[#]], l /. {i_, j_} :> Mod[# + {i, j}, 8, 1]} & /@ Range[n - 1])}]]; and use as s0 = triangulate[8, {{1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}}]; rotateDiags2[s0][8]? $\endgroup$ – kglr Feb 5 '20 at 16:36
  • $\begingroup$ It doesn't work when I modify to use it with my function. I'll explain what I am trying to do: each diagonal in a triangulation is a diagonal of some quadrilateral and each quadrilateral has precisely two diagonals, so when I click on some diagonal I want it to flip to that other diagonal of that quadrilateral, which will give me another triangulation, and this is the reason why I need the updating: the quadrilateral in which diagonal lies depends on the triangulation. $\endgroup$ – amator2357 Feb 5 '20 at 17:00
  • $\begingroup$ I use this function to flip the diagonal inside the quadrilateral: FlipDiagonal[T_,D_]:=Module[{TriangulationAsGraph=Graph[#]& @ T/. List -> UndirectedEdge}, Complement[Flatten@Select[IGCliques[TriangulationAsGraph,{3}],SubsetQ[#,D] &],D] ]. As an example: FlipDiagonal[{{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {1, 8}, {1, 3}, {3, 5}, {5, 7}, {5, 8}, {3, 8}},{1,3}] $\endgroup$ – amator2357 Feb 5 '20 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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