■ 데카르트 좌표계 사용하기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 선 그리기 가능 여부

        /// </summary>

        private bool canDrawLine = false;

 

        /// <summary>

        /// A 값

        /// </summary>

        private int aValue;

 

        /// <summary>

        /// B 값

        /// </summary>

        private int bValue;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.drawButton.Click += drawButton_Click;

            this.drawPanel.Paint  += drawPanel_Paint;

            this.SizeChanged      += Form_SizeChanged;

        }

 

        #endregion

 

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

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

 

        #region 폼 크기 변경시 처리하기 - Form_SizeChanged(sender, e)

 

        /// <summary>

        /// 폼 크기 변경시 처리하기

        /// </summary>

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

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

        private void Form_SizeChanged(object sender, EventArgs e)

        {

            this.drawPanel.Invalidate();

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void drawButton_Click(object sender, EventArgs e)

        {

            #region a 값을 구한다.

 

            string aString = aTextBox.Text.Trim();

            

            if(string.IsNullOrEmpty(aString))

            {

                this.canDrawLine = false;

 

                this.drawPanel.Invalidate();

 

                MessageBox.Show("A 항목에 값을 입력해 주시기 바랍니다.");

 

                return;

            }

 

            int aValue;

 

            if(!int.TryParse(aString, out aValue))

            {

                this.canDrawLine = false;

 

                this.drawPanel.Invalidate();

 

                MessageBox.Show("A 항목에 정수 값을 입력해 주시기 바랍니다.");

 

                return;

            }

 

            #endregion

            #region b 값을 구한다.

 

            string bString = bTextBox.Text.Trim();

            

            if(string.IsNullOrEmpty(bString))

            {

                this.canDrawLine = false;

 

                this.drawPanel.Invalidate();

 

                MessageBox.Show("B 항목에 값을 입력해 주시기 바랍니다.");

 

                return;

            }

 

            int bValue;

 

            if(!int.TryParse(bString, out bValue))

            {

                this.canDrawLine = false;

 

                this.drawPanel.Invalidate();

 

                MessageBox.Show("B 항목에 정수 값을 입력해 주시기 바랍니다.");

 

                return;

            }

 

            #endregion

 

            this.canDrawLine = true;

            this.aValue      = aValue;

            this.bValue      = bValue;

 

            this.drawPanel.Invalidate();

        }

 

        #endregion

        #region 그리기 패널 PAINT 처리하기 - drawPanel_Paint(sender, e)

 

        /// <summary>

        /// 그리기 패널 PAINT 처리하기

        /// </summary>

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

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

        private void drawPanel_Paint(object sender, PaintEventArgs e)

        {

            Panel panel = sender as Panel;

            

            Graphics graphics = e.Graphics;

 

            int halfWidth  = drawPanel.ClientRectangle.Width  / 2;

            int halfHeight = drawPanel.ClientRectangle.Height / 2;

 

            e.Graphics.TranslateTransform(halfWidth, halfHeight);

 

            e.Graphics.ScaleTransform(1, -1);

 

            // 수평선을 그린다.

            graphics.DrawLine(Pens.DarkGray, -halfWidth, 0, halfWidth, 0);

 

            graphics.DrawLine(Pens.DarkGray, -halfWidth, 0, -halfWidth + 5,  5);

            graphics.DrawLine(Pens.DarkGray, -halfWidth, 0, -halfWidth + 5, -5);

 

            graphics.DrawLine(Pens.DarkGray, halfWidth, 0, halfWidth - 5,  5);

            graphics.DrawLine(Pens.DarkGray, halfWidth, 0, halfWidth - 5, -5);

 

            // 수평선 눈금을 그린다.

            for(int i = 0; i < halfWidth; i += 10)

            {

                graphics.DrawLine(Pens.DarkGray,  i, 3,  i, -3);

                graphics.DrawLine(Pens.DarkGray, -i, 3, -i, -3);

            }

 

            // 수직선을 그린다.

            graphics.DrawLine(Pens.DarkGray, 0, halfHeight, 0, -halfHeight);

            

            graphics.DrawLine(Pens.DarkGray, 0, halfHeight, -5, halfHeight - 5);

            graphics.DrawLine(Pens.DarkGray, 0, halfHeight,  5, halfHeight - 5);

 

            graphics.DrawLine(Pens.DarkGray, 0, -halfHeight, -5, -halfHeight + 5);

            graphics.DrawLine(Pens.DarkGray, 0, -halfHeight,  5, -halfHeight + 5);

 

            // 수직선 눈금을 그린다.

            for(int i = 0; i < halfHeight; i += 10)

            {

                graphics.DrawLine(Pens.DarkGray, -3,  i, 3,  i);

                graphics.DrawLine(Pens.DarkGray, -3, -i, 3, -i);

            }

 

            // 1차 방정식에 따라 선을 그린다.

            if(this.canDrawLine)

            {

                int x1 = -halfWidth;

                int y1 = this.aValue * x1 + this.bValue;

 

                int x2 = halfWidth;

                int y2 = this.aValue * x2 + this.bValue;

 

                graphics.DrawLine(Pens.Red, x1, y1, x2, y2);

 

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요