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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="1차 방정식 직선을 기준으로 대칭 포인트 구하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Margin="10">
        <Border
            BorderBrush="Black"
            BorderThickness="1">
            <Canvas Name="canvas" />
        </Border>
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

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

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

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

            Loaded += Window_Loaded;
        }

        #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)
        {
            double slope      = 2 / 3d;
            double yIntercept = 3;

            Point lineStartPoint = new Point();

            lineStartPoint.X = 0;
            lineStartPoint.Y = slope * lineStartPoint.X + yIntercept;

            Point lineEndPoint = new Point();

            lineEndPoint.X = this.canvas.ActualWidth;
            lineEndPoint.Y = slope * lineEndPoint.X + yIntercept;

            Line line = new Line();

            line.Stroke          = Brushes.Gray;
            line.StrokeThickness = 3;

            line.X1 = lineStartPoint.X;
            line.Y1 = lineStartPoint.Y;
            line.X2 = lineEndPoint.X;
            line.Y2 = lineEndPoint.Y;

            this.canvas.Children.Add(line);

            Point sourcePoint = new Point();

            sourcePoint.X = 200;
            sourcePoint.Y = 400;

            Ellipse sourceEllipse = new Ellipse();

            sourceEllipse.Fill = Brushes.Blue;

            sourceEllipse.Width  = 8;
            sourceEllipse.Height = 8;

            Canvas.SetLeft(sourceEllipse, sourcePoint.X - 4);
            Canvas.SetTop (sourceEllipse, sourcePoint.Y - 4);

            this.canvas.Children.Add(sourceEllipse);

            Point symmetryPoint = GetSymmetryPoint(slope, yIntercept, sourcePoint);

            Ellipse symmetryEllipse = new Ellipse();

            symmetryEllipse.Fill = Brushes.Red;

            symmetryEllipse.Width  = 8;
            symmetryEllipse.Height = 8;

            Canvas.SetLeft(symmetryEllipse, symmetryPoint.X - 4);
            Canvas.SetTop (symmetryEllipse, symmetryPoint.Y - 4);

            this.canvas.Children.Add(symmetryEllipse);
        }

        #endregion

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

        #region 대칭 포인트 구하기 - GetSymmetryPoint(slope, yIntercept, sourcePoint)

        /// <summary>
        /// 대칭 포인트 구하기
        /// </summary>
        /// <param name="slope">1차 방정식 선 기울기</param>
        /// <param name="yIntercept">1차 방정식 선 Y 절편</param>
        /// <param name="sourcePoint">소스 포인트</param>
        /// <returns>대칭 포인트</returns>
        private Point GetSymmetryPoint(double slope, double yIntercept, Point sourcePoint)
        {
            double slopeSquare = slope * slope;

            double targetX = ((1 - slopeSquare) * sourcePoint.X + 2 * slope * sourcePoint.Y - 2 * slope * yIntercept) / (1 + slopeSquare);
            double targetY = (2 * slope * sourcePoint.X - (1 - slopeSquare) * sourcePoint.Y + 2 * yIntercept) / (1 + slopeSquare);

            return new Point(targetX, targetY);
        }

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

댓글을 달아 주세요