Clan x86

Technical (Development, Security, etc.) => General Programming => Tutorials, References, and Examples => Topic started by: MyndFyre on April 19, 2006, 05:35:37 AM

Title: [C#] Drawing Waves
Post by: MyndFyre 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:

    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.
Title: Re: [C#] Drawing Waves
Post by: Sidoh on April 19, 2006, 11:23:29 AM
Cool.
Title: Re: [C#] Drawing Waves
Post by: Blaze on April 20, 2006, 09:35:16 PM
Class Form1... :P
Title: Re: [C#] Drawing Waves
Post by: MyndFyre on April 21, 2006, 03:31:21 AM
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