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

问题描述 投票:-4回答:2

我想绘制sin(θ)*cos(θ),但它不起作用。我可以画罪或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

c# .net winforms math drawing
2个回答
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);
        }
    }

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

© www.soinside.com 2019 - 2024. All rights reserved.