Drawing concentric circles with alternating colors by means of foreach in TikZ
I would like to draw concentric circles like these:
by means of TikZ and foreach
command. The way I achieved it is very elementary (and burdensome):
documentclass[standalone]
usepackage{tikz}
begin{document}
begin{tikzpicture}[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
end{tikzpicture}
end{document}
To no avail I tried to apply foreach
, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.
tikz-pgf foreach loops
add a comment |
I would like to draw concentric circles like these:
by means of TikZ and foreach
command. The way I achieved it is very elementary (and burdensome):
documentclass[standalone]
usepackage{tikz}
begin{document}
begin{tikzpicture}[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
end{tikzpicture}
end{document}
To no avail I tried to apply foreach
, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.
tikz-pgf foreach loops
add a comment |
I would like to draw concentric circles like these:
by means of TikZ and foreach
command. The way I achieved it is very elementary (and burdensome):
documentclass[standalone]
usepackage{tikz}
begin{document}
begin{tikzpicture}[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
end{tikzpicture}
end{document}
To no avail I tried to apply foreach
, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.
tikz-pgf foreach loops
I would like to draw concentric circles like these:
by means of TikZ and foreach
command. The way I achieved it is very elementary (and burdensome):
documentclass[standalone]
usepackage{tikz}
begin{document}
begin{tikzpicture}[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
end{tikzpicture}
end{document}
To no avail I tried to apply foreach
, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.
tikz-pgf foreach loops
tikz-pgf foreach loops
asked 1 hour ago
Mad HatterMad Hatter
627520
627520
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You mean something like this:
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,3,5,7,9,11,13,15,17,19}
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
foreach x in {2,4,6,8,10,12,14,16,18, 20}
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
end{tikzpicture}
end{document}
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
1
Perfect. Thanks a lot!
– Mad Hatter
1 hour ago
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
52 mins ago
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
50 mins ago
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
49 mins ago
@Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).
– TeXnician
44 mins ago
add a comment |
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
{
static readonly SKPaint yellowStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
};
static readonly SKPaint redStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
};
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
{
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"{filename}.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
{
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
{
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
}
document.EndPage();
}
}
private static void Main()
{
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
{
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
p.Start();
}
}
}
2
Please don't radiate too much :) (+1)
– Raaja
42 mins ago
I am a SkiaSharp evangelist.
– God Must Be Crazy
41 mins ago
2
Nice alternative indeed.
– Raaja
39 mins ago
add a comment |
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,...,20}{
pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
draw[line width=8pt,currcol] (0,0) circle (x cm);}
end{tikzpicture}
end{document}
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
42 mins ago
1
@Raaja Yes, it is :)
– TeXnician
42 mins ago
add a comment |
A PSTricks solution.
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,2,...,10}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}
endpspicture
end{document}
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
foreach j in {1,...,10}{%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,...,j}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}%
endpspicture}
end{document}
1
You did not animate this.
– AlexG
15 mins ago
add a comment |
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,2,...,19}{
pgfmathsetmacromycolor{isodd{numexprx}?"red!70":"yellow!50"}%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
}%
end{tikzpicture}
end{document}
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%2f471465%2fdrawing-concentric-circles-with-alternating-colors-by-means-of-foreach-in-tikz%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You mean something like this:
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,3,5,7,9,11,13,15,17,19}
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
foreach x in {2,4,6,8,10,12,14,16,18, 20}
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
end{tikzpicture}
end{document}
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
1
Perfect. Thanks a lot!
– Mad Hatter
1 hour ago
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
52 mins ago
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
50 mins ago
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
49 mins ago
@Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).
– TeXnician
44 mins ago
add a comment |
You mean something like this:
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,3,5,7,9,11,13,15,17,19}
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
foreach x in {2,4,6,8,10,12,14,16,18, 20}
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
end{tikzpicture}
end{document}
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
1
Perfect. Thanks a lot!
– Mad Hatter
1 hour ago
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
52 mins ago
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
50 mins ago
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
49 mins ago
@Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).
– TeXnician
44 mins ago
add a comment |
You mean something like this:
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,3,5,7,9,11,13,15,17,19}
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
foreach x in {2,4,6,8,10,12,14,16,18, 20}
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
end{tikzpicture}
end{document}
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
You mean something like this:
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,3,5,7,9,11,13,15,17,19}
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
foreach x in {2,4,6,8,10,12,14,16,18, 20}
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
end{tikzpicture}
end{document}
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
answered 1 hour ago
RaajaRaaja
2,7652832
2,7652832
1
Perfect. Thanks a lot!
– Mad Hatter
1 hour ago
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
52 mins ago
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
50 mins ago
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
49 mins ago
@Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).
– TeXnician
44 mins ago
add a comment |
1
Perfect. Thanks a lot!
– Mad Hatter
1 hour ago
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
52 mins ago
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
50 mins ago
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
49 mins ago
@Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).
– TeXnician
44 mins ago
1
1
Perfect. Thanks a lot!
– Mad Hatter
1 hour ago
Perfect. Thanks a lot!
– Mad Hatter
1 hour ago
@MadHatter Also, this happens to be the first time I am using
for
loop in TeX
. So, thanks to you ;)– Raaja
52 mins ago
@MadHatter Also, this happens to be the first time I am using
for
loop in TeX
. So, thanks to you ;)– Raaja
52 mins ago
1
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
50 mins ago
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
50 mins ago
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
49 mins ago
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
49 mins ago
@Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).
– TeXnician
44 mins ago
@Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).
– TeXnician
44 mins ago
add a comment |
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
{
static readonly SKPaint yellowStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
};
static readonly SKPaint redStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
};
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
{
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"{filename}.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
{
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
{
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
}
document.EndPage();
}
}
private static void Main()
{
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
{
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
p.Start();
}
}
}
2
Please don't radiate too much :) (+1)
– Raaja
42 mins ago
I am a SkiaSharp evangelist.
– God Must Be Crazy
41 mins ago
2
Nice alternative indeed.
– Raaja
39 mins ago
add a comment |
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
{
static readonly SKPaint yellowStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
};
static readonly SKPaint redStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
};
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
{
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"{filename}.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
{
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
{
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
}
document.EndPage();
}
}
private static void Main()
{
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
{
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
p.Start();
}
}
}
2
Please don't radiate too much :) (+1)
– Raaja
42 mins ago
I am a SkiaSharp evangelist.
– God Must Be Crazy
41 mins ago
2
Nice alternative indeed.
– Raaja
39 mins ago
add a comment |
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
{
static readonly SKPaint yellowStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
};
static readonly SKPaint redStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
};
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
{
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"{filename}.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
{
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
{
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
}
document.EndPage();
}
}
private static void Main()
{
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
{
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
p.Start();
}
}
}
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
{
static readonly SKPaint yellowStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
};
static readonly SKPaint redStroke = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
};
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
{
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"{filename}.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
{
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
{
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
}
document.EndPage();
}
}
private static void Main()
{
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
{
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
p.Start();
}
}
}
answered 45 mins ago
God Must Be CrazyGod Must Be Crazy
5,15511039
5,15511039
2
Please don't radiate too much :) (+1)
– Raaja
42 mins ago
I am a SkiaSharp evangelist.
– God Must Be Crazy
41 mins ago
2
Nice alternative indeed.
– Raaja
39 mins ago
add a comment |
2
Please don't radiate too much :) (+1)
– Raaja
42 mins ago
I am a SkiaSharp evangelist.
– God Must Be Crazy
41 mins ago
2
Nice alternative indeed.
– Raaja
39 mins ago
2
2
Please don't radiate too much :) (+1)
– Raaja
42 mins ago
Please don't radiate too much :) (+1)
– Raaja
42 mins ago
I am a SkiaSharp evangelist.
– God Must Be Crazy
41 mins ago
I am a SkiaSharp evangelist.
– God Must Be Crazy
41 mins ago
2
2
Nice alternative indeed.
– Raaja
39 mins ago
Nice alternative indeed.
– Raaja
39 mins ago
add a comment |
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,...,20}{
pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
draw[line width=8pt,currcol] (0,0) circle (x cm);}
end{tikzpicture}
end{document}
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
42 mins ago
1
@Raaja Yes, it is :)
– TeXnician
42 mins ago
add a comment |
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,...,20}{
pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
draw[line width=8pt,currcol] (0,0) circle (x cm);}
end{tikzpicture}
end{document}
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
42 mins ago
1
@Raaja Yes, it is :)
– TeXnician
42 mins ago
add a comment |
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,...,20}{
pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
draw[line width=8pt,currcol] (0,0) circle (x cm);}
end{tikzpicture}
end{document}
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,...,20}{
pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
draw[line width=8pt,currcol] (0,0) circle (x cm);}
end{tikzpicture}
end{document}
edited 38 mins ago
answered 45 mins ago
TeXnicianTeXnician
24.8k63187
24.8k63187
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
42 mins ago
1
@Raaja Yes, it is :)
– TeXnician
42 mins ago
add a comment |
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
42 mins ago
1
@Raaja Yes, it is :)
– TeXnician
42 mins ago
Next time, I will know how to apply this
ifodd
;) [IMO, it is useful, if the loop counts grows].– Raaja
42 mins ago
Next time, I will know how to apply this
ifodd
;) [IMO, it is useful, if the loop counts grows].– Raaja
42 mins ago
1
1
@Raaja Yes, it is :)
– TeXnician
42 mins ago
@Raaja Yes, it is :)
– TeXnician
42 mins ago
add a comment |
A PSTricks solution.
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,2,...,10}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}
endpspicture
end{document}
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
foreach j in {1,...,10}{%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,...,j}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}%
endpspicture}
end{document}
1
You did not animate this.
– AlexG
15 mins ago
add a comment |
A PSTricks solution.
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,2,...,10}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}
endpspicture
end{document}
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
foreach j in {1,...,10}{%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,...,j}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}%
endpspicture}
end{document}
1
You did not animate this.
– AlexG
15 mins ago
add a comment |
A PSTricks solution.
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,2,...,10}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}
endpspicture
end{document}
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
foreach j in {1,...,10}{%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,...,j}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}%
endpspicture}
end{document}
A PSTricks solution.
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,2,...,10}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}
endpspicture
end{document}
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]{standalone}
psset{runit=4pt,unit=psrunit}
begin{document}
foreach j in {1,...,10}{%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in {1,...,j}{%
ifoddidefc{yellow}elsedefc{red}fi
pscircle[linecolor=c]{i}}%
endpspicture}
end{document}
edited 10 mins ago
answered 21 mins ago
God Must Be CrazyGod Must Be Crazy
5,15511039
5,15511039
1
You did not animate this.
– AlexG
15 mins ago
add a comment |
1
You did not animate this.
– AlexG
15 mins ago
1
1
You did not animate this.
– AlexG
15 mins ago
You did not animate this.
– AlexG
15 mins ago
add a comment |
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,2,...,19}{
pgfmathsetmacromycolor{isodd{numexprx}?"red!70":"yellow!50"}%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
}%
end{tikzpicture}
end{document}
add a comment |
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,2,...,19}{
pgfmathsetmacromycolor{isodd{numexprx}?"red!70":"yellow!50"}%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
}%
end{tikzpicture}
end{document}
add a comment |
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,2,...,19}{
pgfmathsetmacromycolor{isodd{numexprx}?"red!70":"yellow!50"}%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
}%
end{tikzpicture}
end{document}
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclass{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
foreach x in {1,2,...,19}{
pgfmathsetmacromycolor{isodd{numexprx}?"red!70":"yellow!50"}%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
}%
end{tikzpicture}
end{document}
answered 26 mins ago
sgmoyesgmoye
3,82811226
3,82811226
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%2f471465%2fdrawing-concentric-circles-with-alternating-colors-by-means-of-foreach-in-tikz%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