Software & Finance





C# - Plot SIN, COS and TAN Series





I have given here the C# source code to plot the trig. series functions - SIN, COS and TAN

 

Source Code


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace CSharpPlotTrigSeries

{

    public partial class PlotTrigSeries : Form

    {

        public PlotTrigSeries()

        {

            InitializeComponent();

        }

 

        public void PutPixel(Graphics g, int x, int y, Color c)

        {

            Bitmap bm = new Bitmap(1, 1);

            bm.SetPixel(0, 0, c);

            g.DrawImageUnscaled(bm, x, y);

        }

 

        bool m_bPlotSinSeries = true;

        bool m_bPlotCosSeries = true;

        bool m_bPlotTanSeries = true;

 

        private void Form1_Paint(object sender, PaintEventArgs e)

        {

            Graphics g = e.Graphics;

            g.Clear(Color.White);

            Rectangle rect = new Rectangle(0, 0, 500, 400);

 

            int margin = 50;

            int width = rect.Width - margin * 2;

            int height = rect.Height - margin * 2;

 

            Pen tPen = new Pen(Color.Black);

            g.DrawRectangle(tPen, 0 + margin / 2, 0 + margin / 2, width, height);

            g.DrawLine(tPen, 0 + margin / 2, margin / 2 + height / 2, width + margin / 2, margin / 2 + height / 2);

 

            double legendypos = margin;

            double legendxpos = margin;

 

            double PI = Math.PI;

            if (m_bPlotSinSeries == true)

            {

                tPen = new Pen(Color.Red);

                Brush br = new SolidBrush(Color.Red);

                for (double i = 0; i <= 720.01; i += 0.1)

                {

                    double value = Math.Sin(i * PI / 180);

                    value += 1.0;

                    int ypos = (int)(value / 2.0 * height + margin / 2);

                    int xpos = (int)(i / 720.0 * width + margin / 2);

                    PutPixel(g, xpos, ypos, Color.Red);

                    if ((Convert.ToInt32(i) % 90) == 0)

                    {

                        g.DrawLine(tPen, xpos, margin / 2 + height / 2 - 5, xpos, margin / 2 + height / 2 + 5);

                        g.DrawString(Convert.ToInt32(i).ToString(), Font, br, xpos - 10, margin / 2 + height / 2 + 10, StringFormat.GenericDefault);

                    }

                }

                g.DrawString("SIN Series - RED Color", Font, br, (float)legendxpos, (float)legendypos, StringFormat.GenericDefault);

                legendypos += 20;

            }

 

            if (m_bPlotCosSeries == true)

            {

                tPen = new Pen(Color.Blue);

                Brush br = new SolidBrush(Color.Blue);

 

                for (double i = 0; i <= 720.01; i += 0.1)

                {

                    double value = Math.Cos(i * PI / 180);

                    value += 1.0;

                    int ypos = (int)(value / 2.0 * height + margin / 2);

                    int xpos = (int)(i / 720.0 * width + margin / 2);

                    PutPixel(g, xpos, ypos, Color.Blue);

                    if ((Convert.ToInt32(i) % 90) == 0)

                    {

                        g.DrawLine(tPen, xpos, margin / 2 + height / 2 - 5, xpos, margin / 2 + height / 2 + 5);

                        g.DrawString(Convert.ToInt32(i).ToString(), Font, br, xpos - 10, margin / 2 + height / 2 + 10, StringFormat.GenericDefault);

                    }

                }

                g.DrawString("COS Series - BLUE Color", Font, br, (float)legendxpos, (float)legendypos, StringFormat.GenericDefault);

                legendypos += 20;

            }

 

            if (m_bPlotTanSeries == true)

            {

                double factor = 10; // Don't use this factor, it is only for drawing purpose.

                tPen = new Pen(Color.DarkBlue);

                Brush br = new SolidBrush(Color.DarkBlue);

                for (double i = 0; i <= 720.01; i += 0.1)

                {

                    double value = Math.Tan(i * PI / 180);

 

                    if (value > factor)

                        value = factor;

                    if (value < -factor)

                        value = -factor;

                    value += factor;

 

                    int ypos = (int)(value / (factor * 2) * height + margin / 2);

                    int xpos = (int)(i / 720.0 * width + margin / 2);

 

                    PutPixel(g, xpos, ypos, Color.DarkBlue);

                    if ((Convert.ToInt32(i) % 90) == 0)

                    {

                        g.DrawLine(tPen, xpos, margin / 2 + height / 2 - 5, xpos, margin / 2 + height / 2 + 5);

                        g.DrawString(Convert.ToInt32(i).ToString(), Font, br, xpos - 10, margin / 2 + height / 2 + 10, StringFormat.GenericDefault);

                    }

                }

                g.DrawString("TAN Series - DarkBlue Color", Font, br, (float)legendxpos, (float)legendypos, StringFormat.GenericDefault);

                legendypos += 20;

            }

            g.Dispose();

        }

 

        private void tanSeriesToolStripMenuItem_CheckedChanged(object sender, EventArgs e)

        {

            m_bPlotTanSeries = tanSeriesToolStripMenuItem.Checked;

        }

 

        private void cosSeriesToolStripMenuItem_CheckedChanged(object sender, EventArgs e)

        {

            m_bPlotCosSeries = cosSeriesToolStripMenuItem.Checked;

        }

 

        private void sinSeriesToolStripMenuItem_CheckedChanged(object sender, EventArgs e)

        {

            m_bPlotSinSeries = sinSeriesToolStripMenuItem.Checked;

 

        }

 

        private void sinSeriesToolStripMenuItem_Click(object sender, EventArgs e)

        {

            sinSeriesToolStripMenuItem.Checked = !sinSeriesToolStripMenuItem.Checked;

            Invalidate();

        }

 

        private void cosSeriesToolStripMenuItem_Click(object sender, EventArgs e)

        {

            cosSeriesToolStripMenuItem.Checked = !cosSeriesToolStripMenuItem.Checked;

            Invalidate();

        }

 

        private void tanSeriesToolStripMenuItem_Click(object sender, EventArgs e)

        {

            tanSeriesToolStripMenuItem.Checked = !tanSeriesToolStripMenuItem.Checked;

            Invalidate();

        }

    }

}

 

 

