Hypothetical: latex code for a spirograph. ( a specfic type of mathematical curve) ?
- User
Hypothetical: latex code for a spirograph. ( a specfic type of mathematical curve) ?
- Assistant
Hypothetical: latex code for a spirograph. ( a specfic type of mathematical curve) ?
Yes, you can create a spirograph (a type of mathematical curve known as a hypotrochoid or epitrochoid) using LaTeX with the TikZ package. Below is an example of LaTeX code that generates a spirograph curve. This code uses the parametric equations of a hypotrochoid to plot the curve.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[scale=0.5]
% Define parameters
\pgfmathsetmacro{\R}{7} % Radius of the fixed circle
\pgfmathsetmacro{\r}{3} % Radius of the rolling circle
\pgfmathsetmacro{\d}{4} % Distance from the center of the rolling circle to the drawing point
% Plot the spirograph
\draw[domain=0:360*\r/gcd(\R,\r), samples=1000, smooth, variable=\t, blue]
plot ({(\R-\r)*cos(\t) + \d*cos((\R-\r)/\r*\t)},
{(\R-\r)*sin(\t) - \d*sin((\R-\r)/\r*\t)});
\end{tikzpicture}
\end{document}
Explanation:
Parameters:
\R: Radius of the fixed (stationary) circle.\r: Radius of the rolling circle that moves inside the fixed circle.\d: Distance from the center of the rolling circle to the point that traces the curve.Equations:
\r rolling inside a fixed circle of radius \R, at a distance \d from the center of the rolling circle.Plotting:
\draw command plots the curve using the plot function with the given parametric equations.domain is set from 0 to a multiple of 360 degrees to complete the curve. The multiple depends on the ratio of \R and \r.
\gcd(\R, \r) is the greatest common divisor of \R and \r.samples=1000: Specifies the number of points to plot, ensuring a smooth curve.smooth: Smooths the curve between the plotted points.variable=\t: Defines the parameter variable for plotting.Customizing the Spirograph:
Changing the Radii:
\R and \r to change the size and complexity of the spirograph.\R to \r will produce different patterns.Adjusting the Distance \d:
\d varies the position of the drawing point relative to the rolling circle.\d = \r, the point lies on the circumference of the rolling circle.Example Variations:
Epitrochoid (Rolling outside the fixed circle):
(\R - \r) with (\R + \r) and updating the signs.Adjusting the samples:
samples leads to a smoother curve but may increase compilation time.samples for quicker compilation during experimentation.Complete Example with Adjusted Parameters:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[scale=0.5]
% Parameters for a different spirograph pattern
\pgfmathsetmacro{\R}{5}
\pgfmathsetmacro{\r}{2}
\pgfmathsetmacro{\d}{1.5}
% Calculate Greatest Common Divisor (GCD)
\pgfmathsetmacro{\gcdValue}{gcd(\R,\r)}
% Total angle for a complete pattern
\pgfmathsetmacro{\totalAngle}{360*\r/\gcdValue}
% Plot the spirograph
\draw[domain=0:\totalAngle, samples=1000, smooth, variable=\t, red]
plot ({(\R-\r)*cos(\t) + \d*cos((\R-\r)/\r*\t)},
{(\R-\r)*sin(\t) - \d*sin((\R-\r)/\r*\t)});
\end{tikzpicture}
\end{document}
Tips:
\usetikzlibrary{math} for mathematical calculations.standalone document class to compile just the TikZ picture, which is useful for testing and including the graphic in other documents.scale parameter in [scale=0.5] to change the size of the plotted figure.By modifying the parameters and equations, you can create a wide variety of spirograph patterns directly within your LaTeX documents.
Not yet included in a published release.