天天看點

WPF太陽、地球、月球運動軌迹模拟

原文: WPF太陽、地球、月球運動軌迹模拟

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/yangyisen0713/article/details/18216803

WPF模拟太陽。月球、地球三者運動軌迹的模拟,現在還沒有加上太陽自傳的動畫,有興趣的可以加上。

主要是利用EllipseGeometry實作路徑的繪制

xaml代碼如下:

<Window
    x:Class="WpfApp11.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="地球、月球、太陽運動模拟"
    Width="1366"
    Height="768"
    WindowStartupLocation="CenterScreen"> 
    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="earth.jpg" />
        </Grid.Background>
        <Ellipse
            Name="ellipse3"
            Width="150"
            Height="150"
            Margin="619,321,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            ToolTip="太陽">
            <Ellipse.Fill>
                <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                    <GradientStop Offset="0.246" Color="#FFFFCA00" />
                    <GradientStop Offset="0.967" Color="#FFFF0034" />
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>
        <!--  通過使用EllipseGeometry實作橢圓路徑的繪制  -->
        <Path
            Margin="308,136,120,81"
            RenderTransformOrigin="0.415,0.498"
            Stroke="#FFFF7900"
            StrokeThickness="5">
            <Path.Data>
                <EllipseGeometry
                    x:Name="e1"
                    Center="400 250"
                    RadiusX="400"
                    RadiusY="250" />
            </Path.Data>
        </Path>
        <Grid
            Name="grid1"
            Width="484"
            Height="352"
            Margin="30,-12,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top">
            <Grid.RenderTransform>
                <MatrixTransform x:Name="grid" />
            </Grid.RenderTransform>
            <!--  Grid觸發器  -->
            <Grid.Triggers>
                <EventTrigger RoutedEvent="Page.Loaded">
                    <BeginStoryboard>
                        <Storyboard x:Name="sb1" RepeatBehavior="Forever">
                            <MatrixAnimationUsingPath
                                x:Name="ma1"
                                Storyboard.TargetName="grid"
                                Storyboard.TargetProperty="Matrix"
                                Duration="0:1:0" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Grid.Triggers>
            <Ellipse
                Name="ellipse1"
                Width="100"
                Height="100"
                Margin="227,94,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                ToolTip="地球">
                <Ellipse.Fill>
                    <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                        <GradientStop Offset="0" Color="#FFE0DFDD" />
                        <GradientStop Offset="0.975" Color="#FF0035FF" />
                    </LinearGradientBrush>
                </Ellipse.Fill>
                <Ellipse.RenderTransform>
                    <TransformGroup>
                        <MatrixTransform x:Name="earth" />
                    </TransformGroup>
                </Ellipse.RenderTransform>
                <Ellipse.Triggers>
                    <EventTrigger RoutedEvent="Page.Loaded" />
                </Ellipse.Triggers>
            </Ellipse>
            <Ellipse
                Name="ellipse2"
                Width="50"
                Height="50"
                Margin="228,40,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                ToolTip="月球">
                <Ellipse.Fill>
                    <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                        <GradientStop Offset="0" Color="White" />
                        <GradientStop Offset="0.943" Color="#FFDDD2BE" />
                    </LinearGradientBrush>
                </Ellipse.Fill>
                <Ellipse.RenderTransform>
                    <TransformGroup>
                        <MatrixTransform x:Name="moon" />
                    </TransformGroup>
                </Ellipse.RenderTransform>
                <Ellipse.Triggers>
                    <EventTrigger RoutedEvent="Page.Loaded">
                        <BeginStoryboard>
                            <Storyboard x:Name="sb2" RepeatBehavior="Forever">
                                <MatrixAnimationUsingPath
                                    x:Name="ma2"
                                    Storyboard.TargetName="moon"
                                    Storyboard.TargetProperty="Matrix"
                                    Duration="0:0:30" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Ellipse.Triggers>
            </Ellipse>
            <Path
                Width="122"
                Height="200"
                Margin="262.522,59,0,93"
                HorizontalAlignment="Left"
                Stroke="#FF00FF40"
                StrokeThickness="5">
                <Path.Data>
                    <EllipseGeometry
                        x:Name="ellipseGeometry1"
                        Center="50 100"
                        RadiusX="50"
                        RadiusY="100">
                        <EllipseGeometry.Transform>
                            <SkewTransform AngleY="-20" />
                        </EllipseGeometry.Transform>
                    </EllipseGeometry>
                </Path.Data>
            </Path>
        </Grid>
        <TextBox
            Width="448"
            Height="110"
            Margin="891,7,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Background="{x:Null}"
            BorderBrush="#FF0012FF"
            BorderThickness="0"
            Foreground="White"
            Text="地球饒太陽公轉,月球饒地球公轉。太陽、地球、月球都在自轉太陽是太陽系的主宰,是恒星。地球是太陽系的一顆行星,月球是地球的一顆天然衛星。地球繞着太陽公轉,月球繞着地球公轉。太陽不可能位于地球和月球之間。"
            TextWrapping="Wrap" />
    </Grid>
</Window>       

最後效果如圖:

WPF太陽、地球、月球運動軌迹模拟
public MainWindow()
        {
            InitializeComponent();
            ma2.PathGeometry = ellipseGeometry1.GetFlattenedPathGeometry(); 
            ma1.PathGeometry = e1.GetFlattenedPathGeometry(); 
        }