Here's the MatLab code to create a spirograph, I used it in conjunction with a 3D program to create a 3D spirograph, but this is it's core.
%%%%%%%% Spirograph Code %%%%%%%%%%
clear; clc; %Clean workspace.
%%%%% Imput Parameters (play with theese)
teethBig = 50; %Only integers please.
teethSmall = 26; %Only integers please.
penPosition = 1; %Any value (realistic drawing ocurr with values between 0 and 1)
%%%%%%%%%
%Calculate the number of revolutions to finish the drawing.
r0 = teethBig;
r1 = teethSmall;
while( r1 ~= 0 )
r2 = mod(r0,r1);
r0 = r1;
r1 = r2;
end
revolutions = teethSmall / r0;
%Create the driven variable.
theta = linspace(0,2*pi*revolutions,1000);
%Here is the meat of the process.
drawingX = (teethBig - teethSmall)*cos(theta) +...
penPosition*teethSmall*cos(-teethBig*theta/teethSmall);
drawingY = (teethBig - teethSmall)*sin(theta) +...
penPosition*teethSmall*sin(-teethBig*theta/teethSmall);
%Drawing the result.
plot(drawingX,drawingY,'lineWidth',2);
axis equal;
set(gca,'XTick',[]);
set(gca,'YTick',[]);
%%%%%%%% End Code %%%%%%%%%
%%%%%%%% Spirograph Code %%%%%%%%%%
clear; clc; %Clean workspace.
%%%%% Imput Parameters (play with theese)
teethBig = 50; %Only integers please.
teethSmall = 26; %Only integers please.
penPosition = 1; %Any value (realistic drawing ocurr with values between 0 and 1)
%%%%%%%%%
%Calculate the number of revolutions to finish the drawing.
r0 = teethBig;
r1 = teethSmall;
while( r1 ~= 0 )
r2 = mod(r0,r1);
r0 = r1;
r1 = r2;
end
revolutions = teethSmall / r0;
%Create the driven variable.
theta = linspace(0,2*pi*revolutions,1000);
%Here is the meat of the process.
drawingX = (teethBig - teethSmall)*cos(theta) +...
penPosition*teethSmall*cos(-teethBig*theta/teethSmall);
drawingY = (teethBig - teethSmall)*sin(theta) +...
penPosition*teethSmall*sin(-teethBig*theta/teethSmall);
%Drawing the result.
plot(drawingX,drawingY,'lineWidth',2);
axis equal;
set(gca,'XTick',[]);
set(gca,'YTick',[]);
%%%%%%%% End Code %%%%%%%%%
Category Resources / Tutorials
Species Unspecified / Any
Size 1087 x 858px
File Size 689 kB
MatLab is a sophisticated scientific calculator, unfortunately it's not free (far from it).
Luckily there is a free option to MatLab; Octave is a free program that does everything MatLab does, and it's free.
Luckily there is a free option to MatLab; Octave is a free program that does everything MatLab does, and it's free.
It's a very light program, it runs on very old machines where I work, and it runs pretty good. It only requires computational power when you ask it to solve a complex model; once I was working with a combinatory model and this program took 3 days in giving me the result. But for a simple program like this one, any computer is enough.
FA+

Comments