Россия, г. Ейск |
Знакомство с Silverlight
Упражнение 10.2. Определение видимости спутников ГЛОНАСС (WPF)
ГЛОНАСС- и GPS-навигаторы могут уверенно принимать сигналы только со спутников, расположенных выше определенного угла к горизонту - . Попытаемся определить критерий видимости спутников от наблюдателя, расположенного на поверхности Земли на основании радиуса Земли, высоты орбиты спутников ГЛОНАСС и угла к горизонту
. Ситуацию можно описать графически:
Требуется определить расстояние от наблюдателя до точки пересечения прямой с углом к линии горизонта и орбитой спутника.
Уравнение окружности:
Найдем точку пересечения прямой и окружности:

При маске горизонта , радиусе Земли
и высоте орбиты спутников ГЛОНАСС 19100000 м находим корни квадратного уравнения:

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

Иными словами, при круговой орбите спутников и маске горизонта от наблюдателя будут видны спутники, расположенные не далее 23584177 м.
Для решения задачи создадим проект wpf Lecture_7_2.
<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>
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.
<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>
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); } } }Листинг .