是否有产生三角波一行功能? [英] Is there a one-line function that generates a triangle wave?

查看:941
本文介绍了是否有产生三角波一行功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的方式模的生成锯齿波。它不必是连续的。

这里是我的意思是:

  INT M = 10;
INT X = 0;
INT I = 0;
而(ⅰ&下; M * 3){
    的printf(%D,X);
    X ++;
    X = X%米;
    我++;
}

生成一个序列0..9,三次看起来像这样:

注意,峰右侧的斜率仅仅是一个图形神器

一班轮在这种情况下的 X = 1 +%M


我想是这样的:

如果你知道单行为其他波形(正弦波,方波),这将是很好的了解为好。

更新:每个人的答案都非常有帮助,我有一个跟进的问题。

什么会被添加到三角波函数以使线曲线的斜率或缩小这样的:

谢谢大家,你不同的答案让我从一个更大的视角看问题。特别感谢诺多为他取的公式延伸到二次曲线。


解决方案

  X =米 -  ABS(I%(2 * M) -  M)

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.

解决方案

x = m - abs(i % (2*m) - m)

这篇关于是否有产生三角波一行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