Drawing a figure showing an $n$-gon and its direction into triangles

I don't have so much experience with Mathematica. Could anyone help me to reproduce this picture with Mathematica? • This may be of interest – Bob Hanlon Aug 4 '16 at 0:24

This is how I would implement the geometry demonstration you want to make. Perhaps it will work for you too.

With[{color = RGBColor[0.45, 0.75, 1.0]},
Manipulate[
GraphicsRow[
{Graphics[
{EdgeForm[Black], FaceForm[White], Polygon[CirclePoints[{1, 0}, n]],
FaceForm[color], Triangle[pts]}],
Graphics[
{EdgeForm[Black], FaceForm[color], Triangle[pts],
Dashed, Line[{pts[], .5 (pts[] + pts[])}],
Text[
With[{n = n}, HoldForm["θ" = π/n]], .375 pts[] + .125 pts[]]}]},
Frame -> All],
{θ, None},
{pts, None},
{{n, 6}, Range[3, 9], Setter},
TrackedSymbols :> {n},
Initialization :> (
θ := 2. π/n;
pts := {{0., 0.}, {1., 0.}, {Cos[θ], Sin[θ]}})]] Update

Adding a lot labels, while always doable, is always rather fussy. It takes a fair amount of trial and error to get the positioning parameters adjusted so the labels are in positions where they don't interfere with the geometry or with each other. When building an interactive demonstration, it gets worse because many of the labels will require dynamic relocation.

So the code I'm adding below, which includes the labels you requested, is considerably longer and somewhat more complex than my first go at answering your question.

{r_?NumericQ, θ_?NumericQ}, pivot : {_?NumericQ, _?NumericQ} : {0, 0}] :=
r {Cos[θ], Sin[θ]} + pivot

centroid[{p1_?NumericQ, p2_?NumericQ, p3_?NumericQ}] :=
RegionCentroid[Triangle[{p1, p2, p3}]]

With[{color = RGBColor[0.45, 0.75, 1.0]},
DynamicModule[{names, θ, p1, p2, p3, s1, s2, h, pts, h0},
Manipulate[
GraphicsRow[
{Graphics[
{EdgeForm[Black], FaceForm[White], Polygon[CirclePoints[{1, 0}, n]],
FaceForm[color], Triangle[pts],
{Red, Thick, Circle[p1, s1/4, {0, θ}]},
Text[Style[Row[{"θ =", 2 π/n}], 9], centroid[pts]],
Inset[names[[n - 2]], {Right, Top}, {Right, Top}],
Inset[Row[{"θ = ", θ/Degree, "°"}], {Right, Bottom}, {Right, Bottom}]},
ImageSize -> 250],
Graphics[
{EdgeForm[Black], FaceForm[color], Triangle[pts],
{Dashed, Line[{p1, h0}]},
{Red, Thick, Circle[p1, s1/4, {0, θ/2}]},
Text[Style["h", 12], radialPoint[{.6 h, .6 θ}]],
Text[Style[" s", 12], h0, {-1, -1}],
Text[Style["r", 12], radialPoint[{.5 s1, -5 °}]],
Text[Style["r", 12], radialPoint[{.5 s2, θ + 6 °}]],
Text[
Style[Row[{HoldForm["θ"/2], " =", π/n}], 9], centroid[{p1, p2, h0}]]},
ImageSize -> 250]},
Frame -> All],
{{n, 6, "sides"}, Range[3, 9], Setter},
TrackedSymbols :> {n},
SaveDefinitions -> True,
Initialization :> (
names =
{"Triangle", "Square", "Pentagon", "Hexagon",
"Heptagon", "Octagon", "Nonagon"};
θ := 2. π/n;         (* apex angle of the blue triangle *)
p1 = {0., 0.};
p2 = {1., 0.};
p3 := {Cos[θ], Sin[θ]};
pts := {p1, p2, p3}; (* vertices of the blue triangle *)
h0 := (p3 + p2)/2;   (* foot of the altitude of the blue triangle *)
s1 = Norm[p2];       (* lower side of the blue triangle *)
s2 = Norm[p3];       (* upper side of the blue triangle *)
h := Norm[h0])       (* altitude of the blue triangle *)]]]

Here is how the demonstration generated by the updated code looks when n = 3 and n = 9.

Implementation Notes

• I introduced many new local variables to keep the code (I hope) readable. So I'm no longer using the {var, None} trick to localize variables; rather I wrapped the the Manipulate expression with DynamicModule` to do that.
• I defined the helper function radialPoint because I often find it easier to think about placing objects in graphics in terms of a radial position with respect to a pivot point. Here I use it to place labels.
• After trying several other schemes, I finally decided to place the angle labels at the centroid of triangle containing the angle. Hence, the helper function centroid.
• The variables which get initialized in the initialization section fall into two categories, static and dynamic; i.e., those that do not depend on n and those that do. I think it important to point out that the static ones are initialized with Set ( = ) and the dynamic ones with SetDelayed ( := ), because this is something beginners often stumble over.
• You will find a lot mysterious real numbers appearing as adjustment factors in the expressions that place text. Those are the fuss factors I mentioned at beginning of this update. they fine tune the positioning. I work them out by trial-and-error. I have never found any automatic way to generate them. Making graphics look decent is something of art.
• Your answer is really complete, thanks! Could you just insert in the triangle "h" for the height, "b" for the base and "y" for one of the two other side? – Sandra Ross Aug 4 '16 at 2:55
• Hi @Sandra, you can try modifying m_goldberg's answer yourself. If you notice, he used the Text[] primitive to put text on the right panel. You can try using that to put the labels you want. – J. M. will be back soon Aug 4 '16 at 5:10
• It's interesting how you introduce variables. Do you think the {θ, None} control spec is equivalent to defining θ in DynamicModule (I would do it like that), and do you know if Mathematica internally does that, i.e. moves Initialization block to the start of DynamicModule? – BoLe Aug 4 '16 at 7:26
• I have been able to change it, but the angle $\theta = \frac{\pi}{5}$ is wrong; the angle should be $\theta = \frac{\pi}{2 \cdot 5}$ – Sandra Ross Aug 4 '16 at 15:20
• @SandraRoss. The angle I indicate is the one between the dashed line and the lower side. – m_goldberg Aug 4 '16 at 15:25