Опубликован: 01.11.2011 | Доступ: свободный | Студентов: 1424 / 63 | Оценка: 3.84 / 3.44 | Длительность: 15:38:00
Специальности: Программист
Лекция 7:

Знакомство с Silverlight

Упражнение 10.2. Определение видимости спутников ГЛОНАСС (WPF)

ГЛОНАСС- и GPS-навигаторы могут уверенно принимать сигналы только со спутников, расположенных выше определенного угла к горизонту - \alpha. Попытаемся определить критерий видимости спутников от наблюдателя, расположенного на поверхности Земли на основании радиуса Земли, высоты орбиты спутников ГЛОНАСС и угла к горизонту \alpha. Ситуацию можно описать графически:


Требуется определить расстояние от наблюдателя до точки пересечения прямой с углом \alpha к линии горизонта и орбитой спутника.

Уравнение прямой: y=R+x\times\tg

Уравнение окружности: (R+H)^2=x^2+y^2

Найдем точку пересечения прямой и окружности:

y^2=(R+H)^2-x^2=R^2+2R\tg\alpha x+x^2(tg\alpha)^2\\ x^2(1+(\tg\alpha)^2)+ 2R\tg\alpha x-2RH-H^2=0\\ a=1+(\tg\alpha)^2\\ b=2R\tg\alpha\\ c=-H(2R+H)

При маске горизонта \alpha =10^0, радиусе Земли R=6378136\text{ м} и высоте орбиты спутников ГЛОНАСС 19100000 м находим корни квадратного уравнения:

x_1= -25407331,34\\ x_2=23225880,35

Отрицательный корень отбрасываем, принимаем катет треугольник m=x_2. Находим гипотенузу

n=\frac m{\cos(\alpha)} =23584177\text{ м}
.

Иными словами, при круговой орбите спутников и маске горизонта 10^0 от наблюдателя будут видны спутники, расположенные не далее 23584177 м.

Для решения задачи создадим проект wpf Lecture_7_2.

Код MainWindow.xaml:

<Window x:Class="Lecture_7_2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="bttn" Click="bttn_Click" Margin="0,0,400,280" Content="Нажми меня!"/>
    </Grid>
</Window>
    

Код MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lecture_7_2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
                private void bttn_Click(object sender, RoutedEventArgs e)
        {
            double R, H, alpha;
            string str;
            R = 6378136;
            H = 19100000;
            alpha = 10 * System.Math.PI / 180;
            double a, b, c, d, x1, x2;
            a = 1 + System.Math.Pow((System.Math.Tan(alpha)), 2);
            b = 2 * R * Math.Tan(alpha);
            c = -H * (2 * R + H);
            d = System.Math.Pow(b, 2) - 4 * a * c;

            double distance;

            if (d < 0) { str = "Действительных корней нет!"; }
            else
            {
                x1 = (-b - System.Math.Sqrt(d)) / (2 * a);
                x2 = (-b + System.Math.Sqrt(d)) / (2 * a);

                if (x1 < 0) { distance = x2 / System.Math.Cos(alpha); }
                else { distance = x1 / System.Math.Cos(alpha); }

                str = "x1 = " + x1 + "\nx2 = " + x2 + "\nРасстояние = " + distance + " м";
            }

            MessageBox.Show(str);
        }
    }
}
    
Листинг .

Получаем следующий результат:


Упражнение 10.3. Создание программы-переводчика

В следующем примере мы разработаем программу-переводчик. Создаем новый проект Lecture_7_3.

MainWindow.xaml:

<Window x:Class="Lecture_7_3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="grid1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBox Name="txtQuestion" VerticalAlignment="Stretch" 
HorizontalAlignment="Stretch" Margin="10,10,13,10" TextWrapping="Wrap" 
Grid.Row="0" FontFamily="Verdana" 
 FontSize="24" Foreground="Green">
            Введите слово по-английски
        </TextBox>

        <Button VerticalAlignment="Top" HorizontalAlignment="Left" 
Margin="10,0,0,20" Width="127" Height="23" 
Name="cmdAnswer" Click="cmdTranslate_Click" Grid.Row="1">
            Перевести
        </Button>

        <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
Margin="10,10,13,10" Name="txtAnswer" TextWrapping="Wrap" 
IsReadOnly="True" FontFamily="Verdana" FontSize="24" 
Foreground="Green" Grid.Row="2">
            Здесь будет перевод
        </TextBox>
    </Grid>
</Window>
    

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lecture_7_3
{
    class AnswerGenerator
    {
        string str;
        public string getTranslate(string Question)
        {
            switch (Question)
            {
                case "Apple": str = "Яблоко"; break;
                case "Lemon": str = "Лимон"; break;
                case "Orange": str = "Апельсин"; break;
                case "Mellon": str = "Дыня"; break;
                case "Water": str = "Вода"; break;
                case "Bred": str = "Хлеб"; break;
                case "Butter": str = "Масло"; break;
            }
            return str;
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void cmdTranslate_Click(object sender, RoutedEventArgs e)
        {
            AnswerGenerator generator = new AnswerGenerator();
            txtAnswer.Text = generator.getTranslate(txtQuestion.Text);
        }
    }
}
    
Листинг .