Triangular Wave

This gives a triangular wave of period 6, oscillating between 3 and 0.

Square Wave

y = (x++ % 6) < 3 ? 3 : 0;

This gives a regular square wave of period 6, oscillating between 3 and 0.

Sine Wave

y = 3 * sin((float)x / 10);

This gives a sine wave of period 20 pi, oscillating between 3 and -3.

Update:

Curvy Triangular Wave

To get a variation of the triangular wave that has curves rather than straight lines, you just need to introduce an exponent into the equation to make it quadratic.

Concave curves (i.e. x^2 shape):

y = pow(abs((x++ % 6) - 3), 2.0);

Concave curves (i.e. sqrt(x) shape):

y = pow(abs((x++ % 6) - 3), 0.5);

Alternatively to using the pow function, you could simply define a square function and use the sqrt function in math.h, which would probably improve performance a bit.

Also, if you want to make the curves steeper/shallower, just try changing the indices.

In all of these cases you should easily be able to adjust constants and add scaling factors in the right places to give variations of the given waveforms (different periods, ampltiudes, asymmetries, etc.).

Note that my x does not correspond to your x in the question. The x I used here represents the conventional value along the x-axis, and similarly y represents the value along the y-axis. The x here seems to be the i in your question.
– NoldorinJul 2 '09 at 10:43

I could add these waveforms to get more complicated ones as well, yes?
– willc2Jul 2 '09 at 11:16

@JoanVenge: The subtraction of a constant is to offset the position of the curve so it's symmetric around the x-axis. Thus 3 is actually the amplitude (half of the peak-trough distance, which is 6). The pow(..., 2.0) is to form the quadratic curve.
– NoldorinOct 27 '12 at 23:33

Where x is a running integer, and y the triangle wave output.
A is the amplitude of the wave, and P the half-period. For instance, A=5 will produce a wave which goes from 0 to 5; P=10 will produce a wave with a period of 20. The wave starts at y=0 for x=0.

Note that y will be a floating-point number unless P is a factor of A.
And, yes, for the mathematical purists: A is technically twice the amplitude of the wave, but look at the picture below and you'll understand what I mean.

Edit: What I call amplitude is actually not the amplitude, but the maximum value (i.e. 5 if the curve oscillates betwen 0 and 5). The amplitude in the mathematical sense is half of that. But you get the point.

this results in a "V" wave; you'd need to add "y = m - y" to get a triangle wave
– bubakerJul 2 '09 at 10:58

A wave is usually considered infinite, making a "V wave" and a triangle wave essentially identical. But you're right in so far that if you want f(0) = 0, you have to do a shift.
– balpha♦Jul 2 '09 at 11:09

I've tested it with a simple loop and you guys haven't answered the man's question at all. Cut and pasting won't help people. No wonder so many mass media hoaxes have so much success. With a folks that repeats mistakes of others that cannot be any surprise is it. And people even give positive reputation rates for these wrong answers?! Unbelievable! But again, this parallels with my previous remark.

So first off we are going to explain to our folks what a TRIANGLE wave IS. Well it is a wave which has a period consisting of TWO equal sloped ramps. A ramp sloped UP and a ramp equally as the first but sloped DOWN contrary to a SAWTOOTH which has a ramp sloped UP or a ramp sloped DOWN repeated after a reversed talud.

PS: The last one who gave "y(x)=(2a/π)arcsin(sin((2π/p)*x))" is too complicated, we are looking for a fast c++ routine so trigonometry is absolutely out of the question.

Test routine:

(...)

for (byte V=0; V<255; V++)
{
unsigned int x = evenramp(V);
plotRGB(0,0,x,x,x);
delay(100); // make sure you have your own delay function declared of course
/* use your own graphic function !!! plotRGB(row,column,R,G,B) */
/* the light intensity will give you the change of value V in time */
/* all functions suggested as answer give SAWTOOTH and NOT TRIANGLE */
/* it is a TRIANGLE the man wants */
}
float triangleattempt(unsigned int x) // place any answered formula after '255 *' behind return.
{
return 255 * (255 - abs(255 - 2*(x % 255))); // this will show a SAWTOOTH
}
//All given answers up to now excluding "function xs ( xx : float ): float" (this is not the requested one-liner, sorry) that are not a symmetrical triangle wave
// m - abs(i % (2*m) - m); (this is still a SAWTOOTH wave and not a TRIANGLE wave)
// abs((x++ % 6) - 3); (this is still a SAWTOOTH wave and not a TRIANGLE wave)
// abs(amplitude - x % (2*amplitude)); (this is still a SAWTOOTH wave and not a TRIANGLE wave)

I have tested the formula on a Linux program called KmPlot. Linux users can get kmplot via the root terminal typing apt-get install kmplot and if that doesn't work try using the regular terminal and type sudo apt-get install kmplot and if that doesn't work watch this youtube video for general instructions on installing a Linux program http://www.youtube.com/watch?v=IkhcwxC0oUg

SO A CORRECT ANSWER to the thread question is an example of a symmetrical triangle function declaration in c++ form shown below:

The other answers were clear and concise, much more directly answering the question with a "one liner". From the question itself it's clear that the OP was aware of what a triangle wave is, so unsure why this is so hostile towards them.
– Luke BriggsSep 17 '15 at 11:33

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).