In our first 3D printing lesson, we extruded graphs of regular functions like $y = f(x)$. But regular functions have a major limitation: they can only produce one $y$-value for each $x$-value (the vertical line test). That means you can never use a standard function to create closed loops, cloverleaves, stars, or heart medallions!
To 3D print real-world objects like medallions, keychains, and decorative shapes, we use parametric equations.
Instead of expressing $y$ directly in terms of $x$, both $x$ and $y$ are computed independently from a third variable called a parameter, usually an angle $t$ (or $\text{deg}$):
$$x = x(t)$$
$$y = y(t)$$
As the angle $\text{deg}$ sweeps through a full revolution from $0^\circ$ to $360^\circ$, the $(x, y)$ coordinates trace out a complete closed shape. When passed into print3d(x, y, R), our 3D engine turns the path into a seamless, solid, 3D-printable object!
Here is the core code for generating a 4-leaf clover design:
for deg = 0, 360, 2 do r = 15 + 10 * dcos(4 * deg) x[k] = r * dcos(deg) y[k] = r * dsin(deg) k = k + 1 end id = print3d(x, y, R)
Move the mouse over a dotted box for more information.
Polar to Cartesian: Any polar shape with radius $r(\theta)$ is easily converted into $x$ and $y$ using $x = r \cdot \cos(\theta)$ and $y = r \cdot \sin(\theta)$. In Lua, dcos() and dsin() take angles directly in degrees ($0^\circ$ to $360^\circ$).
Controlling Petals & Lobes: The number multiplying deg inside the cosine determines the number of leaves or lobes! Changing 4 * deg to 3 * deg creates a 3-leaf Irish shamrock, and 5 * deg creates a 5-petal star or flower.
Seamless 3D Printing: Because the curve starts at $0^\circ$ and returns to $360^\circ$, our 3D engine automatically stitches the start and end into a seamless closed ring with no internal dividing walls.
All coordinates in x and y are interpreted in millimeters on your 3D printer build plate.
Now you try. Set the petal multiplier to 4 and R = 1.2 to generate your 3D four-leaf clover. Once printed on screen, try changing the multiplier to 3 for a shamrock or 5 for a flower!
Type your code here:
See your results here:
The code above is designed to create a 3D-printable four-leaf clover medallion!
Replace the first ???? with 4 so the cosine oscillates 4 times to produce four clover petals.
Replace the second ???? with your desired filament thickness (try 1.2 mm).
Once you hit Run, explore your 3D design in the interactive viewer, and click the Download button to save your STL file!
Share your code
Show a friend, family member, or teacher what you've done!