TransWikia.com

Can I use `regular polygon` wthout using `node`?

TeX - LaTeX Asked on June 11, 2021

For instance, I can execute this command:

filldraw[fill=blue,draw=black,opacity=0.3] (0,0) circle (2cm);

But I want something for regular polygons as well, something like this:

filldraw[fill=blue,draw=black,opacity=0.3] (0,0) regular polygon, regular polygon sides=6 (2cm);

Is such a command available, or should I first write newcommand and define a script to do this?

One Answer

I present two solutions, both have advantages and disadvantages :

  • the first one use pics and node, so you have to specify the actions (fill, draw...) separately from the path. For example you can use pic[draw]{poly=5:1cm} to draw a pentagon with inner radius of 1cm.
  • the second one use insert path and is more close to circle in a way that it insert simply the path corresponding to the polygon, and so use the path's actions. For example you can use [poly=5:1cm] to insert a pentagon with outer radius of 1cm.
documentclass[tikz,border=7pt]{standalone}
usetikzlibrary{shapes.geometric} % <- for regular polygon nodes
tikzset{
  pics/poly/.style args={#1:#2}{
    code = {
      node[regular polygon, regular polygon sides={#1}, pic actions,
        outer sep=0pt,inner sep={#2}] at (0,0){};
    }
  },
  poly/.style args={#1:#2}{
    insert path={
      +(0:#2) foreach ~ in {1,...,#1}{ -- +(~*360/#1:#2)} +(0,0)
    }
  },
}

begin{document}
  begin{tikzpicture}
    draw[blue, fill=yellow] (0,0) -- (1,1)
      [poly=7:1cm]
      -- (4,1)
      pic[fill=red, opacity=.5]{poly=7:1cm}
      -- (2,2)
    ;
  end{tikzpicture}
end{document}

enter image description here

Answered by Kpym on June 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP