Graphics, Figures & TablesPSTricks | Draw regular polygons on top of each other

Information and discussion about graphics, figures & tables in LaTeX documents.
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks | Draw regular polygons on top of each other

Post by CrazyHorse »

svend_tveskaeg wrote:Hi again, Herbert.

I am starting to confuse myself. :?

Is there any chance that I can get you to

(1) create the to figures in the attached pictures for me, and
(2) take a look at my attempt to understand your example and comment on it?

Ad (1): I tend to learn something like this better by studying examples.
Ad (2): I would really like to understand your example.

A mixture of examples, explanations, and my own attemps often seem to be the best approach for me.

P.S. The two figures are created using the same ugly approach as in the initial approach to creating the triangle with the hexagon on top.
the first one is quite simple and I suppose you can get the secong yourself with the help from the code of the first example:

Code: Select all

\documentclass{article}
\usepackage{pst-eucl}

\begin{document}

\def\valA{4 }% space at the end!
\def\valK{1.75 }% space at the end!

\psset{unit=1.5,offset=-12pt}
\begin{pspicture}(-0.2,0)(\valA,\valA)
  \pstGeonode[CurveType=polygon,PointName=,
              PointSymbol=none](0,0){A}(\valA,0){B}(\valA,\valA){C}(0,\valA){D}
  \pnode(!\valA % horizontal distance value
    dup \valK sub % a-k = 2x
    2 div         % a-k/2=x
   ){b1}
  \pnode(!\psGetNodeCenter{b1} b1.x b1.y \valK add){b2}
  \psdots(b1)(b2)\uput[180](b1){b1}\uput[180](b2){b2}
  \pcline{|-|}(A)(B)\ncput*[nrot=:U]{$a$}
  \pcline{|-|}(B)(b1)\ncput*[nrot=:U]{$x$}
  \pcline{|-|}(b1)(b2)\ncput*[nrot=:U]{$k$}
  \pcline{|-|}(b2)(C)\ncput*[nrot=:U]{$x$}
  \psset{linestyle=dashed}
  \psline(b1)(! \psGetNodeCenter{b2}
     b2.y % as x value
     0)
   \psline(b2)(! \psGetNodeCenter{b2} b2.y \valA)
   \psline(!\psGetNodeCenter{b1} b1.y 0)(! \psGetNodeCenter{b1} 0 b1.y )
   \psline(!\psGetNodeCenter{b2} 0 b2.y )(! \psGetNodeCenter{b1} b1.y \valA)
\end{pspicture}

\end{document}
I marked the two important nodes b1 and b2. We do not need more, all other can be derived from these two.
Attachments
xx.png
xx.png (4.06 KiB) Viewed 6838 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks | Draw regular polygons on top of each other

Post by svend_tveskaeg »

Once again, very elegant!

Hmmm. I feel that a generalisation of your code is only obvious from the pentagon with decagon on top and onwards. (Sorry, I should of course have said that in my previous post! :oops: )

I have written the attached note (in Danish, but I hope the computations make sense ayway) for my students; this illustrates why I think the generalisation is not obvious before the pengagon with the decagon on top.

P.S. I am still trying to understand RPN notation, but I will post my questions regarding this in another forum.

Update:
Can \valK be made relative to \valA?

Update 2:
If I can get you to also draw the pentagon+decagon for me, I will try to create a hexagon+dodecagon and post my try here.
Attachments
week7-exercise.pdf
Polygon exercise.
(292.71 KiB) Downloaded 406 times
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks | Draw regular polygons on top of each other

Post by CrazyHorse »

svend_tveskaeg wrote:Once again, very elegant!

Hmmm. I feel that a generalisation of your code is only obvious from the pentagon with decagon on top and onwards. (Sorry, I should of course have said that in my previous post! :oops: )

I have written the attached note (in Danish, but I hope the computations make sense ayway) for my students; this illustrates why I think the generalisation is not obvious before the pengagon with the decagon on top.

P.S. I am still trying to understand RPN notation, but I will post my questions regarding this in another forum.

Update:
Can \valK be made relative to \valA?
yes, but is easier to set \valA=1 and \valK<1, then it is relative:
(The main nodes are P0,P1,... and the nodes on the sides are P0:1 and P0:2 for the first side and so on)

Code: Select all

\documentclass{article}
\usepackage{pst-eucl}

\begin{document}

\def\valA{1 }% space at the end!
\def\valK{0.3 }% space at the end!

\psset{unit=4,offset=-12pt}

\begin{pspicture}(-0.2,-0.5)(\valA,\valA)
  \pstGeonode[CurveType=polygon,PointName=,PointSymbol=none]
    (0,0){P0}
    (\valA,0){P1}
    (! \valA dup add 36 cos mul 36 PtoC){P2}
    (!\valA dup add 36 cos mul 72 PtoC){P3}
    (\valA;108){P4}
  \multido{\iA=0+72,\iP=0+1}{5}{%
    \rput{\iA}(P\iP){%
      \pnode(! \valA \valK sub % a-k = 2x
                2 div          % a-k/2=x
                0              % the y value
            ){P\iP:1}
     \pnode(!\psGetNodeCenter{P\iP:1} \valA P\iP:1.x sub 0){P\iP:2} 
    }% end of \rput
  }% end of multido
  \pcline{|-|}(P0)(P1)\ncput*[nrot=:U]{$a$}
  \pcline{|-|}(P1)(P1:1)\ncput*[nrot=:U]{$x$}
  \pcline{|-|}(P1:1)(P1:2)\ncput*[nrot=:U]{$k$}
  \pcline{|-|}(P1:2)(P2)\ncput*[nrot=:U]{$x$}
  \psset{linestyle=dashed}
  \multido{\iA=0+1,\iB=1+1}{4}{\psline(P\iA:2)(P\iB:1)}
  \psline(P4:2)(P0:1)
\end{pspicture}

\end{document}
Attachments
xx.png
xx.png (7.69 KiB) Viewed 6830 times
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks | Draw regular polygons on top of each other

Post by svend_tveskaeg »

Herbert, you are (of course) a PSTricks wizard! 8-)

1) Can you show my how to make \valK equal to, say, \valA/sqrt(5)? (For the square, \valK = \valA/sqrt(5), autually.) I would just like to see how it is done; I have no intention of putting in into the code.
2) I will post be try with the hexagon+dodecagon later tonight.
3) I will also try to rewrite your code for the pentagon+decagon to get the triangle+hexagon and square+octagon. (I have no intention of posting this unless someone would like to see it.)

I will mark this thread as solved as soon as question 1) is answered.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks | Draw regular polygons on top of each other

Post by CrazyHorse »

svend_tveskaeg wrote:Herbert, you are (of course) a PSTricks wizard! 8-)

1) Can you show my how to make \valK equal to, say, \valA/sqrt(5)? (For the square, \valK = \valA/sqrt(5), autually.) I would just like to see how it is done; I have no intention of putting in into the code.
2) I will post be try with the hexagon+dodecagon later tonight.
3) I will also try to rewrite your code for the pentagon+decagon to get the triangle+hexagon and square+octagon. (I have no intention of posting this unless someone would like to see it.)

Code: Select all

\def\valK{ \valA 5 sqrt div}
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks | Draw regular polygons on top of each other

Post by svend_tveskaeg »

I actually think that I begin to understand the RPN notation now!

However, when generating the hexagon+dodecagon, I encounter problems.

I calculated that \valK = (2 sqrt(3) - 3)*\valA.

Now for my code:

Code: Select all

\documentclass{article}
\usepackage{pst-eucl}

\begin{document}

\begin{figure}
\centering
% Length a.
\def\valA{1 }% Remember space before `}'.
% Length k.
\def\valK{\valA 3 sqrt 2 mul 3 sub mul }% Remember space before `}'.
 \psset{unit=5,offset=-12pt}
  \begin{pspicture}(\valA,\valA)
    \pstGeonode[CurveType=polygon,PointName=,PointSymbol=none]
      (0,0){P0}
      (\valA,0){P1}
      (!\valA dup add 30 cos mul 30 PtoC){P2}
      (!\valA dup add 30 cos mul 60 PtoC){P3}
      (!\valA dup add 30 cos mul 90 PtoC){P4}
      (\valA;120){P5}
    \multido{\iA=0+60,\iP=0+1}{6}{%
      \rput{\iA}(P\iP){%
        \pnode(!\valA \valK sub % a-k   = 2x
                 2 div          % a-k/2 =  x
                 0              % y value
              ){P\iP:1}
       \pnode(!\psGetNodeCenter{P\iP:1} \valA P\iP:1.x sub 0){P\iP:2} 
      } % end of \rput
    }   % end of \multido
    \pcline{|-|}(P3)(P4)     \ncput*{$a$}
    \pcline{|-|}(P2)(P2:1)   \ncput*{$x$}
    \pcline{|-|}(P2:1)(P2:2) \ncput*{$k$}
    \pcline{|-|}(P2:2)(P3)   \ncput*{$x$}
    \psset{linestyle=dashed}
    \multido{\iA=0+1,\iB=1+1}{5}{\psline(P\iA:2)(P\iB:1)}
    \psline(P5:2)(P0:1)
  \end{pspicture}
