News:

Help! We're trapped in the computer, and the computer is trapped in 2008! Someone call the time police!

Main Menu

[C#] Drawing Waves

Started by MyndFyre, April 19, 2006, 05:35:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MyndFyre

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:

    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.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Sidoh


Blaze

And like a fool I believed myself, and thought I was somebody else...

MyndFyre

Quote from: Blaze on April 20, 2006, 09:35:16 PM
Class Form1... :P

Quote from: MyndFyrex86] link=topic=5620.msg65809#msg65809 date=1145439337]
Took about 15 minutes. 

Quote from: MyndFyrex86] link=topic=5620.msg65809#msg65809 date=1145439337]
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
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.