2
$\begingroup$

I am trying to show and calculate the area of two overlapping, shifted hexagons using the following code:

Needs["Polytopes`"]; DynamicModule[{tv, r, pt, sup},
LocatorPane[Dynamic[pt],
tv = Dynamic@TranslationTransform[pt][Vertices[Hexagon]];
r = Dynamic@RegionIntersection[{MeshRegion[Vertices[Hexagon], 
    Polygon[{1, 2, 3, 4, 5, 6}]], MeshRegion[tv, Polygon[{1, 2, 3, 4, 5, 6}]]}];
sup = Dynamic@Chop[N@RegionMeasure[r, 2], 10^-5];
If[Length[r] == 0, 
   Dynamic@Show[{RegionPlot[r], 
      Graphics[{{EdgeForm[Thick], White, Opacity[0], 
         Polygon[Vertices[Hexagon]]}, {EdgeForm[Thick], White, 
         Opacity[0], Polygon[tv]}}]}, Frame -> True, 
     PlotLabel -> "Area = " <> ToString[sup], 
     PlotRange -> {{-1, 3}, {-3, 3}}, AspectRatio -> 5.5/4], 
   Dynamic@Show[
     Graphics[{{EdgeForm[Thick], White, Opacity[0], 
        Polygon[Vertices[Hexagon]]}, {EdgeForm[Thick], White, 
        Opacity[0], Polygon[tv]}}], Frame -> True, 
     PlotLabel -> "Area = 0", PlotRange -> {{-1, 3}, {-3, 3}}, 
     AspectRatio -> 5.5/4]
   ]
  ]
 ]

My problem is that I cannot figure out why the intersection region is not plotted.

I obtain something like this:

enter image description here

$\endgroup$
1
  • $\begingroup$ Dynamic@TranslationTransform[pt][Vertices[Hexagon]]; does nothing. Dynamic updates pixels on the screen so whenever you have Dynamic@Something; nothing is being updated. $\endgroup$ Nov 13, 2014 at 21:32

1 Answer 1

3
$\begingroup$

Too many Dynamics and in the wrong place:

DynamicModule[{tv, r, pt = {1., 0.}, sup},
 LocatorPane[Dynamic[pt],
  Dynamic[
   tv = TranslationTransform[pt][Vertices[Hexagon]];
   r = RegionIntersection[{MeshRegion[Vertices[Hexagon], 
       Polygon[{1, 2, 3, 4, 5, 6}]], 
      MeshRegion[tv, Polygon[{1, 2, 3, 4, 5, 6}]]}];
   sup = Chop[N@RegionMeasure[r, 2], 10^-5];
   If[Length[r] == 0, 
    Show[{RegionPlot[r], 
      Graphics[{{EdgeForm[Thick], White, Opacity[0], 
         Polygon[Vertices[Hexagon]]}, {EdgeForm[Thick], White, 
         Opacity[0], Polygon[tv]}}]}, Frame -> True, 
     PlotLabel -> "Area = " <> ToString[sup], 
     PlotRange -> {{-1, 3}, {-3, 3}}, AspectRatio -> 5.5/4], 
    Show[Graphics[{{EdgeForm[Thick], White, Opacity[0], 
        Polygon[Vertices[Hexagon]]}, {EdgeForm[Thick], White, 
        Opacity[0], Polygon[tv]}}], Frame -> True, 
     PlotLabel -> "Area = 0", PlotRange -> {{-1, 3}, {-3, 3}}, 
     AspectRatio -> 5.5/4]]]]]

Mathematica graphics

Notes: (1) In your code, you have a couple of assignments like

tv = Dynamic@TranslationTransform[pt][Vertices[Hexagon]];

This means that tv would never be equal to TranslationTransform.... Dynamic[code] does not return the value of code. It holds code unevaluated to be interpreted by the Front End. When and if the Front End sees output to be displayed that contains Dynamic[code], it shows the current value of code and updates it according to the rules of dynamic updating (which are complex). This sort of assignment is rare, since one usually wants to use result, not just display it -- in your case, the translated hexagon tv is used to compute r.

(2) I removed the Dynamic around Show. The use of Dynamic inside a code block is to mark code that can be updated and displayed independently of some other variables. But the whole code displayed by the LocatorPane is a function of pt; it all needs to be evaluated whenever pt moves.

$\endgroup$
1
  • $\begingroup$ Nice explanation regarding the use of "Dynamic" $\endgroup$ Nov 14, 2014 at 10:23

Your Answer

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

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