2013-04-23 1 views
1

타원 기하학이 있습니다. C#을 사용하여 원의 중심에서 중심으로 그려진 선으로 타원의 반지름을 표시하고 싶습니다. 이것을 어떻게 할 수 있습니까?반경의 타원 표시 - WPF

참고 : 타원의 중심과 반경은 고정되어 있지 않으며 사용자가 정의합니다.

enter image description here

+3

당신이 _anything_ 지금까지 시도 했습니까? 먼저 [FAQ] 및 [ask] –

+0

을 읽고 xaml 또는 코드를 사용 하시겠습니까? – David

+0

네, 시도해 보았습니다. 죄송합니다. 코드를 여기에 넣는 것을 잊어 버렸습니다. David - 코드 – Sangeetha

답변

5

는 알려진 센터와 반경 타원가 있다고 가정 :이 일을 여러 가지 방법이 있습니다

 Path path = new Path(); 
     EllipseGeometry eg = new EllipseGeometry(); 
     eg.Center = new Point(left + side/2, top + side/2); 
     eg.RadiusX = side/2; 
     eg.RadiusY = side/2; 
     path.Data = eg; 
     paths.Add(path); 
     canvas1.Children.Add(paths[paths.Count - 1]); 
     . 
     . 
     path = new Path(); 
     borderColor.Color = Colors.Red; 
     path.Stroke = borderColor; 
     path.StrokeThickness = 2; 
     LineGeometry r = new LineGeometry(); 
     r.StartPoint = eg.Center; 
     r.EndPoint = new Point(eg.Center.X + eg.RadiusX, eg.Center.Y); 
     path.Data = r; 
     paths.Add(path); 
     canvas1.Children.Add(paths[paths.Count - 1]); 

enter image description here

1

입니다. 여기에는 귀하의 필요를 충족시키지 않을 수도있는 하나가 있습니다. 그것은 단지 사용자 정의 컨트롤입니다. 원의 반경은 사용자 정의 컨트롤의 크기에 따라 다르며 컨트롤의 크기가 균일 해집니다. 사용자 정의 컨트롤의 위치는 사용자가 결정합니다. 내부 선의 각도는 묶을 수 있습니다.

using System; 
using System.ComponentModel; 
using System.Windows; 
using System.Windows.Controls; 

namespace TestWPF 
{ 
    public partial class CircleTest : UserControl, INotifyPropertyChanged 
    { 
     public CircleTest() 
     { 
      InitializeComponent(); 

      this.SizeChanged += CircleTest_SizeChanged; 
     } 

     void CircleTest_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e) 
     { 
      double radius; 
      if (ActualHeight < ActualWidth) 
      { 
       Width = ActualHeight; 
       _center = new Point(Width/2, ActualHeight/2); 
       radius = ActualHeight/2; 
      } 
      else 
      { 
       Height = ActualWidth; 
       _center = new Point(ActualWidth/2, Height/2); 
       radius = ActualWidth/2; 
      } 

      _endPoint = new Point(Center.X, Center.Y - radius); 

      NotifyOfPropertyChange("Center"); 
      NotifyOfPropertyChange("EndPoint"); 
     } 

     public double Angle 
     { 
      get { return (double)GetValue(AngleProperty); } 
      set { SetValue(AngleProperty, value); } 
     } 
     public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(CircleTest), new PropertyMetadata(45.0)); 

     private Point _center; 
     public Point Center 
     { 
      get { return _center; } 
     } 

     private Point _endPoint; 
     public Point EndPoint 
     { 
      get { return _endPoint; } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void NotifyOfPropertyChange(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

뒤에

사용자 제어 XAML

<UserControl x:Class="TestWPF.CircleTest" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Foreground="Blue" Background="White" 
      x:Name="CT" SnapsToDevicePixels="True"> 
    <Grid> 
     <Ellipse Stroke="{Binding Foreground, ElementName=CT}" Fill="{Binding Background, ElementName=CT}" /> 
     <Line X1="{Binding Center.X, ElementName=CT}" X2="{Binding EndPoint.X, ElementName=CT}" Y1="{Binding Center.Y, ElementName=CT}" Y2="{Binding EndPoint.Y, ElementName=CT}" 
       Stroke="{Binding Foreground, ElementName=CT}"> 
      <Line.RenderTransform> 
       <RotateTransform Angle="{Binding Angle, ElementName=CT}" CenterX="{Binding Center.X, ElementName=CT}" CenterY="{Binding Center.Y, ElementName=CT}" /> 
      </Line.RenderTransform> 
     </Line> 
     <TextBlock Text="{Binding Angle, ElementName=CT, StringFormat='N2'}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="3" /> 
    </Grid> 
</UserControl> 

사용자 제어 코드는이처럼 사용하십시오 :

<Window x:Class="TestWPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:test="clr-namespace:TestWPF" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <test:CircleTest Width="200" Height="200" Foreground="Purple" Angle="{Binding Value, ElementName=SL}" /> 
     <Slider x:Name="SL" Minimum="0" Maximum="360" VerticalAlignment="Bottom" Margin="20" /> 
    </Grid> 
</Window>