namespace CSharpPlotTrigSeries

{

    partial class PlotTrigSeries

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

 

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Windows Form Designer generated code

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.menuStrip1 = new System.Windows.Forms.MenuStrip();

            this.plotToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

            this.sinSeriesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

            this.cosSeriesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

            this.tanSeriesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

            this.menuStrip1.SuspendLayout();

            this.SuspendLayout();

            //

            // menuStrip1

            //

            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

            this.plotToolStripMenuItem});

            this.menuStrip1.Location = new System.Drawing.Point(0, 0);

            this.menuStrip1.Name = "menuStrip1";

            this.menuStrip1.Size = new System.Drawing.Size(492, 24);

            this.menuStrip1.TabIndex = 0;

            this.menuStrip1.Text = "menuStrip1";

            //

            // plotToolStripMenuItem

            //

            this.plotToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

            this.sinSeriesToolStripMenuItem,

            this.cosSeriesToolStripMenuItem,

            this.tanSeriesToolStripMenuItem});

            this.plotToolStripMenuItem.Name = "plotToolStripMenuItem";

            this.plotToolStripMenuItem.Size = new System.Drawing.Size(37, 20);

            this.plotToolStripMenuItem.Text = "Plot";

            //

            // sinSeriesToolStripMenuItem

            //

            this.sinSeriesToolStripMenuItem.Checked = true;

            this.sinSeriesToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;

            this.sinSeriesToolStripMenuItem.Name = "sinSeriesToolStripMenuItem";

            this.sinSeriesToolStripMenuItem.Size = new System.Drawing.Size(152, 22);

            this.sinSeriesToolStripMenuItem.Text = "Sin Series";

            this.sinSeriesToolStripMenuItem.CheckedChanged += new System.EventHandler(this.sinSeriesToolStripMenuItem_CheckedChanged);

            this.sinSeriesToolStripMenuItem.Click += new System.EventHandler(this.sinSeriesToolStripMenuItem_Click);

            //

            // cosSeriesToolStripMenuItem

            //

            this.cosSeriesToolStripMenuItem.Checked = true;

            this.cosSeriesToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;

            this.cosSeriesToolStripMenuItem.Name = "cosSeriesToolStripMenuItem";

            this.cosSeriesToolStripMenuItem.Size = new System.Drawing.Size(152, 22);

            this.cosSeriesToolStripMenuItem.Text = "Cos Series";

            this.cosSeriesToolStripMenuItem.CheckedChanged += new System.EventHandler(this.cosSeriesToolStripMenuItem_CheckedChanged);

            this.cosSeriesToolStripMenuItem.Click += new System.EventHandler(this.cosSeriesToolStripMenuItem_Click);

            //

            // tanSeriesToolStripMenuItem

            //

            this.tanSeriesToolStripMenuItem.Checked = true;

            this.tanSeriesToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;

            this.tanSeriesToolStripMenuItem.Name = "tanSeriesToolStripMenuItem";

            this.tanSeriesToolStripMenuItem.Size = new System.Drawing.Size(152, 22);

            this.tanSeriesToolStripMenuItem.Text = "Tan Series";

            this.tanSeriesToolStripMenuItem.CheckedChanged += new System.EventHandler(this.tanSeriesToolStripMenuItem_CheckedChanged);

            this.tanSeriesToolStripMenuItem.Click += new System.EventHandler(this.tanSeriesToolStripMenuItem_Click);

            //

            // PlotTrigSeries

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(492, 373);

            this.Controls.Add(this.menuStrip1);

            this.MainMenuStrip = this.menuStrip1;

            this.Name = "PlotTrigSeries";

            this.Text = "CSharp - Plot  Trig Series - softwareandfinance.com";

            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

            this.menuStrip1.ResumeLayout(false);

            this.menuStrip1.PerformLayout();

            this.ResumeLayout(false);

            this.PerformLayout();

 

        }

 

        #endregion

 

        private System.Windows.Forms.MenuStrip menuStrip1;

        private System.Windows.Forms.ToolStripMenuItem plotToolStripMenuItem;

        private System.Windows.Forms.ToolStripMenuItem sinSeriesToolStripMenuItem;

        private System.Windows.Forms.ToolStripMenuItem cosSeriesToolStripMenuItem;

        private System.Windows.Forms.ToolStripMenuItem tanSeriesToolStripMenuItem;

    }

}

 

 

using System;

using System.Collections.Generic;

using System.Windows.Forms;

 

namespace CSharpPlotTrigSeries

{

    static class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new PlotTrigSeries());

        }

    }

}

Click here to download the source code and executable

 

 

Output