Author Topic: [C#] Drawing Waves  (Read 6680 times)

0 Members and 1 Guest are viewing this topic.

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
[C#] Drawing Waves
« on: April 19, 2006, 05:35:37 am »
I needed to do a sound demonstration for a psychology project, and thought I'd utilize wave theory for the background.  However, I didn't have a convenient way of drawing sine waves.  So I made a C# project do it for me.  Took about 15 minutes.  Here's the interesting stuff:
Code: [Select]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.pictureBox1.Image = GenerateSineWave(360, 50, 360);
            this.pictureBox2.Image = GenerateSineWave(360, 50, 180);
            this.pictureBox3.Image = GenerateSineWave(360, 50, 120);
            this.pictureBox4.Image = GenerateCombinedSineWave(360, 50, 360, 180, 120);
        }


        public static Image GenerateSineWave(int width, int height, int period)
        {
            Bitmap bmp = new Bitmap(width, height);
            using (Graphics gfx = Graphics.FromImage(bmp))
            {
                gfx.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);
                Brush scale = new SolidBrush(Color.Red);
                Pen pScale = new Pen(scale);
                gfx.DrawLine(pScale, 0, 0, 0, height);
                int midHeight = height / 2;
                gfx.DrawLine(pScale, 0, midHeight, width, midHeight);
            }

            double periodRatio = 360.0 / (double)period;

            for (int i = 0; i < width; i++)
            {
                double rad = DegToRad((double)i * periodRatio);
                double sinVal = Math.Sin(rad);
                double y = -(sinVal * 20.0) + 25;
                bmp.SetPixel(i, (int)y, Color.Black);
            }

            return bmp;
        }

        public static Image GenerateCombinedSineWave(int width, int height, int pd1, int pd2, int pd3)
        {
            Bitmap bmp = new Bitmap(width, height);
            using (Graphics gfx = Graphics.FromImage(bmp))
            {
                gfx.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);
                Brush scale = new SolidBrush(Color.Red);
                Pen pScale = new Pen(scale);
                gfx.DrawLine(pScale, 0, 0, 0, height);
                int midHeight = height / 2;
                gfx.DrawLine(pScale, 0, midHeight, width, midHeight);
            }
            double pdRat1 = 360.0 / (double)pd1;
            double pdRat2 = 360.0 / (double)pd2;
            double pdRat3 = 360.0 / (double)pd3;

            for (int i = 0; i < width; i++)
            {
                double rad1 = DegToRad((double)i * pdRat1);
                double rad2 = DegToRad((double)i * pdRat2);
                double rad3 = DegToRad((double)i * pdRat3);
                double sin1 = Math.Sin(rad1);
                double sin2 = Math.Sin(rad2);
                double sin3 = Math.Sin(rad3);
                double y = sin1 + sin2 + sin3;
                y = -(y * 20.0 / 3.0) + 25.0;
                bmp.SetPixel(i, (int)y, Color.Black);
            }

            return bmp;
        }

        private static double DegToRad(double degr)
        {
            return (Math.PI * degr) / 180.0;
        }

Full Visual Studio 2005 project is included in the attachments.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [C#] Drawing Waves
« Reply #1 on: April 19, 2006, 11:23:29 am »
Cool.

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: [C#] Drawing Waves
« Reply #2 on: April 20, 2006, 09:35:16 pm »
Class Form1... :P
And like a fool I believed myself, and thought I was somebody else...

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C#] Drawing Waves
« Reply #3 on: April 21, 2006, 03:31:21 am »
Class Form1... :P

Took about 15 minutes. 

I needed to do a sound demonstration for a psychology project,

I also used a partial class, designers, and didn't rename the designed form objects.  (this.pictureBox1)  :P
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.