我用一个简单的循环测试了它,你们根本没有回答这个人的问题。剪切和粘贴对人们没有帮助。难怪如此多的大众媒体恶作剧如此成功。对于一个重复他人错误的人来说,这不足为奇。人们甚至对这些错误的答案给予正面评价?!难以置信的!但是,这又与我之前的评论相似。
首先,我们将向我们的人们解释三角波是什么。好吧,它是一个波,其周期由两个相等的斜坡组成。一个向上倾斜的斜坡和一个与第一个相同但向下倾斜的斜坡与一个锯齿形相反,它具有一个向上倾斜的斜坡或一个向下倾斜的斜坡,在一个反向的 talud 之后重复。
PS:最后一个给出的“y(x)=(2a/π)arcsin(sin((2π/p)*x))”太复杂了,我们正在寻找一个快速的c++例程所以三角函数是绝对不可能。
测试例程:
(...)
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)
=> 我找到了一个以数学符号准确说明答案的来源:http://mathworld.wolfram.com/TriangleWave.html
我已经在一个名为 KmPlot 的 Linux 程序上测试了这个公式。 Linux 用户可以通过 root 终端输入 apt-get install kmplot 来获取 kmplot,如果这不起作用,请尝试使用常规终端并输入 sudo apt-get install kmplot,如果这不起作用,请观看此 youtube 视频以获取有关一般说明安装Linux程序http://www.youtube.com/watch?v=IkhcwxC0oUg
所以对线程问题的正确答案是如下所示的 C++ 形式的对称三角形函数声明示例:
(...)
int symetrictriangle(float x)
{
unsigned int period = 30; // number of increases of x before a new cycle begins
unsigned int amplitude = 100; // top to bottom value while the bottom value is always zero
return amplitude * 2 * abs(round(x/period)-(x/period));
}
(...)
干杯!