3
$\begingroup$

I am not sure if my question makes any sense, however I will try to make it clear in the description. I am trying to plot a polygon inscribed inside a circle, where the variables are radius of the circle and number of sides. One way I did it was put all of the functions inside Manipulate command,however it is cumbersome and hard to read but it works:-

ClearAll["Global`*"]
Manipulate[
 ListLinePlot[
  Transpose@{Table[
     r/(Sqrt[3]*Sin[π/(n)] + Cos[π/(n)])*
      Cos[θ], {θ, 0, 2 π, (2 π)/n}], 
    Table[r/(Sqrt[3]*Sin[π/(n)] + Cos[π/(n)])*
      Sin[θ], {θ, 0, 2 π, (2 π)/n}]}, 
  AspectRatio -> Automatic], {{r, 15}, 5, 20}, {{n, 8}, 3, 30, 1}]

Output:

enter image description here

The other way I am approaching is to define the functions and then use Manipulate command. However things are not working out the way they should.

ClearAll["Global`*"]
f1[r_, n_, θ_] := 
  r/(Sqrt[3]*Sin[π/(n)] + Cos[π/(n)])*Cos[θ];
f2[r_, n_, θ_] := 
  r/(Sqrt[3]*Sin[π/(n)] + Cos[π/(n)])*Sin[θ];
g[f1_, f2_] := 
  Transpose@{Table[
     f1[r, n, θ], {θ, 0, 2 π, (2 π)/n}], 
    Table[f2[r, n, θ], {θ, 0, 2 π, (2 π)/n}]};
Manipulate[
 ListLinePlot[g[f1, f2], AspectRatio -> Automatic], {n, 3, 10, 1}]

The above code is unable to run because n, which is number of sides is not defined and so Mathematica is unable to create a list of functions.I want to know, how do I go about coding like this in Mathematica. Also I am using Mathematica 11.2.

I want to extract the co-ordinates from the plots generated above to finally create something like this:- Star generated using Matlab

I want to actually calculate the circumference of this star.I know that this can be achieved other ways but I need to have a visual representation. I have attached the Matlab code for this:-

r=6.5;
n=14;

alpha=pi/(n-1);
c=r/(sqrt(3)*sin(alpha)+cos(alpha));

theta=linspace(0,2*pi,n);
semi_theta=theta(2)/2;
x=zeros(1,n);
y=zeros(1,n);
xx=zeros(1,n+n-1);
yy=zeros(1,n+n-1);
i=1;
j=2;


while i<=n
    x(i)=c*cos(theta(i));
    y(i)=c*sin(theta(i));

    if i>=2
        side_length=sqrt((x(i)-x(i-1))^2+(y(i)-y(i-1))^2);

        x_temp=(r)*cos(theta(i)-alpha);
        y_temp=(r)*sin(theta(i)-alpha);

        xx(j)=x_temp;
        yy(j)=y_temp;

        xx(j-1)=x(i-1);
        xx(j+1)=x(i);
        yy(j-1)=y(i-1);

        yy(j+1)=y(i);

        j=j+2;
    end
    i=i+1;

end
plot(xx,yy)

perimeter=(n-1)*side_length*2;
disp(perimeter)
pbaspect([1 1 1])

Thanks, Arunim

$\endgroup$
  • $\begingroup$ It looks like you could either pass n to g as a third argument, or define g inside Manipulate -- something like Manipulate[ g[f1_,f2_]:=...; ListLinePlot... ]. The first is probably better and simpler. $\endgroup$ – jjc385 Nov 6 '17 at 19:49
  • $\begingroup$ @jjc385 Thanks for the reply. Essentially you want me to do it the first way. However, the problem is I plan to use this polygon to plot something else, like a star or something, so the first method will become even more cumbersome. $\endgroup$ – Arunim Bhattacharya Nov 6 '17 at 19:55
  • $\begingroup$ Could you edit the post with information explaining exactly what you'd like to do with the star (or something)? $\endgroup$ – jjc385 Nov 6 '17 at 19:58
  • 3
    $\begingroup$ CirclePoints[] is really nice for this: With[{n = 8, r = 1}, Graphics[{Polygon[CirclePoints[{r, 0}, n] ~Riffle~ CirclePoints[{r/(Cos[π/n] + Sqrt[3] Sin[π/n]), π/n}, n]]}]] $\endgroup$ – J. M. will be back soon Nov 6 '17 at 21:22
6
$\begingroup$

You can use LocalizeVariables->False

Manipulate[
  ListLinePlot[g[f1,f2],AspectRatio->Automatic],
{n,3,10,1},
LocalizeVariables->False
]

But you still to add control variable to set values for r.

ClearAll["Global`*"]
f1[r_, n_, \[Theta]_] := 
  r/(Sqrt[3]*Sin[\[Pi]/(n)] + Cos[\[Pi]/(n)])*Cos[\[Theta]];
f2[r_, n_, \[Theta]_] := 
  r/(Sqrt[3]*Sin[\[Pi]/(n)] + Cos[\[Pi]/(n)])*Sin[\[Theta]];
g[f1_, f2_] := Transpose@{
    Table[f1[r, n, \[Theta]], {\[Theta], 0, 2 \[Pi], (2 \[Pi])/n}],
    Table[f2[r, n, \[Theta]], {\[Theta], 0, 2 \[Pi], (2 \[Pi])/n}]
    };

Manipulate[
 ListLinePlot[g[f1, f2], AspectRatio -> Automatic],
 {n, 3, 10, 1},
 {r, 1, 10, 1},
 TrackedSymbols :> {n, r},
 LocalizeVariables -> False
 ]

enter image description here

$\endgroup$
  • $\begingroup$ Awesome!! Exactly what I was looking for. Thanks a lot! $\endgroup$ – Arunim Bhattacharya Nov 6 '17 at 20:13
  • $\begingroup$ @ArunimBhattacharya Don't forget to accept the answer if it fit your needs; just click on the tick on the top left of the answer. $\endgroup$ – anderstood Nov 6 '17 at 21:15
  • $\begingroup$ @anderstood done. $\endgroup$ – Arunim Bhattacharya Nov 6 '17 at 22:09
  • $\begingroup$ @ArunimBhattacharya You must have upvoted, but not accepted :) Acceptation is the button just below the "downvote" arrow. It represents a tick mark. $\endgroup$ – anderstood Nov 6 '17 at 22:27

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.