Advertisement
CherMi

Clock v0.02

Dec 16th, 2020 (edited)
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Windows.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Animation;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18.  
  19. namespace Clock
  20. {
  21.     /// <summary>
  22.     /// Interaction logic for MainWindow.xaml
  23.     /// </summary>
  24.  
  25.  
  26.     public partial class MainWindow : Window
  27.     {
  28.         private int millisecondInterval;
  29.         private double scaleK;
  30.         private static MyClock clock;
  31.         private static Oscillator oscillator;
  32.  
  33.         public MainWindow()
  34.         {
  35.             InitializeComponent();
  36.  
  37.             secondsTextBox.PreviewTextInput += ValidateNumbers;
  38.             secondsTextBox.TextChanged += ValidateLessThan60;
  39.  
  40.             clock = new MyClock();
  41.             oscillator = new Oscillator(1d); //TODO: сделать переменным
  42.            
  43.             scaleK = 250d / 4d; //TODO: должно зависеть от переменных
  44.             millisecondInterval = 10; //TODO: Должно зависеть от маятника
  45.             DispatcherTimer dispatcherTimer = new DispatcherTimer();
  46.             dispatcherTimer.Interval = TimeSpan.FromMilliseconds(millisecondInterval);
  47.             dispatcherTimer.Tick += ExecuteEveryTick;
  48.             dispatcherTimer.Start();
  49.         }
  50.  
  51.         private void ValidateNumbers(object sender, TextCompositionEventArgs e)
  52.         {
  53.             for(int i = 0; i < e.Text.Length; i++)
  54.             {
  55.                 if(!Char.IsDigit(e.Text[i]))
  56.                 {
  57.                     MessageBox.Show("Неверный формат ввода. В данном поле разрешено вводить только цифры.", "Ошибка формата ввода");
  58.                     e.Handled = true;
  59.                 }
  60.             }
  61.         }
  62.  
  63.         private void ValidateLessThan60(object sender, TextChangedEventArgs e)
  64.         {
  65.             TextBox textBox = (TextBox)sender;
  66.             int value = 0;
  67.             try
  68.             {
  69.                 value = Int32.Parse(textBox.Text);
  70.             }
  71.             catch(Exception ex)
  72.             {
  73.                 textBox.Text = "0";
  74.                 MessageBox.Show("Неверный формат ввода. В данном поле разрешено вводить только числа от 0 до 59 включительно.", "Ошибка формата ввода");
  75.                 e.Handled = true;
  76.             }
  77.  
  78.             if (value < 0 && value >= 60)
  79.             {
  80.                 textBox.Text = "0";
  81.                 MessageBox.Show("Неверный формат ввода. В данном поле разрешено вводить только числа от 0 до 59 включительно.", "Ошибка формата ввода");
  82.                 e.Handled = true;
  83.             }
  84.         }
  85.  
  86.         private void ExecuteEveryTick(object sender, EventArgs e)
  87.         {
  88.             clock.secAngle += (6d / 1000) * (millisecondInterval); //Секундная стрелка проходит 6 градусов за 1000 мс
  89.             clock.minAngle += (0.1d / 1000) * millisecondInterval; //Минутная стрелка проходит 0.1 градусов за 1000 мс
  90.             clock.hAngle += (30d / 3600) * millisecondInterval / 1000; //Часовая стрелка проходит (30 / 3600) градусов за 1000 мс
  91.  
  92.             RotateTransform secondArrowRotateTransform = new RotateTransform(clock.secAngle);
  93.             RotateTransform minuteArrowRotateTransform = new RotateTransform(clock.minAngle);
  94.             RotateTransform hourArrowRotateTransform = new RotateTransform(clock.hAngle);
  95.  
  96.             secondArrow.RenderTransform = secondArrowRotateTransform;
  97.             minuteArrow.RenderTransform = minuteArrowRotateTransform;
  98.             hourArrow.RenderTransform = hourArrowRotateTransform;
  99.  
  100.  
  101.             oscillator.time += millisecondInterval;
  102.             oscillator.x = oscillator.CalcX() * scaleK;
  103.             oscillator.y = oscillator.CalcY() * scaleK;
  104.  
  105.             TranslateTransform oscillatorTranslateTransform = new TranslateTransform();
  106.             oscillatorTranslateTransform.X = oscillator.x;
  107.             oscillatorTranslateTransform.Y = oscillator.y;
  108.             oscillatorWeight.RenderTransform = oscillatorTranslateTransform;
  109.  
  110.             oscillatorRope.X2 = 375 + oscillator.x;
  111.             oscillatorRope.Y2 = 250 + oscillator.y;
  112.         }
  113.     }
  114.  
  115.     public class MyClock
  116.     {
  117.         private int sec;
  118.         private int min;
  119.         private int h;
  120.         public double secAngle;
  121.         public double minAngle;
  122.         public double hAngle;
  123.  
  124.  
  125.         public MyClock()
  126.         {
  127.             sec = 0;
  128.             min = 0;
  129.             h = 0;
  130.             secAngle = 0;
  131.             minAngle = 0;
  132.             hAngle = 0;
  133.         }
  134.  
  135.         public MyClock(int sec, int min, int h)
  136.         {
  137.             Second = sec;
  138.             Minute = min;
  139.             Hour = h;
  140.             secAngle = sec/6;
  141.             minAngle = min/6;
  142.             hAngle = h*30;
  143.         }
  144.  
  145.         public int Second
  146.         {
  147.             get
  148.             {
  149.                 return sec;
  150.             }
  151.             set
  152.             {
  153.                 sec = value % 60;
  154.             }
  155.         }
  156.  
  157.         public int Minute
  158.         {
  159.             get
  160.             {
  161.                 return min;
  162.             }
  163.             set
  164.             {
  165.                 min = value % 60;
  166.             }
  167.         }
  168.  
  169.         public int Hour
  170.         {
  171.             get
  172.             {
  173.                 return h;
  174.             }
  175.             set
  176.             {
  177.                 h = value % 12;
  178.             }
  179.         }
  180.     }
  181.  
  182.     public class Oscillator
  183.     {
  184.         public double angle = 30; //Угол отклонения маятника от вертикальной оси в грудасах
  185.         public double r; //Радиус колеса, образующего циклоиду
  186.         public double L; //Длина нити; L = 4 * r;
  187.         public double T; //Период колебаний
  188.         public double phi; //Фаза колебаний
  189.         public double decelerationK = 0; //Коэффициент затухания
  190.         public double Amplitude;
  191.         public int time;
  192.         public double x, y; //Координаты маятника
  193.  
  194.         public Oscillator(double r)
  195.         {
  196.             this.time = 0;
  197.             this.r = r; //[м]
  198.             this.L = 4 * r; //[м]
  199.             this.x = 0; //[м]
  200.             this.y = 0; //[м]
  201.  
  202.             this.Amplitude = Math.Sin(GradToRadian(angle)) * L;
  203.             const double g = 9.81;
  204.             this.T = 2 * Math.PI * Math.Sqrt(L / g);
  205.         }
  206.  
  207.         public double CalcX()  //Вычисляем значение X без учёта начальных координат
  208.         {
  209.             double angularFrequency = (2 * Math.PI) / this.T; //Циклическая частота [рад/c]
  210.             double deceleration = Math.Pow(Math.E, -this.decelerationK * this.time / 1000d); //Время в секундах
  211.                                                                                            //TODO: уточнить коэф. замедления
  212.  
  213.             return this.Amplitude * Math.Cos(angularFrequency * (this.time / 1000d)) * deceleration; //TODO: время в секундах
  214.         }
  215.         public double CalcY()
  216.         {
  217.             return Math.Sqrt(L * L - Math.Pow(CalcX(), 2)) - L;
  218.         }
  219.  
  220.         private double GradToRadian(double angle)
  221.         {
  222.             return angle * Math.PI / 180;
  223.         }
  224.     }
  225. }
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement