Colorize sides regular 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
add a comment |
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 '18 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 '18 at 23:04
add a comment |
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 '18 at 22:59
Antonin DécimoAntonin Décimo
534
534
Do you really want to use a node?
– Sigur
Nov 21 '18 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 '18 at 23:04
add a comment |
Do you really want to use a node?
– Sigur
Nov 21 '18 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 '18 at 23:04
Do you really want to use a node?
– Sigur
Nov 21 '18 at 23:02
Do you really want to use a node?
– Sigur
Nov 21 '18 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 '18 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 '18 at 23:04
add a comment |
3 Answers
3
active
oldest
votes
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 |
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 |
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 |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 8:55
IgnasiIgnasi
93.3k4167310
93.3k4167310
add a comment |
add a comment |
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 |
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 |
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 '18 at 23:37
answered Nov 21 '18 at 23:08
SigurSigur
24.8k356139
24.8k356139
add a comment |
add a comment |
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 |
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 |
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 '18 at 23:27
marmotmarmot
100k4115222
100k4115222
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.
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 '18 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 '18 at 23:04