简体   繁体   中英

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

I want to draw sin(θ)*cos(θ) , but it doesn't work. I can draw sin or cos , but I want to draw sin(θ)*cos(θ) together.

Here is my code

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

And I want this result: 在此处输入图片说明

Actually, the real function you are looking for is a little bit different... see an example here . Looking at this article about polar flowers, I'm sure it will get pointed to the right direction, and it also contains a full working source code.

Just an example, supposing you use a panel in your form on which to draw the polar flower:

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

The basic formulation states that if the petals variable value is:

  • even , then it represents half the amount of petals of the polar flower
  • odd , then it represents the amount of petals of the polar flower

so if you define Double petals = 2.0d; , you will obtain 4 petals... and if you define Double petals = 5.0d; , you will obtain 5 petals.

输出

You may find it easier to look at the corresponding Parametric Equations .

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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