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

Information and discussion about graphics, figures & tables in LaTeX documents.
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 »

It all.

Consider the following MWE:

Code: Select all

\documentclass{article}
\usepackage{auto-pst-pdf,pstricks-add}

\begin{document}

\begin{figure}
 \centering
  \begin{pspicture}(4.37,3.56)
   \psline[linewidth=0.7pt](0.2,0)(2.2,3.46)(4.2,0)(0.2,0)
   \psline[linewidth=0.7pt,linestyle=dashed,dash=2pt](1.53,0)(0.87,1.15)
   \psline[linewidth=0.7pt,linestyle=dashed,dash=2pt](1.53,2.31)(2.87,2.31)
   \psline[linewidth=0.7pt,linestyle=dashed,dash=2pt](3.53,1.15)(2.87,0)
   \psline[linewidth=0.5pt]{|<->|}(0,0.12)(2.03,3.56)
   \rput(0.83,1.9){$a$}
   \psline[linewidth=0.5pt]{|<->|}(3.04,2.41)(2.37,3.56)
   \rput(2.9,3.05){$x$}
   \psline[linewidth=0.5pt]{|<->|}(3.71,1.25)(3.04,2.41)
   \rput(3.6,1.92){$k$}
   \psline[linewidth=0.5pt]{|<->|}(4.37,0.1)(3.71,1.25)
   \rput(4.25,0.75){$x$}
  \end{pspicture}
\end{figure}

\end{document}
How do I create this figure using the packages pst-poly and pst-node (and/or whatever is needed to draw this as easy as possible)?

Thank you in advance!

P.S. The auto-pst-pdf package is because I compile using PDFLaTeX.

Update:
Here is an improved version:

Code: Select all

\documentclass{article}
\usepackage{auto-pst-pdf,pstricks-add}

\begin{document}

\begin{figure}[htbp]
 \centering
  \begin{pspicture}(0.2,0)(4.37,3.46)
  \psset{unit=1.5,offset=12pt}
   \pnode(0.2,0){A}
   \pnode(2.2,3.46){B}
   \pnode(4.2,0){C}
   \pspolygon(A)(B)(C)
   \pnode(1.53,0){D}
   \pnode(0.87,1.15){E}
   \pnode(1.53,2.31){F}
   \pnode(2.87,2.31){G}
   \pnode(3.53,1.15){H}
   \pnode(2.87,0){I}
   \pspolygon[linestyle=dashed,linewidth=0.5pt](D)(E)(F)(G)(H)(I)
   \pcline{|-|}(A)(B)
   \ncput*{$a$}
   \pcline{|-|}(B)(G)
   \ncput*{$x$}
   \pcline{|-|}(G)(H)
   \ncput*{$k$}
   \pcline{|-|}(H)(C)
   \ncput*{$k$}
  \end{pspicture}
\end{figure}

\end{document}
I would like to improve the code even further: If I do not have to calculate the coordinates of the nodes A--I by hand, it would be nice.

(In the figure, $x = k$. I am not sure if it makes a difference.)
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

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

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 would like to improve the code even further: If I do not have to calculate the coordinates of the nodes A--I by hand, it would be nice.

(In the figure, $x = k$. I am not sure if it makes a difference.)
I do not know if I really understand. However, here a solution, where you can specify the value for k and the rest will be done automatically!

Code: Select all

\documentclass{article}
\usepackage{auto-pst-pdf,pst-eucl}

\begin{document}

\def\valK{1.37 }% space at the end!

  \begin{pspicture}(0.2,0)(4.37,3.46)
  \psset{unit=1.5,offset=12pt}
  \pstGeonode[CurveType=polygon,PointName=,PointSymbol=none](0.2,0){A}(2.2,3.46){B}(4.2,0){C}
   \pnode[C](\valK;180){I}\pnode[C](\valK;120){H}
   \pnode[C](!\valK dup add 180 PtoC){G}\pnode[C](!\valK dup add 120 PtoC){F}
   \pcline{|-|}(A)(B)\ncput*[nrot=:U]{$a$}
   \pcline{|-|}(B)(F)\ncput*[nrot=:U]{$x$}
   \pcline{|-|}(F)(H)\ncput*[nrot=:U]{$k$}
   \pcline{|-|}(H)(C)\ncput*[nrot=:U]{$k$}
   \psset{linestyle=dashed}
   \psline(I)(H)
   \psline(F)(! \psGetNodeCenter{B}\psGetNodeCenter{F} B.x dup add F.x sub F.y)
   \psline(G)(!\psGetNodeCenter{A}
               \psGetNodeCenter{G} 
               G.x A.x sub 60 PtoC A.y add exch A.x add exch )
    \end{pspicture}

\end{document}
Ans her another solution which uses only two definitions A and K:

Code: Select all

\documentclass{article}
\usepackage{auto-pst-pdf,pst-eucl}

\begin{document}

\def\valA{4 }% space at the end!
\def\valK{1.37 }% 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 2 div 3 sqrt 2 div \valA mul){B}(\valA,0){C}
   \pnode[C](\valK;180){I}\pnode[C](\valK;120){H}
   \pnode[C](!\valK dup add 180 PtoC){G}\pnode[C](!\valK dup add 120 PtoC){F}
   \pcline{|-|}(A)(B)\ncput*[nrot=:U]{$a$}
   \pcline{|-|}(B)(F)\ncput*[nrot=:U]{$x$}
   \pcline{|-|}(F)(H)\ncput*[nrot=:U]{$k$}
   \pcline{|-|}(H)(C)\ncput*[nrot=:U]{$k$}
   \psset{linestyle=dashed}
   \psline(I)(H)
   \psline(F)(! \psGetNodeCenter{B}\psGetNodeCenter{F} B.x dup add F.x sub F.y)
   \psline(G)(!\psGetNodeCenter{A}
               \psGetNodeCenter{G} 
               G.x A.x sub 60 PtoC A.y add exch A.x add exch )
\end{pspicture}

\end{document}
Attachments
xx.png
xx.png (5.28 KiB) Viewed 14047 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 »

CrazyHorse wrote:I do not know if I really understand.
Neither would I if I others wrote the initial post. :D

All I tried to say was that the length of the line segments marked with a k on the figure is the same length as the line segment marked with an x.

Your solution is (or course) very elegent. I will definitely use is.

Any change I can make you use algebraic notation instead of RPN notation in your two examples? (I have never understood the latter notation and therefore do not know how to modify the example---I would like to use your construction in other figures.)

Thank you in advance!
``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:
CrazyHorse wrote:I do not know if I really understand.
Neither would I if I others wrote the initial post. :D

All I tried to say was that the length of the line segments marked with a k on the figure is the same length as the line segment marked with an x.

Your solution is (or course) very elegent. I will definitely use is.

Any change I can make you use algebraic notation instead of RPN notation in your two examples? (I have never understood the latter notation and therefore do not know how to modify the example---I would like to use your construction in other figures.)
algebraic notation is not possible in all cases. However, the postfix notation is really simple. You have one stack and can put numbers, functions, arrays. ... on this stack. For example "4 3 add" means add the upper two values on the stack and put the sum instaed on the stack. "PtoC" is an internal PSTricks function which converts the upper two elements on the stack into x y values: "5 90 PtoC" takes 5 for the radius 90 for the angle from the stack and puts "0 5" instead on the stack, 0 for x and 5 for y
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 »

Okay. Thank you for the explanation, Herbert.

Please take a look at the attached image.

Have I understood the notation correct?

P.S. I will write the other three expressions in RPN notation as in the image later tonight for verification (if I have understood the notation at all ;) ).

Update:
I know this forum is not for questions like my last one, but I hope it is acceptable (in this particular thread) anyway.
Attachments
How to understand the RPN notation.
How to understand the RPN notation.
expression.png (4.94 KiB) Viewed 14028 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:Okay. Thank you for the explanation, Herbert.

Please take a look at the attached image.

Have I understood the notation correct?

P.S. I will write the other three expressions in RPN notation as in the image later tonight for verification (if I have understood the notation at all ;) ).

Update:
I know this forum is not for questions like my last one, but I hope it is acceptable (in this particular thread) anyway.
only a little mistake, but well done:

Code: Select all

\valA dup mul    = \valA^2
3 sqrt 2 div     = sqrt(3)/2
div              = \valA^2 / sqrt(3)/2

\valA dup mul 3 sqrt 2 div div 
it is no problem to have two div operators in series. It takes the last two elements from the stack and divedes them and puts the result back on top:

Code: Select all

1 2 3 4 5 div div div div 
is possible and the same as 1/2/3/4/5

Important for definition with \def is the space at the end: \def\A{1 } and not
\def\A{1}. Without the space something like \A \A add will be on PostScript side as 11add (without any space, which are "eaten" by TeX).
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, once again.

I believe I understand why

Code: Select all

\valA dup mul    = \valA^2
3 sqrt 2 div     = sqrt(3)/2
div              = \valA^2 / sqrt(3)/2

\valA dup mul 3 sqrt 2 div div
but that was not what you wrote in the second example here. You wrote

Code: Select all

\valA 2 div 3 sqrt 2 div \valA mul
and not

Code: Select all

\valA dup mul 3 sqrt 2 div div
I do not understand why I have made `a little mistake', i.e., why the expression in the picture is not correct. :? I agree that the expression you wrote in your latest answer is more elegant than the original one in your example, but why is my interpretation of the expression wrong?

P.S. After a clarification of the my problem, I will try to `convert' the other three expressions.
Last edited by svend_tveskaeg on Sat Sep 29, 2012 11:15 pm, edited 2 times in total.
``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:Thank you, once again.

I believe I understand why

Code: Select all

\valA dup mul    = \valA^2
3 sqrt 2 div     = sqrt(3)/2
div              = \valA^2 / sqrt(3)/2

\valA dup mul 3 sqrt 2 div div
but that was not what you wrote in the second example here. You wrote

Code: Select all

\valA 2 div 3 sqrt 2 div \valA mul
and not
ah, I see. A misunderstanding. In my example are two values, x and y
x= \valA/2
y= \valA/sqrt(3)/2

And I didn't realized that your expression refers to this.
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 »

Nice, that I am not that retarded. ;)

In the attached picture expression2a.png, have I interpreted the first three expressions correct?

Regarding the last expression, I do not know what exch does. I read that it has something to do with exchanging elements at the top of a stack, but I do not understand that. :oops:

If I could get an explanation of the last expression and get it written as `an algebraic' expression, I would much appreciate that.

P.S. In expression2b.png is your reformulation of the first expression. (It is just a reminder to myself.)
Attachments
How to understand RPN notation, 1.
How to understand RPN notation, 1.
expression2a.png (27.06 KiB) Viewed 14008 times
How to understand RPN notation, 2.
How to understand RPN notation, 2.
expression2b.png (6.49 KiB) Viewed 14008 times
Last edited by svend_tveskaeg on Sun Sep 30, 2012 3:16 am, edited 1 time in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
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 »

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.
Attachments
Square.
Square.
square.png (8.23 KiB) Viewed 14006 times
Pentagon.
Pentagon.
pentagon.png (17.02 KiB) Viewed 14006 times
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply