# How draw the trefoil curve with tikz

I need to draw with tikz a closed curve of the following shape,

which is kind of like a trefoil knot suppressed into one plane, but is also rotated. I was wondering if there could be any help or suggestion on that? Thanks in advance.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=0.7]
\draw[line width=0.3mm, -stealth] (0,-4)--(0,4);
\draw[line width=0.3mm, -stealth] (-4,0)--(4,0);
\draw[-stealth, domain=0:540, variable=\t,samples=200, line width=0.7mm]
plot ({-cos(\t)+2*cos(2*\t)}, {sin(\t)+2*sin(2*\t)});
\draw[fill=white, line width=0.5mm] (0,0) circle[radius=0.25 em];
\draw[fill=white, line width=0.5mm] (2,0) circle[radius=0.25 em];
\draw(-0.3,-0.1) node[below]{$0$};
\draw(2,0.1) node[above]{$1$};
\end{tikzpicture}
\end{document}

• Nice self-answer. Please edit it to make it complete, starting with \documentclass and ending with \end{document} as it should be. Commented Apr 17, 2022 at 18:12
• @SebGlav Re-edited~
– OnoL
Commented Apr 17, 2022 at 18:41
• 360 is enough. Commented Apr 17, 2022 at 20:15
\documentclass{standalone}
\usepackage{tkz-fct}

\begin{document}
\begin{tikzpicture}
\tkzInit[xmin=-4,xmax=4,ymin=-4,ymax=4,xstep=.2,ystep=.2]
\tkzFctPar[samples=400,domain=-pi:pi]{-cos(t)+2*cos(2*t)}{sin(t)+2*sin(2*t)}
\end{tikzpicture}
\end{document}


Here's two ways I did it but I think the second one is closer to what you're looking for.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\definecolor{sidecolor}{HTML}{E7E7E7}

\begin{document}

\begin{tikzpicture}[very thick]
\draw[gray!30] (-2,-2) grid (2,2);

\draw (1.3,0) .. controls (1,0.8) and (0,0.5) .. (-0.5,0);
\draw[cm={1,0,0,-1,(0,0)}] (1.3,0) .. controls (1,0.8) and (0,0.5) .. (-0.5,0);
\draw[rotate=120] (1.3,0) .. controls (1,0.8) and (0,0.5) .. (-0.5,0);
\draw[cm={1,0,0,-1,(0,0)},rotate=240] (1.3,0) .. controls (1,0.8) and (0,0.5) .. (-0.5,0);
\draw[rotate=240] (1.3,0) .. controls (1,0.8) and (0,0.5) .. (-0.5,0);
\draw[cm={1,0,0,-1,(0,0)},rotate=120] (1.3,0) .. controls (1,0.8) and (0,0.5) .. (-0.5,0);

\end{tikzpicture}

\begin{tikzpicture}[very thick]
\draw[gray!30] (-2,-2) grid (2,2);

\draw (1.3,0) to[out=90,in=60] (-0.5,0);
\draw[cm={1,0,0,-1,(0,0)}] (1.3,0) to[out=90,in=60] (-0.5,0);
\draw[rotate=120] (1.3,0) to[out=90,in=60] (-0.5,0);
\draw[cm={1,0,0,-1,(0,0)},rotate=240] (1.3,0) to[out=90,in=60] (-0.5,0);
\draw[rotate=240] (1.3,0) to[out=90,in=60] (-0.5,0);
\draw[cm={1,0,0,-1,(0,0)},rotate=120] (1.3,0) to[out=90,in=60] (-0.5,0);

\end{tikzpicture}

\end{document}


A PSTricks solution just for either fun or comparison purposes. Other trivial accessories are intentionally removed for the sake of simplicity.

\documentclass[pstricks]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-5,-5)(5,5)
\psaxes{->}(0,0)(-4.5,-4.5)(4.5,4.5)[$x$,0][$y$,180]
\curvepnodes[plotpoints=100]{0}{360 DegtoRad}{-cos(t) + 2*cos(2*t) | sin(t) + 2*sin(2*t) }{A}
\psnpolygon[linecolor=red](0,\numexpr\Anodecount-1){A}
\end{pspicture}
\end{document}


A SkiaSharp solution just for fun! We can compile the following with \immediate\write18 to make it more related to TeX.

using CSharpMath.SkiaSharp;
using SkiaSharp;
using System.Diagnostics;
using static System.MathF;

class Diagram
{
const float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm

static float PtToCm(float pt) => pt / scale;

static readonly SKPaint strokeBlack = new SKPaint
{
Style = SKPaintStyle.Stroke,
StrokeWidth = PtToCm(0.8f),
Color = SKColors.Black,
StrokeCap = SKStrokeCap.Round,
IsAntialias = true
};

static readonly SKPaint strokeRed = new SKPaint
{
Style = SKPaintStyle.Stroke,
StrokeWidth = PtToCm(0.8f),
Color = SKColors.Red,
StrokeCap = SKStrokeCap.Round,
IsAntialias = true
};

static readonly SKPaint fillGreen = new SKPaint
{
Style = SKPaintStyle.Fill,
StrokeWidth = PtToCm(0.8f),
Color = SKColors.Green,
StrokeCap = SKStrokeCap.Round,
IsAntialias = true
};

static readonly SKRect domain = new SKRect(-5f, 5f, 5f, -5f);

static readonly float width = domain.Width * scale;
static readonly float height = -domain.Height * scale;

public static void Generate(string filename)
{
using (var stream = new SKFileWStream(\$"{filename}.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
{
canvas.Scale(scale, -scale);
canvas.Translate(-domain.Left, -domain.Top);

canvas.DrawLine(domain.Left + 0.5f, 0, domain.Right - 0.5f, 0, strokeBlack);
canvas.DrawLine(0, domain.Bottom + 0.5f, 0, domain.Top - 0.5f, strokeBlack);

int N = 100;
int start = 0; // degrees
int stop = 360; // degrees
float d = (stop - start) * PI / (180 * N);

SKPoint[] pts = Enumerable
.Range(0, N)
.Select(i => new SKPoint
{
X = -Cos(i * d) + 2 * Cos(2 * i * d),
Y = Sin(i * d) + 2 * Sin(2 * i * d)
})
.ToArray();

for (int i = 0; i < N; ++i)
canvas.DrawLine(pts[i], pts[(i + 1) % N], strokeRed);

canvas.DrawCircle(1, 0, PtToCm(2), fillGreen);

MathPainterExtension.Painter.FontSize = PtToCm(12);
canvas.Draw("1", 1.3f, PtToCm(10));

MathPainterExtension.Painter.FontSize = PtToCm(8);
canvas.Draw(@"\begin{cases} x(t) = -\cos t + 2 \cos (2t) \\ y(t) = \sin t + 2 \sin (2t) \end{cases}", 2.5f, 4);
document.EndPage();
}
}


• This feels a little bit far from TeX. Can you edit your post to explain how this relates to TeX? Commented Apr 17, 2022 at 21:04