첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 주기
        /// </summary>
        private const int PERIOD = 24;

        /// <summary>
        /// 색상 배열
        /// </summary>
        private Color[] colorArray;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 이벤트를 설정한다.

            Load                             += Form_Load;
            Resize                           += Form_Resize;
            this.drawButton.Click            += drawButton_Click;
            this.rightCanvasPictureBox.Paint += rightCanvasPictureBox_Paint;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            ResizeRedraw = true;

            this.colorArray = new Color[] 
            {
                Color.Pink,
                Color.Red,
                Color.Orange,
                Color.Yellow,
                Color.Lime,
                Color.Cyan,
                Color.Blue,
                Color.Violet,
                Color.Pink,
                Color.Red,
                Color.Orange,
                Color.Yellow,
                Color.Lime,
                Color.Cyan,
                Color.Blue,
                Color.Violet,
                Color.Pink,
                Color.Red,
                Color.Orange,
                Color.Yellow,
                Color.Lime,
                Color.Cyan,
                Color.Blue,
                Color.Violet
            };

            Draw();
        }

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

        /// <summary>
        /// 폼 크기 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Resize(object sender, EventArgs e)
        {
            Draw();
        }

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

        /// <summary>
        /// 그리기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void drawButton_Click(object sender, EventArgs e)
        {
            Draw();
        }

        #endregion
        #region 오른쪽 캔버스 픽처 박스 페인트시 처리하기 - rightCanvasPictureBox_Paint(sender, e)

        /// <summary>
        /// 오른쪽 캔버스 픽처 박스 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void rightCanvasPictureBox_Paint(object sender, PaintEventArgs e)
        {
            DrawButterfly
            (
                e.Graphics,
                rightCanvasPictureBox.ClientSize.Width,
                rightCanvasPictureBox.ClientSize.Height
            );
        }

        #endregion

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

        #region 색상 구하기 - GetColor(t)

        /// <summary>
        /// 색상 구하기
        /// </summary>
        /// <param name="t">T</param>
        /// <returns>색상</returns>
        private Color GetColor(double t)
        {
            return this.colorArray[(int)(t / Math.PI)];
        }

        #endregion
        #region 나비 그리기 - DrawButterfly(graphics, width, height)

        /// <summary>
        /// 나비 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        private void DrawButterfly(Graphics graphics, int width, int height)
        {
            graphics.SmoothingMode = SmoothingMode.AntiAlias;

            graphics.Clear(Color.Black);

            RectangleF worldRectangle = new RectangleF(-4.0f, -4.4f, 8.0f, 7.3f);

            float xCenter = (worldRectangle.Left + worldRectangle.Right ) / 2;
            float yCenter = (worldRectangle.Top  + worldRectangle.Bottom) / 2;

            graphics.TranslateTransform(-xCenter, -yCenter);

            float scale = Math.Min
            (
                width  / worldRectangle.Width,
                height / worldRectangle.Height
            );
            
            graphics.ScaleTransform(scale, scale, MatrixOrder.Append);

            graphics.TranslateTransform(width / 2, height / 2, MatrixOrder.Append);

            PointF pt0;
            PointF pt1;

            double t = 0;

            double expression = Math.Exp(Math.Cos(t)) - 2 * Math.Cos(4 * t) - Math.Pow(Math.Sin(t / 12), 5);

            pt1 = new PointF((float)(Math.Sin(t) * expression), (float)(-Math.Cos(t) * expression));

            using(Pen pen = new Pen(Color.Blue, 0))
            {
                const long lineCount = 5000;

                for(long i = 0; i < lineCount; i++)
                {
                    t = i * PERIOD * Math.PI / lineCount;

                    expression = Math.Exp(Math.Cos(t)) - 2 * Math.Cos(4 * t) - Math.Pow(Math.Sin(t / 12), 5);

                    pt0 = pt1;

                    pt1 = new PointF
                    (
                        (float)( Math.Sin(t) * expression),
                        (float)(-Math.Cos(t) * expression)
                    );

                    pen.Color = GetColor(t);

                    graphics.DrawLine(pen, pt0, pt1);
                }
            }
        }

        #endregion
        #region 그리기 - Draw()

        /// <summary>
        /// 그리기
        /// </summary>
        private void Draw()
        {
            this.leftCanvasPictureBox.Image = null;

            this.leftCanvasPictureBox.Refresh();

            this.rightCanvasPictureBox.Refresh();

            int bitmapWidth  = this.rightCanvasPictureBox.ClientSize.Width;
            int bitmapHeight = this.rightCanvasPictureBox.ClientSize.Height;

            Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight);

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                DrawButterfly(graphics, bitmapWidth, bitmapHeight);
            }

            this.leftCanvasPictureBox.Image = bitmap;

            this.leftCanvasPictureBox.Refresh();

            DrawButterfly(this.rightCanvasPictureBox.CreateGraphics(), bitmapWidth, bitmapHeight);
        }

        #endregion
    }
}
728x90
그리드형(광고전용)
Posted by icodebroker
,