用笔在C#中绘制y = sin(θ)* cos(θ)

时间:2017-12-25 20:48:56

标签: c# .net winforms math drawing

我想绘制sin(θ)*cos(θ),但它不起作用。 我可以画 sin cos , 但我想一起画sin(θ)*cos(θ)

这是我的代码

private void button1_Click(object sender, EventArgs e)
{
   Graphics drw = this.CreateGraphics();
   Pen pen = new Pen(Brushes.Black, 7.0f);
   float x1 = 0;
   float y1 = 0;
   float xoy = 200;
   float ef = 20;

   for (double  i=0;i<40;i+=1)
   {
      double radi = (float)(i * 180 / Math.PI);
      float temp = (float)Math.Cos(radi)*(float)Math.Sin(radi);
      drw.DrawLine(pen, x1 * ef, y1 * ef + xoy, ef * (float)i, temp * ef + xoy);
      x1 = (float)i;
      y1 = temp;
    }
}

我想要这个结果: enter image description here

2 个答案:

答案 0 :(得分:1)

实际上,您正在寻找的实际功能有点不同......请参阅示例here。看看关于极地花的this article,我确信它会指向正确的方向,并且它还包含完整的工作源代码。

举一个例子,假设您在表单中使用了一个用于绘制极地花的面板:

panel.OnPaint += Panel_Paint;

private void Panel_Paint(Object sender, PaintEventArgs e)
{
    Double scale = ((Panel)sender).Width / 2.0d;
    Double repetitions = Math.Round(scale, 0);
    Double basis = (2.0d * Math.PI) / scale;
    Double petals = 2.0d;

    using (Graphics g = e.Graphics)
    {
        using (Pen pen = new Pen(Brushes.Red, 2.0f))
        {
            for (Double i = 0.0f; i < (repetitions - 1); ++i)
            {
                Double t0 = i*basis;
                Double t1 = (i + 1)*basis;

                Double x0 = Math.Sin(petals * t0) * Math.Cos(t0);
                Double x1 = Math.Sin(petals * t1) * Math.Cos(t1);

                Double y0 = Math.Sin(petals * t0) * Math.Sin(t0);
                Double y1 = Math.Sin(petals * t1) * Math.Sin(t1);

                g.DrawLine
                    (
                        pen,
                        (Single) ((scale*x0) + scale),
                        (Single) ((scale*y0) + scale),
                        (Single) ((scale*x1) + scale),
                        (Single) ((scale*y1) + scale)
                    );
            }
        }
    }
}

基本公式表明,如果petals变量值为:

  • 甚至,然后它代表极地花瓣数量的一半
  • 奇数,然后它代表极地花瓣的数量

因此,如果您定义Double petals = 2.0d;,您将获得4个花瓣......如果您定义Double petals = 5.0d;,您将获得5个花瓣。

Output

答案 1 :(得分:1)

您可能会发现更容易查看相应的 Parametric Equations

enter image description here

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        var g = e.Graphics;

        double pi = Math.PI;
        int n = 100;
        var t = Enumerable.Range(0, n).Select(p => p * 2 * pi / n).ToArray();
        var x = t.Select(p => Math.Sin(2 * p) * Math.Cos(p)).ToArray();
        var y = t.Select(p => Math.Sin(2 * p) * Math.Sin(p)).ToArray();

        Pen pen = new Pen(Brushes.Black, 3);

        int scale = 100;
        int shift = 100;
        for (int i = 0; i < n - 1; i++)
        {
            g.DrawLine(pen, scale*(float)x[i] + shift, 
                scale*(float)y[i] + shift, 
                scale*(float)x[i + 1] + shift, 
                scale*(float)y[i + 1] + shift);
        }
    }