첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형

TestProject.zip
0.01MB

▶ Line.cs

using System;
using System.Drawing;

namespace TestProject
{
    /// <summary>
    /// 선
    /// </summary>
    public class Line
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 시작 포인트
        /// </summary>
        public Point StartPoint;
        
        /// <summary>
        /// 종료 포인트
        /// </summary>
        public Point EndPoint;

        #endregion

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

        #region 생성자 - Line(startPoint, endPoint)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        public Line(Point startPoint, Point endPoint)
        {
            this.StartPoint = startPoint;
            this.EndPoint   = endPoint;
        }

        #endregion

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

        #region 포인트 배열 구하기 - GetPointArray(count)

        /// <summary>
        /// 포인트 배열 구하기
        /// </summary>
        /// <param name="count">카운트</param>
        /// <returns>포인트 배열</returns>
        public Point[] GetPointArray(int count)
        {
            Point[] pointArray = new Point[count];

            int yDifference = EndPoint.Y - StartPoint.Y, xdiff = EndPoint.X - StartPoint.X;

            double slope = (double)(EndPoint.Y - StartPoint.Y) / (EndPoint.X - StartPoint.X);

            double x, y;

            --count;

            for(double i = 0; i < count; i++)
            {
                y = slope == 0 ? 0                   : yDifference * (i / count);
                x = slope == 0 ? xdiff * (i / count) : y / slope;

                pointArray[(int)i] = new Point((int)Math.Round(x) + StartPoint.X, (int)Math.Round(y) + StartPoint.Y);
            }

            pointArray[count] = EndPoint;

            return pointArray;
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

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

        #region Field

        /// <summary>
        /// 시작 포인트
        /// </summary>
        private Point startPoint = new Point(100, 100);

        /// <summary>
        /// 종료 포인트
        /// </summary>
        private Point endPoint = new Point(600, 400);

        /// <summary>
        /// 포인트 카운트
        /// </summary>
        private int pointCount = 10;

        /// <summary>
        /// 포인트 리스트
        /// </summary>
        private List<Point> pointList;

        /// <summary>
        /// 직선 펜
        /// </summary>
        private Pen linePen = new Pen(Color.Blue, 3);

        #endregion

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

        #region 생성자 - MainForm()

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

            this.pointList = new Line(this.startPoint, this.endPoint).GetPointArray(this.pointCount).ToList();

            Paint += Form_Paint;
        }

        #endregion

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

        #region 폼 페인트시 처리하기 - Form_Paint(sender, e)

        /// <summary>
        /// 폼 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(this.linePen, this.startPoint, this.endPoint);

            foreach(Point point in this.pointList)
            {
                FillPoint(e.Graphics, Brushes.Red, point, 5);
            }
        }

        #endregion

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

        #region 포인트 채우기 - FillPoint(graphics, brush, point, radius)

        /// <summary>
        /// 포인트 채우기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="brush">브러시</param>
        /// <param name="point">포인트</param>
        /// <param name="radius">반경</param>
        public void FillPoint(Graphics graphics, Brush brush, PointF point, float radius)
        {
            graphics.FillEllipse(brush, point.X - radius, point.Y - radius, 2 * radius, 2 * radius);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker

댓글을 달아 주세요