繁体   English   中英

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

[英]Drawing y = sin(θ) * cos(θ) in C# with a Pen

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

我想要这个结果: 在此处输入图片说明

实际上,您正在寻找的真正功能有点不同……请参阅此处的示例。 看看这篇关于极地花的文章,我相信它会指向正确的方向,并且它还包含一个完整的工作源代码。

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

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花瓣。

输出

您可能会发现查看相应的参数方程更容易。

在此处输入图片说明

    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);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM