# How do you draw sin, cos and tan with shaders?

I want to draw sin, cos, tan and other mathematical functions with the shader editor. How do I do this?

you can use this node setup:

You can now add any math node between the two multiplier nodes.

• +1 but... Hehe.... the challenge is to get a stroke with even thickness. A true (perpendicular-distance-from-sine) function is a bit of a nightmare.... Commented May 14, 2022 at 10:28
• I am sure you can easily do that!!! Commented May 14, 2022 at 10:41

This aspect may not matter that much to you.. ( if it did, it might be better to go to a vector-graphics program and use its internal rasterization, or implement the graph as geometry in Blender).

As always, open to challenge, but stroking a sine-wave, or other trig. functions, with an even thickness, turns out to be a hard problem. See, for example, this article. And you would need different distance-functions for every graphed function.

You could let the compositor in Blender approximate it for you with edge-detection..

First graph f(X) < Y:

And then put the result (on the left) through edge-detection and dilation in the compositor:

but I wouldn't expect it to be worth the hack, since even that is still an approximation..

• Isn’t pixel based always just an approximation….?😇 of course +1 for this nice solution Commented May 14, 2022 at 13:47
• @Chris true. But I think spline rasterisation, as in vector graphics, is a bit cleverer than Dilate? Finding curve-stroking algorithms is harder than I thought. Commented May 14, 2022 at 13:51
• I love watching perfectionist doing their work - but I am not one 🙃😀 Commented May 14, 2022 at 13:52
• you won't believe it, but i don't "see" myself as an artist. Don't know why i have a problem with "artist" but i see myself as a digital creator..... ;) that's more neutral ...and art is misused to much...i think...but yeah...don't know.... ;) Commented May 14, 2022 at 14:05
• It has been [redacted] years since I taught vector graphics, but IIRC, we solved curve width problems using mean weighted dithering. I can't find the papers now, but it doesn't really matter because you can't do that with Blender shaders anyway, AFAICT Commented May 14, 2022 at 15:20

A really simple node setup that gives you a gradient as well, which of course can be changed to a solid line if you want.

Using a MixRGB node set to difference mode, comparing sin(x) to y just like an equation. You can do this to plot any equation, really.

Here it is with a custom color, thickness, and gradient.

Well, I can't offer a uniform line thickness, but I can give you procedural axis grid lines