title: Problem with duty cycle (rectangular wave) with drawline C#
body: <p>I need to draw a rectangular wave using drawline, depending on the duty cycle, the frequency, and the amplitude (using trackbars). Everything is working fine, except for the duty cycle.</p>
  <p>Here is the formula for my wave, which is basically a square wave :</p>
  <pre><code>double y = Math.Sign(Math.Sin(x / 180 * Math.PI)) * amplitude.Value;
  </code></pre>
  <p>I'm using a pictureBox in order to draw it (every integers are only here for a better display on the pictureBox):</p>
  <pre><code>private void P_ZoneDeTrace_Paint(object sender, PaintEventArgs e)
          {
              Graphics g = e.Graphics;
              Pen myPen = new Pen(Color.Green);
  
          double x1 = 10;
          double y1 = 0;
              
         
          for (double x = 1; x &lt; pictureBox1.Width*10; x++)
          {
              double y = Math.Sign(Math.Sin(x / 180 * Math.PI)) * amplitude.Value;
           
              g.DrawLine(myPen, (float)x1, (float)y1 + 120, (float)(x / fréquence.Value) + 10, (float)y + 120);
              x1 = x / fréquence.Value + 10;
              y1 = y;
              
          }}
  </code></pre>
  <p>I have now a regular square wave. Now I need to transform it into a rectangular wave, using the duty cyle, which is a trackbar, going from 0 to 10. If it's equal to 0, the wave is always at -1, and if it's equal to 10, the wave is always at 1.</p>
  <p>In my <code>else</code> I tried to manage the wave when the duty cycle is between 1 and 9 :</p>
  <ul>
  <li>close to 1, every pattern has to stay longer at -1</li>
  <li>close to 9, every pattern has to stay longer at 1</li>
  </ul>
  <p>Here is the code, right under the y formula, to do that :</p>
  <pre><code> int i = 0;
   int val = 0;
                  if (rapport.Value == 0)
                  {
                      if (y &lt; 0)
                      {
                          y = y * -1;
                      }
                  }
                  if (rapport.Value == 10)
                  {
                      if (y &gt; 0)
                      {
                          y = y * -1;
                      }
                  }
  
                  else
                  {
                      if (y&gt;0 &amp;&amp; i &lt; rapport.Value *18 &amp;&amp; val==0)
                      {
                          y = y * -1;
                          i++;
                      }
  
                      if (y &gt; 0 &amp;&amp; i == (rapport.Value * 18))
                      {
                          val = 1;
  
                      }
  
  
                      if (y &lt; 0 &amp;&amp; val == 1)
                      {
                          i = 0;
                          val = 0;
                      }
  
  
                  }
  </code></pre>
  <p>I multiply &quot;y&quot; by -1 to go from -1 to 1 and conversely. &quot;rapport&quot; is the duty cycle, and it's multiplied by 18 in order to be proportional with my wave.
  &quot;val&quot; help me to modify other patterns than the first one (if I don't use &quot;val&quot;, only the first one is modified)</p>
  <p>The problem is that only the first pattern is working well, as described before. For other patterns, the wave is staying to long at 1 when duty cycle is close to 1 (and conversely if it's close to 9).</p>
  <p>Here is the full signal :
  <a href="https://i.stack.imgur.com/CfYow.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CfYow.png" alt="enter image description here" /></a></p>
  <p>The first pattern, which is right :</p>
  <p><a href="https://i.stack.imgur.com/hwAyf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hwAyf.png" alt="enter image description here" /></a></p>
  <p>Other patterns, wrong, and that should be the same as the first one :</p>
  <p><a href="https://i.stack.imgur.com/5Eo6O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5Eo6O.png" alt="enter image description here" /></a></p>
  <p>Every patterns should be the same as the first one, and as you can see, it's not working. They are moving using the trackbar, but not enough, the most beeing as the picture above).</p>
  <p>If you could help me to resolve this problem, every pattenrs having to behave as the 1st one ? thanks in advance.</p>