Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about x = 1 - x? Or if you want it to be time based use Timer % 2</p> <p>Oh, you wanted the values between 0 and 1 as well. how about Math.Abs(100 - (Timer % 200)) / 100 Where timer is something like DateTime.Now.TimeOfDay.TotalMilliseconds</p> <p><strong>Edit:</strong> My tests indicate that this is more than twice as fast as the Sin method. For 1 million iterations, the sin method takes .048 seconds while the Abs method takes about .023 seconds. Also, you get different waveforms out of the two, of course. Sin produces a Sine wave, while Abs produces a triangular wave.</p> <pre><code>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 &lt; 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 &lt;= graph.GetUpperBound(1); y++) for (int x = 0; x &lt;= graph.GetUpperBound(0); x++) graph[x, y] = ' '; for (int x = 0; x &lt; count; x++) { int col = x * 80 / count; graph[col, (int)(results[x] * graph.GetUpperBound(1))] = 'o'; } for (int y = 0; y &lt;= graph.GetUpperBound(1); y++) { for (int x = 0; x &lt; 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)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload