첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 정규 분포 곡선(Normal Distribution Curve) 그리기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Text;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.drawButton.Click += drawButton_Click;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 그리기 버튼 클릭시 처리하기 - drawButton_Click(sender, e)

 

        /// <summary>

        /// 그리기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void drawButton_Click(object sender, EventArgs e)

        {

            float mean              = float.Parse(this.meanTextBox.Text);

            float standardDeviation = float.Parse(this.standardDeviationTextBox.Text);

            float variance          = standardDeviation * standardDeviation;

 

            Bitmap bitmap = new Bitmap

            (

                this.canvasPictureBox.ClientSize.Width,

                this.canvasPictureBox.ClientSize.Height

            );

 

            using(Graphics graphics = Graphics.FromImage(bitmap))

            {

                graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

                const float MINIMUM_WORLD_X = -5.1f;

                const float MINIMUM_WORLD_Y = -0.2f;

                const float MAXIMUM_WORLD_X = -MINIMUM_WORLD_X;

                const float MAXIMUM_WORLD_Y = 1.1f;

                const float WORLD_WIDTH     = MAXIMUM_WORLD_X - MINIMUM_WORLD_X;

                const float WORLD_HEIGHT    = MAXIMUM_WORLD_Y - MINIMUM_WORLD_Y;

 

                RectangleF worldRectangle = new RectangleF

                (

                    MINIMUM_WORLD_X,

                    MINIMUM_WORLD_Y,

                    WORLD_WIDTH,

                    WORLD_HEIGHT

                );

 

                PointF[] devicePointArray =

                {

                    new PointF(0, this.canvasPictureBox.ClientSize.Height),

                    new PointF(this.canvasPictureBox.ClientSize.Width, this.canvasPictureBox.ClientSize.Height),

                    new PointF(0, 0),

                };

 

                Matrix transformMatrix = new Matrix(worldRectangle, devicePointArray);

 

                using(Pen pen = new Pen(Color.Red, 0))

                {

                    using(Font font = new Font("Arial", 8))

                    {

                        graphics.Transform = transformMatrix;

 

                        pen.Color = Color.Black;

 

                        graphics.DrawLine(pen, MINIMUM_WORLD_X, 0, MAXIMUM_WORLD_X, 0);

 

                        for(int x = (int)MINIMUM_WORLD_X; x <= MAXIMUM_WORLD_X; x++)

                        {

                            graphics.DrawLine(pen, x, -0.05f, x, 0.05f);

                            graphics.DrawLine(pen, x + 0.25f, -0.025f, x + 0.25f, 0.025f);

                            graphics.DrawLine(pen, x + 0.50f, -0.025f, x + 0.50f, 0.025f);

                            graphics.DrawLine(pen, x + 0.75f, -0.025f, x + 0.75f, 0.025f);

                        }

 

                        graphics.Transform = new Matrix();

 

                        graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

 

                        List<PointF> pointList1 = new List<PointF>();

 

                        for(int x = (int)MINIMUM_WORLD_X; x <= MAXIMUM_WORLD_X; x++)

                        {

                            pointList1.Add(new PointF(x, -0.07f));

                        }

 

                        PointF[] pointArray = pointList1.ToArray();

 

                        transformMatrix.TransformPoints(pointArray);

 

                        using(StringFormat stringFormat = new StringFormat())

                        {

                            stringFormat.Alignment     = StringAlignment.Center;

                            stringFormat.LineAlignment = StringAlignment.Near;

 

                            int index = 0;

 

                            for(int x = (int)MINIMUM_WORLD_X; x <= MAXIMUM_WORLD_X; x++)

                            {

                                graphics.DrawString

                                (

                                    x.ToString(),

                                    font,

                                    Brushes.Black,

                                    pointArray[index++],

                                    stringFormat

                                );

                            }

                        }

                   

                        graphics.Transform = transformMatrix;

 

                        pen.Color = Color.Black;

 

                        graphics.DrawLine(pen, 0, MINIMUM_WORLD_Y, 0, MAXIMUM_WORLD_Y);

 

                        for(int y = (int)MINIMUM_WORLD_Y; y <= MAXIMUM_WORLD_Y; y++)

                        {

                            graphics.DrawLine(pen, -0.2f, y, 0.2f, y);

                            graphics.DrawLine(pen, -0.1f, y + 0.25f, 0.1f, y + 0.25f);

                            graphics.DrawLine(pen, -0.1f, y + 0.50f, 0.1f, y + 0.50f);

                            graphics.DrawLine(pen, -0.1f, y + 0.75f, 0.1f, y + 0.75f);

                        }

 

                        graphics.Transform = new Matrix();

 

                        graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

 

                        List<PointF> pointList2 = new List<PointF>();

 

                        for(float y = 0.25f; y < 1.01; y += 0.25f)

                        {

                            pointList2.Add(new PointF(0.2f, y));

                        }

 

                        pointArray = pointList2.ToArray();

 

                        transformMatrix.TransformPoints(pointArray);

 

                        using(StringFormat stringFormat = new StringFormat())

                        {

                            stringFormat.Alignment     = StringAlignment.Near;

                            stringFormat.LineAlignment = StringAlignment.Center;

 

                            int index = 0;

 

                            foreach(float y in new float[] { 0.25f, 0.5f, 0.75f, 1.0f })

                            {

                                graphics.DrawString

                                (

                                    y.ToString("0.00"),

                                    font,

                                    Brushes.Black,

                                    pointArray[index++],

                                    stringFormat

                                );

                            }

                        }

 

                        graphics.Transform = transformMatrix;

 

                        List<PointF> pointList3 = new List<PointF>();

 

                        float oneOver2PI = (float)(1.0 / (standardDeviation * Math.Sqrt(2 * Math.PI)));

 

                        float deltaX = (MAXIMUM_WORLD_X - MINIMUM_WORLD_X) / canvasPictureBox.ClientSize.Width;

 

                        for(float x = MINIMUM_WORLD_X; x <= MAXIMUM_WORLD_X; x += deltaX)

                        {

                            float y = GetY(x, oneOver2PI, mean, standardDeviation, variance);

 

                            pointList3.Add(new PointF(x, y));

                        }

 

                        pen.Color = Color.Red;

 

                        graphics.DrawLines(pen, pointList3.ToArray());

                    }

                }

 

                this.canvasPictureBox.Image = bitmap;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region Y좌표 구하기 - GetY(x, oneOver2PI, mean, standardDeviation, variance)

 

        /// <summary>

        /// Y좌표 구하기

        /// </summary>

        /// <param name="x">X 좌표</param>

        /// <param name="oneOver2PI">ONE OVER 2 PI</param>

        /// <param name="mean">평균</param>

        /// <param name="standardDeviation">표준 편차</param>

        /// <param name="variance">분산</param>

        /// <returns>Y 좌표</returns>

        private float GetY(float x, float oneOver2PI, float mean, float standardDeviation, float variance)

        {

            return (float)(oneOver2PI * Math.Exp(-(x - mean) * (x - mean) / (2 * variance)));

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요