我工作上的一些代码在哪里我有一个 Time 对象与成员 time. Time.time 让 我的时间,因为我的应用程序的开始几秒钟内(float value)。现在我想创建一个脉动的价值在0和1之间,然后从1到0再次,它继续这样做变薄,直到该应用程序的中止。

我想到使用sin()但不知道怎么通过它作为参数,以创建这个脉动的价值。

我将如何创建这个脉动的价值?

亲切的问候, Pollux

有帮助吗?

解决方案

您提使用罪(),所以我想你想它在0和1之间连续脉冲。

像这样的东西就可以了:

float pulse(float time) {
    const float pi = 3.14;
    const float frequency = 10; // Frequency in Hz
    return 0.5*(1+sin(2 * pi * frequency * time));
}

1/frequency = 0.1 second为周期,为1点的之间的时间。

其他提示

怎么x=1-x?或如果你想要它是基于时间使用定时器%2

噢,你想的数值在0和1之间。关于如何数学。Abs(100-(计时器%200))/100 在定时器什么样的日期时间。现在。TimeOfDay.TotalMilliseconds

编辑: 我的测试表明这是多快两倍罪的方法。1万次迭代,罪恶的方法需要.048秒而获取和惠益分享的方法需要大约.023秒钟。还有,你得到不同的波形出了两个人,当然。罪产生的正弦波形,同时Abs产生一个三角形的浪潮。

static void Main(string[] args)
{
   System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
   sw.Start();
   const int count = 1000000;
   float[] results = new float[count];
   for (int i = 0; i < count; i++)
   {
      results[i] = AbsPulse(i/1000000F);
      //results[i] = SinPulse(i / 1000000F);
   }
   sw.Stop();
   Console.WriteLine("Time Elapsed: {0} seconds", sw.Elapsed.TotalSeconds);
   char[,] graph = new char[80, 20];
   for (int y = 0; y <= graph.GetUpperBound(1); y++)
      for (int x = 0; x <= graph.GetUpperBound(0); x++)
         graph[x, y] = ' ';
   for (int x = 0; x < count; x++)
   {
      int col = x * 80 / count;
      graph[col, (int)(results[x] * graph.GetUpperBound(1))] = 'o';
   }
   for (int y = 0; y <= graph.GetUpperBound(1); y++)
   {
      for (int x = 0; x < graph.GetUpperBound(0); x++)
         Console.Write(graph[x, y]);
      Console.WriteLine();
   }
}

static float AbsPulse(float time)
{
   const int frequency = 10; // Frequency in Hz
   const int resolution = 1000; // How many steps are there between 0 and 1
   return Math.Abs(resolution - ((int)(time * frequency * 2 * resolution) % (resolution * 2))) / (float)resolution;
}

static float SinPulse(float time)
{
   const float pi = 3.14F;
   const float frequency = 10; // Frequency in Hz
   return 0.5F * (1 + (float)Math.Sin(2 * pi * frequency * time));
}

一个正弦函数将是理想我想,但你需要调整的时期和规模。

在正弦函数产生的结果在-1和1之间,但希望在0和1之间去要正确要(sin(x)+1)/2缩放。

在零的正弦函数开始,进行到在1 PI / 2,在PI再次为零,-1,3 * pi / 2之间,在2 * PI回零。缩放,第一零会发生在3 * pi / 2之间,之后的第一最大将是5/2 * PI。所以x先前式中的(2*time + 3) * pi/2

全部放在一起:(sin((2*time.time + 3) * pi/2) + 1) / 2

你经常希望它脉搏?

您想从0到去1 10秒让我们说。

float pulseValueForTime(int sec) {
    int pulsePoint = sec % 10;
    float pulsePercent = (float)pulsePoint / (float)10;
    float pulseInTermsOfPI = (pulsePercent * 2 * PI) - PI;
    float sinVal = MagicalSinFunction(pulseInTermsOfPI); // what framework you use to compute sin is up to you... I'm sure you can google that!
    return (sinVal + 1) / 2; // sin is between 1 and -1, translate to between 0 and 1
}

进入查找易于功能。他们做这种事情的方式各种各样 - 线性聚,EXP,罪等

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top