\end{figure}

\end{document}
It is probably some trivial mistake, but I cannot spot it.

I am not sure, but maybe the problem is the line

Code: Select all

(!\valA dup add 30 cos mul 90 PtoC){P4}
P.S. I generated the triangle+hexagon and square+octagon from your code without any problems (apart from some trivial mistakes from my side).
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks | Draw regular polygons on top of each other

Post by CrazyHorse »

svend_tveskaeg wrote:

Code: Select all

(!\valA dup add 30 cos mul 90 PtoC){P4}
P.S. I generated the triangle+hexagon and square+octagon from your code without any problems (apart from some trivial mistakes from my side).
the problem is

Code: Select all

      (!\valA 4 mul 60 cos mul 60 PtoC){P3}

the line from P0 to P3 is 4a*cos(60)
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks | Draw regular polygons on top of each other

Post by svend_tveskaeg »

Thank you.

I think that I have found the source of (some of?) my problems: I an not totally sure what dup in

Code: Select all

\valA dup add 30 cos mul
mean. Am I correct if I claim that

Code: Select all

\valA dup add 30 cos mul = \valA + \valA*cos(30)
? If I am, then why not write

Code: Select all

\valA 30 cos \valA mul add [= \valA + cos(30)*\valA]
instead of

Code: Select all

\valA dup add 30 cos mul
? The first notation is more intuitive to me.

Update:
Okay. It is not the same. :?

Any change I can get you to draw the hepragon+tetradecagon and the octagon+hexadecagon for me? I do not need these figures for anything, but maybe I can find a pattern in the code and then understand it.

I am especially interested in the hepragon+tetradecagon; since gcd(7,360) = 1, it is not possible to make any "shortcuts" regarding the code (as far as I can see).

Update 2:
Actually, the hepragon+tetradecagon should suffice. As mentioned, as long as gcd(#vertices,360) = 1 it should be enough.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks | Draw regular polygons on top of each other

Post by CrazyHorse »

svend_tveskaeg wrote:I think that I have found the source of (some of?) my problems: I an not totally sure what dup in

Code: Select all

\valA dup add 30 cos mul
mean. Am I correct if I claim that

Code: Select all

\valA dup add 30 cos mul = \valA + \valA*cos(30)
? If I am, then why not write

Code: Select all

\valA 30 cos \valA mul add [= \valA + cos(30)*\valA]
instead of

Code: Select all

\valA dup add 30 cos mul
? The first notation is more intuitive to me.

Update:
Okay. It is not the same. :?

Any change I can get you to draw the hepragon+tetradecagon and the octagon+hexadecagon for me? I do not need these figures for anything, but maybe I can find a pattern in the code and then understand it.

I am especially interested in the hepragon+tetradecagon; since gcd(7,360) = 1, it is not possible to make any "shortcuts" regarding the code (as far as I can see).

Update 2:
Actually, the hepragon+tetradecagon should suffice. As mentioned, as long as gcd(#vertices,360) = 1 it should be enough.
"dup" copies the upper element on the stack, eg:

3
4
60

maybe on stack (from top to bottom). Then dup gives

3
3
4
60

that is a bit faster as writing \valA \valA. But nowadays the speed of the machines are so fast that it makes no difference in using dup or two times the value/name. By the way: you can also copy 2 oe more values from the stack:

3
4
60

2 copy gives

3
4
3
4
60
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: PSTricks | Draw regular polygons on top of each other

Post by svend_tveskaeg »

I see. Thank you (once again) for the explanation.

I would very much appreciate it if your would help me one last time; with drawing a hepragon+tetradecagon. If I have misunderstood that

gcd(#vertices, 360) = 1 ==> no possible "shortcuts" in the code when defining the vertices P_0,...,P_{n-1}

then I do not need any further help in this thread.

P.S. I believe that I now understand the basics of RPN notation. :D (See the attached example (the comments are Danish).)
Attachments
RPN-notation.tex
RPN notation.
(2.02 KiB) Downloaded 352 times
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply