Colorize sides regular polygon
up vote
5
down vote
favorite
To illustrate what edge-coloring is, I'd like to colorize a regular polygon. I want to generate the shape using TikZ, then colorize the sides (edges) of the polygon separately, for instance by doing something like this:
draw[red] (polygon.side 1);
Unfortunately, the world does not seem to work like this. For now my ugly workaround is to overdraw using dotted, dashed, and thick lines.
documentclass{standalone}
usepackage{tikz}
usetikzlibrary{shapes.geometric,positioning}
begin{document}
begin{tikzpicture}
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
draw[thick] (polygon.corner 1) -- (polygon.corner 2);
draw[dashed] (polygon.corner 2) -- (polygon.corner 3);
draw[thick] (polygon.corner 4) -- (polygon.corner 5);
draw[dashed] (polygon.corner 5) -- (polygon.corner 1);
end{tikzpicture}
end{document}
Thanks!
tikz-pgf graphs tikz-shape polygon
add a comment |
up vote
5
down vote
favorite
To illustrate what edge-coloring is, I'd like to colorize a regular polygon. I want to generate the shape using TikZ, then colorize the sides (edges) of the polygon separately, for instance by doing something like this:
draw[red] (polygon.side 1);
Unfortunately, the world does not seem to work like this. For now my ugly workaround is to overdraw using dotted, dashed, and thick lines.
documentclass{standalone}
usepackage{tikz}
usetikzlibrary{shapes.geometric,positioning}
begin{document}
begin{tikzpicture}
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
draw[thick] (polygon.corner 1) -- (polygon.corner 2);
draw[dashed] (polygon.corner 2) -- (polygon.corner 3);
draw[thick] (polygon.corner 4) -- (polygon.corner 5);
draw[dashed] (polygon.corner 5) -- (polygon.corner 1);
end{tikzpicture}
end{document}
Thanks!
tikz-pgf graphs tikz-shape polygon
Do you really want to use a node?
– Sigur
Nov 21 at 23:02
I want the simplest thing possible. I may have driven into the wrong direction. Also, why wouldn't I want a node?
– Antonin Décimo
Nov 21 at 23:04
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
To illustrate what edge-coloring is, I'd like to colorize a regular polygon. I want to generate the shape using TikZ, then colorize the sides (edges) of the polygon separately, for instance by doing something like this:
draw[red] (polygon.side 1);
Unfortunately, the world does not seem to work like this. For now my ugly workaround is to overdraw using dotted, dashed, and thick lines.
documentclass{standalone}
usepackage{tikz}
usetikzlibrary{shapes.geometric,positioning}
begin{document}
begin{tikzpicture}
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
draw[thick] (polygon.corner 1) -- (polygon.corner 2);
draw[dashed] (polygon.corner 2) -- (polygon.corner 3);
draw[thick] (polygon.corner 4) -- (polygon.corner 5);
draw[dashed] (polygon.corner 5) -- (polygon.corner 1);
end{tikzpicture}
end{document}
Thanks!
tikz-pgf graphs tikz-shape polygon
To illustrate what edge-coloring is, I'd like to colorize a regular polygon. I want to generate the shape using TikZ, then colorize the sides (edges) of the polygon separately, for instance by doing something like this:
draw[red] (polygon.side 1);
Unfortunately, the world does not seem to work like this. For now my ugly workaround is to overdraw using dotted, dashed, and thick lines.
documentclass{standalone}
usepackage{tikz}
usetikzlibrary{shapes.geometric,positioning}
begin{document}
begin{tikzpicture}
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
draw[thick] (polygon.corner 1) -- (polygon.corner 2);
draw[dashed] (polygon.corner 2) -- (polygon.corner 3);
draw[thick] (polygon.corner 4) -- (polygon.corner 5);
draw[dashed] (polygon.corner 5) -- (polygon.corner 1);
end{tikzpicture}
end{document}
Thanks!
tikz-pgf graphs tikz-shape polygon
tikz-pgf graphs tikz-shape polygon
asked Nov 21 at 22:59
Antonin Décimo
534
534
Do you really want to use a node?
– Sigur
Nov 21 at 23:02
I want the simplest thing possible. I may have driven into the wrong direction. Also, why wouldn't I want a node?
– Antonin Décimo
Nov 21 at 23:04
add a comment |
Do you really want to use a node?
– Sigur
Nov 21 at 23:02
I want the simplest thing possible. I may have driven into the wrong direction. Also, why wouldn't I want a node?
– Antonin Décimo
Nov 21 at 23:04
Do you really want to use a node?
– Sigur
Nov 21 at 23:02
Do you really want to use a node?
– Sigur
Nov 21 at 23:02
I want the simplest thing possible. I may have driven into the wrong direction. Also, why wouldn't I want a node?
– Antonin Décimo
Nov 21 at 23:04
I want the simplest thing possible. I may have driven into the wrong direction. Also, why wouldn't I want a node?
– Antonin Décimo
Nov 21 at 23:04
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
With regular polygon colors
option, there's no need for extra commands to draw the sides. Everything is done within the node command.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric, positioning}
begin{document}
begin{tikzpicture}[
regular polygon colors/.style 2 args={
append after command={%
pgfextra
foreach i [count=ni, remember=ni as lasti (initially #1)] in {#2}{
draw[i] (tikzlastnode.corner lasti) --(tikzlastnode.corner ni);}
endpgfextra
}
},
]
node[minimum size=2cm, regular polygon,
regular polygon sides=5,
regular polygon colors={5}{red,blue,orange,green,cyan}] (polygon) {};
node[minimum size=2cm, regular polygon,
regular polygon sides=7,
regular polygon colors={7}{red, blue, orange, green, cyan, violet, brown},
right=of polygon] (polygon2) {};
end{tikzpicture}
end{document}
add a comment |
up vote
5
down vote
Like this?
documentclass[margin=1mm]{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
draw[c] (i:1cm) -- (72+i:1cm);
}
end{tikzpicture}
end{document}
Edit 1: Another option, to have a better join line:
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c] (i:1cm) -- (72+i:1cm) -- (0,0) -- cycle;
}
fill[white] (90:.95cm)--(162:.95cm)--(234:.95cm)--(306:.95cm)--(378:.95cm)--cycle;
end{tikzpicture}
Edit 2: just for fun.
begin{tikzpicture}
foreach r in {1,.9,...,.1}{
pgfmathparse{30 * r}%
pgfmathsetmacropercent{pgfmathresult}
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c!percent] (i:r cm) -- (72+i:r cm) -- (0,0) -- cycle;
}}
end{tikzpicture}
add a comment |
up vote
3
down vote
Here is a proposal that defines a style polygon side
which can be used e.g. like draw[red,polygon side=2];
to draw the second side red.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric}
begin{document}
begin{tikzpicture}[get next vertex/.code n args={2}{%
pgfmathparse{int(1+mod(#1,5))}
xdef#2{pgfmathresult}},
polygon side/.style={get next vertex={#1}{tmpnextn},
insert path={(polygon.corner #1)-- (polygon.corner tmpnextn)}}
]
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
foreach X [count=Y] in {red,blue,orange,green,cyan}
{draw[X,polygon side=Y];}
end{tikzpicture}
end{document}
Note that as of now the number of corners (or sides) as well as the name of the object (or node), polygon
, are hard coded. One could of course use pgfkeys to store them in some keys, and if you ask me to I'll be happy to add this to this answer.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With regular polygon colors
option, there's no need for extra commands to draw the sides. Everything is done within the node command.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric, positioning}
begin{document}
begin{tikzpicture}[
regular polygon colors/.style 2 args={
append after command={%
pgfextra
foreach i [count=ni, remember=ni as lasti (initially #1)] in {#2}{
draw[i] (tikzlastnode.corner lasti) --(tikzlastnode.corner ni);}
endpgfextra
}
},
]
node[minimum size=2cm, regular polygon,
regular polygon sides=5,
regular polygon colors={5}{red,blue,orange,green,cyan}] (polygon) {};
node[minimum size=2cm, regular polygon,
regular polygon sides=7,
regular polygon colors={7}{red, blue, orange, green, cyan, violet, brown},
right=of polygon] (polygon2) {};
end{tikzpicture}
end{document}
add a comment |
up vote
3
down vote
accepted
With regular polygon colors
option, there's no need for extra commands to draw the sides. Everything is done within the node command.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric, positioning}
begin{document}
begin{tikzpicture}[
regular polygon colors/.style 2 args={
append after command={%
pgfextra
foreach i [count=ni, remember=ni as lasti (initially #1)] in {#2}{
draw[i] (tikzlastnode.corner lasti) --(tikzlastnode.corner ni);}
endpgfextra
}
},
]
node[minimum size=2cm, regular polygon,
regular polygon sides=5,
regular polygon colors={5}{red,blue,orange,green,cyan}] (polygon) {};
node[minimum size=2cm, regular polygon,
regular polygon sides=7,
regular polygon colors={7}{red, blue, orange, green, cyan, violet, brown},
right=of polygon] (polygon2) {};
end{tikzpicture}
end{document}
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With regular polygon colors
option, there's no need for extra commands to draw the sides. Everything is done within the node command.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric, positioning}
begin{document}
begin{tikzpicture}[
regular polygon colors/.style 2 args={
append after command={%
pgfextra
foreach i [count=ni, remember=ni as lasti (initially #1)] in {#2}{
draw[i] (tikzlastnode.corner lasti) --(tikzlastnode.corner ni);}
endpgfextra
}
},
]
node[minimum size=2cm, regular polygon,
regular polygon sides=5,
regular polygon colors={5}{red,blue,orange,green,cyan}] (polygon) {};
node[minimum size=2cm, regular polygon,
regular polygon sides=7,
regular polygon colors={7}{red, blue, orange, green, cyan, violet, brown},
right=of polygon] (polygon2) {};
end{tikzpicture}
end{document}
With regular polygon colors
option, there's no need for extra commands to draw the sides. Everything is done within the node command.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric, positioning}
begin{document}
begin{tikzpicture}[
regular polygon colors/.style 2 args={
append after command={%
pgfextra
foreach i [count=ni, remember=ni as lasti (initially #1)] in {#2}{
draw[i] (tikzlastnode.corner lasti) --(tikzlastnode.corner ni);}
endpgfextra
}
},
]
node[minimum size=2cm, regular polygon,
regular polygon sides=5,
regular polygon colors={5}{red,blue,orange,green,cyan}] (polygon) {};
node[minimum size=2cm, regular polygon,
regular polygon sides=7,
regular polygon colors={7}{red, blue, orange, green, cyan, violet, brown},
right=of polygon] (polygon2) {};
end{tikzpicture}
end{document}
answered Nov 22 at 8:55
Ignasi
90.6k4164303
90.6k4164303
add a comment |
add a comment |
up vote
5
down vote
Like this?
documentclass[margin=1mm]{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
draw[c] (i:1cm) -- (72+i:1cm);
}
end{tikzpicture}
end{document}
Edit 1: Another option, to have a better join line:
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c] (i:1cm) -- (72+i:1cm) -- (0,0) -- cycle;
}
fill[white] (90:.95cm)--(162:.95cm)--(234:.95cm)--(306:.95cm)--(378:.95cm)--cycle;
end{tikzpicture}
Edit 2: just for fun.
begin{tikzpicture}
foreach r in {1,.9,...,.1}{
pgfmathparse{30 * r}%
pgfmathsetmacropercent{pgfmathresult}
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c!percent] (i:r cm) -- (72+i:r cm) -- (0,0) -- cycle;
}}
end{tikzpicture}
add a comment |
up vote
5
down vote
Like this?
documentclass[margin=1mm]{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
draw[c] (i:1cm) -- (72+i:1cm);
}
end{tikzpicture}
end{document}
Edit 1: Another option, to have a better join line:
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c] (i:1cm) -- (72+i:1cm) -- (0,0) -- cycle;
}
fill[white] (90:.95cm)--(162:.95cm)--(234:.95cm)--(306:.95cm)--(378:.95cm)--cycle;
end{tikzpicture}
Edit 2: just for fun.
begin{tikzpicture}
foreach r in {1,.9,...,.1}{
pgfmathparse{30 * r}%
pgfmathsetmacropercent{pgfmathresult}
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c!percent] (i:r cm) -- (72+i:r cm) -- (0,0) -- cycle;
}}
end{tikzpicture}
add a comment |
up vote
5
down vote
up vote
5
down vote
Like this?
documentclass[margin=1mm]{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
draw[c] (i:1cm) -- (72+i:1cm);
}
end{tikzpicture}
end{document}
Edit 1: Another option, to have a better join line:
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c] (i:1cm) -- (72+i:1cm) -- (0,0) -- cycle;
}
fill[white] (90:.95cm)--(162:.95cm)--(234:.95cm)--(306:.95cm)--(378:.95cm)--cycle;
end{tikzpicture}
Edit 2: just for fun.
begin{tikzpicture}
foreach r in {1,.9,...,.1}{
pgfmathparse{30 * r}%
pgfmathsetmacropercent{pgfmathresult}
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c!percent] (i:r cm) -- (72+i:r cm) -- (0,0) -- cycle;
}}
end{tikzpicture}
Like this?
documentclass[margin=1mm]{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
draw[c] (i:1cm) -- (72+i:1cm);
}
end{tikzpicture}
end{document}
Edit 1: Another option, to have a better join line:
begin{tikzpicture}[thick]
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c] (i:1cm) -- (72+i:1cm) -- (0,0) -- cycle;
}
fill[white] (90:.95cm)--(162:.95cm)--(234:.95cm)--(306:.95cm)--(378:.95cm)--cycle;
end{tikzpicture}
Edit 2: just for fun.
begin{tikzpicture}
foreach r in {1,.9,...,.1}{
pgfmathparse{30 * r}%
pgfmathsetmacropercent{pgfmathresult}
foreach i/c in {90/red,162/blue,234/green,306/pink,378/orange}{
fill[c!percent] (i:r cm) -- (72+i:r cm) -- (0,0) -- cycle;
}}
end{tikzpicture}
edited Nov 21 at 23:37
answered Nov 21 at 23:08
Sigur
23.4k354135
23.4k354135
add a comment |
add a comment |
up vote
3
down vote
Here is a proposal that defines a style polygon side
which can be used e.g. like draw[red,polygon side=2];
to draw the second side red.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric}
begin{document}
begin{tikzpicture}[get next vertex/.code n args={2}{%
pgfmathparse{int(1+mod(#1,5))}
xdef#2{pgfmathresult}},
polygon side/.style={get next vertex={#1}{tmpnextn},
insert path={(polygon.corner #1)-- (polygon.corner tmpnextn)}}
]
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
foreach X [count=Y] in {red,blue,orange,green,cyan}
{draw[X,polygon side=Y];}
end{tikzpicture}
end{document}
Note that as of now the number of corners (or sides) as well as the name of the object (or node), polygon
, are hard coded. One could of course use pgfkeys to store them in some keys, and if you ask me to I'll be happy to add this to this answer.
add a comment |
up vote
3
down vote
Here is a proposal that defines a style polygon side
which can be used e.g. like draw[red,polygon side=2];
to draw the second side red.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric}
begin{document}
begin{tikzpicture}[get next vertex/.code n args={2}{%
pgfmathparse{int(1+mod(#1,5))}
xdef#2{pgfmathresult}},
polygon side/.style={get next vertex={#1}{tmpnextn},
insert path={(polygon.corner #1)-- (polygon.corner tmpnextn)}}
]
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
foreach X [count=Y] in {red,blue,orange,green,cyan}
{draw[X,polygon side=Y];}
end{tikzpicture}
end{document}
Note that as of now the number of corners (or sides) as well as the name of the object (or node), polygon
, are hard coded. One could of course use pgfkeys to store them in some keys, and if you ask me to I'll be happy to add this to this answer.
add a comment |
up vote
3
down vote
up vote
3
down vote
Here is a proposal that defines a style polygon side
which can be used e.g. like draw[red,polygon side=2];
to draw the second side red.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric}
begin{document}
begin{tikzpicture}[get next vertex/.code n args={2}{%
pgfmathparse{int(1+mod(#1,5))}
xdef#2{pgfmathresult}},
polygon side/.style={get next vertex={#1}{tmpnextn},
insert path={(polygon.corner #1)-- (polygon.corner tmpnextn)}}
]
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
foreach X [count=Y] in {red,blue,orange,green,cyan}
{draw[X,polygon side=Y];}
end{tikzpicture}
end{document}
Note that as of now the number of corners (or sides) as well as the name of the object (or node), polygon
, are hard coded. One could of course use pgfkeys to store them in some keys, and if you ask me to I'll be happy to add this to this answer.
Here is a proposal that defines a style polygon side
which can be used e.g. like draw[red,polygon side=2];
to draw the second side red.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{shapes.geometric}
begin{document}
begin{tikzpicture}[get next vertex/.code n args={2}{%
pgfmathparse{int(1+mod(#1,5))}
xdef#2{pgfmathresult}},
polygon side/.style={get next vertex={#1}{tmpnextn},
insert path={(polygon.corner #1)-- (polygon.corner tmpnextn)}}
]
node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
foreach X [count=Y] in {red,blue,orange,green,cyan}
{draw[X,polygon side=Y];}
end{tikzpicture}
end{document}
Note that as of now the number of corners (or sides) as well as the name of the object (or node), polygon
, are hard coded. One could of course use pgfkeys to store them in some keys, and if you ask me to I'll be happy to add this to this answer.
answered Nov 21 at 23:27
marmot
80.2k491172
80.2k491172
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f461207%2fcolorize-sides-regular-polygon%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you really want to use a node?
– Sigur
Nov 21 at 23:02
I want the simplest thing possible. I may have driven into the wrong direction. Also, why wouldn't I want a node?
– Antonin Décimo
Nov 21 at 23:04