Is There a One-Line Function That Generates a Triangle Wave?
In a Similar Way That Modulo Generates a Sawtooth Wave. It Doesn't Have to Be Continuous. Here Is What I Mean: Int M = 10; Int X = 0; Int I = 0; While (I <...
In a similar way that modulo generates a sawtooth wave. It doesn't have to be continuous.
here is what i mean:
int m = 10;
int x = 0;
int i = 0;
while (i < m*3) {
printf("%d ", x);
x++;
x = x % m;
i++;
}
generates a sequence 0..9, three times which looks like this:
note that the slope on the right side of the peak is just a graphing artifact
The one-liner in this case is x = i++ % m
What I want is this:
If you know one-liners for the other wave forms (sine, square), that would be good to know as well.
Update: everyone's answers have been very helpful and I have a follow-up question.
What would be added to the triangle wave function to make the slope of the lines curve in or out like this:
Thanks everyone, your varied answers helped me see the problem from a larger perspective. Special thanks to Noldorin for his take on extending the equation to quadratic curves.
8 Answers
Must Read
Triangular Wave
y = abs((x++ % 6) - 3);
This gives a triangular wave of period 6, oscillating between 3 and 0.