첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    Name="window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="두 포인트 간 각도 구하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Border
        Margin="10"
        BorderBrush="Black"
        BorderThickness="1"
        ClipToBounds="True">
        <Grid>
            <Canvas Name="canvas">
                <Canvas.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform
                            ScaleX="1"
                            ScaleY="-1" />
                        <TranslateTransform
                            X="{Binding CenterX, ElementName=window}"
                            Y="{Binding CenterY, ElementName=window}" />
                    </TransformGroup>
                </Canvas.RenderTransform>
            </Canvas>
            <TextBlock Name="textBloack"
                Margin="10"
                HorizontalAlignment="Left"
                VerticalAlignment="Top" />
        </Grid>
    </Border>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public
        
        #region 중심 X 속성 - CenterXProperty

        /// <summary>
        /// 중심 X 속성
        /// </summary>
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register
        (
            "CenterX",
            typeof(double),
            typeof(MainWindow),
            new UIPropertyMetadata
            (
                0d,
                null,
                null,
                true
            )
        );

        #endregion
        #region 중심 Y 속성 - CenterYProperty

        /// <summary>
        /// 중심 Y 속성
        /// </summary>
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register
        (
            "CenterY",
            typeof(double),
            typeof(MainWindow),
            new UIPropertyMetadata
            (
                0d,
                null,
                null,
                true
            )
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 중심 X - CenterX

        /// <summary>
        /// 중심 X
        /// </summary>
        public double CenterX
        {
            get
            {
                return (double)GetValue(CenterXProperty);
            }
            set
            {
                SetValue(CenterXProperty, value);
            }
        }

        #endregion
        #region 중심 Y - CenterY

        /// <summary>
        /// 중심 Y
        /// </summary>
        public double CenterY
        {
            get
            {
                return (double)GetValue(CenterYProperty);
            }
            set
            {
                SetValue(CenterYProperty, value);
            }
        }

        #endregion

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

        #region 생성자 - MainWindow()

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

            Loaded      += Window_Loaded;
            SizeChanged += Window_SizeChanged;
        }

        #endregion

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

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CenterX = this.canvas.ActualWidth  * 0.5;
            CenterY = this.canvas.ActualHeight * 0.5;

            DrawLine(new Point(-10000, 0    ), new Point(10000,      0), Brushes.Gray);
            DrawLine(new Point(     0, 10000), new Point(    0, -10000), Brushes.Gray);

            Point startPoint = new Point(  0,  0);
            Point endPoint   = new Point(150, 50);

            DrawLine(startPoint, endPoint, Brushes.Black);

            DrawEllipse(startPoint, 5, Brushes.Black);
            DrawEllipse(endPoint  , 5, Brushes.Black);

            double angle = GetAngle(startPoint, endPoint);

            this.textBloack.Text = $"두 지점 간 각도 : {angle}";
        }

        #endregion
        #region 윈도우 크기 변경시 처리하기 - Window_SizeChanged(sender, e)

        /// <summary>
        /// 윈도우 크기 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            CenterX = this.canvas.ActualWidth  * 0.5;
            CenterY = this.canvas.ActualHeight * 0.5;
        }

        #endregion

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

        #region 선 그리기 - DrawLine(point1, point2, brush)

        /// <summary>
        /// 선 그리기
        /// </summary>
        /// <param name="point1">포인트 1</param>
        /// <param name="point2">포인트 2</param>
        /// <param name="brush">브러시</param>
        /// <returns>선</returns>
        private Line DrawLine(Point point1, Point point2, Brush brush)
        {
            Line line = new Line();

            line.X1              = point1.X;
            line.Y1              = point1.Y;
            line.X2              = point2.X;
            line.Y2              = point2.Y;
            line.Stroke          = brush;
            line.StrokeThickness = 1;

            this.canvas.Children.Add(line);

            return line;
        }

        #endregion
        #region 타원 그리기 - DrawEllipse(centerPoint, radius, brush)

        /// <summary>
        /// 타원 그리기
        /// </summary>
        /// <param name="centerPoint">중심 포인트</param>
        /// <param name="radius">반경</param>
        /// <param name="brush">브러시</param>
        /// <returns>타원</returns>
        private Ellipse DrawEllipse(Point centerPoint, double radius, Brush brush)
        {
            Ellipse ellipse = new Ellipse();

            ellipse.Width  = radius * 2;
            ellipse.Height = radius * 2;
            ellipse.Fill   = brush;

            Canvas.SetLeft(ellipse, centerPoint.X - radius);
            Canvas.SetTop (ellipse, centerPoint.Y - radius);

            this.canvas.Children.Add(ellipse);

            return ellipse;
        }

        #endregion
        #region 각도 구하기 - GetAngle(startPoint, endPoint)

        /// <summary>
        /// 각도 구하기
        /// </summary>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        /// <returns>각도</returns>
        private double GetAngle(Point startPoint, Point endPoint)
        {
            double radian = Math.Atan2((endPoint.Y - startPoint.Y), (endPoint.X - startPoint.X));
            
            return (radian * (180d / Math.PI) + 360d) % 360d;
        }

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

댓글을 달아 주세요