Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows.Threading;
- using System.Threading.Tasks;
- 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.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Clock
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private int millisecondInterval;
- private double scaleK;
- private static MyClock clock;
- private static Oscillator oscillator;
- public MainWindow()
- {
- InitializeComponent();
- secondsTextBox.PreviewTextInput += ValidateNumbers;
- secondsTextBox.TextChanged += ValidateLessThan60;
- clock = new MyClock();
- oscillator = new Oscillator(1d); //TODO: сделать переменным
- scaleK = 250d / 4d; //TODO: должно зависеть от переменных
- millisecondInterval = 10; //TODO: Должно зависеть от маятника
- DispatcherTimer dispatcherTimer = new DispatcherTimer();
- dispatcherTimer.Interval = TimeSpan.FromMilliseconds(millisecondInterval);
- dispatcherTimer.Tick += ExecuteEveryTick;
- dispatcherTimer.Start();
- }
- private void ValidateNumbers(object sender, TextCompositionEventArgs e)
- {
- for(int i = 0; i < e.Text.Length; i++)
- {
- if(!Char.IsDigit(e.Text[i]))
- {
- MessageBox.Show("Неверный формат ввода. В данном поле разрешено вводить только цифры.", "Ошибка формата ввода");
- e.Handled = true;
- }
- }
- }
- private void ValidateLessThan60(object sender, TextChangedEventArgs e)
- {
- TextBox textBox = (TextBox)sender;
- int value = 0;
- try
- {
- value = Int32.Parse(textBox.Text);
- }
- catch(Exception ex)
- {
- textBox.Text = "0";
- MessageBox.Show("Неверный формат ввода. В данном поле разрешено вводить только числа от 0 до 59 включительно.", "Ошибка формата ввода");
- e.Handled = true;
- }
- if (value < 0 && value >= 60)
- {
- textBox.Text = "0";
- MessageBox.Show("Неверный формат ввода. В данном поле разрешено вводить только числа от 0 до 59 включительно.", "Ошибка формата ввода");
- e.Handled = true;
- }
- }
- private void ExecuteEveryTick(object sender, EventArgs e)
- {
- clock.secAngle += (6d / 1000) * (millisecondInterval); //Секундная стрелка проходит 6 градусов за 1000 мс
- clock.minAngle += (0.1d / 1000) * millisecondInterval; //Минутная стрелка проходит 0.1 градусов за 1000 мс
- clock.hAngle += (30d / 3600) * millisecondInterval / 1000; //Часовая стрелка проходит (30 / 3600) градусов за 1000 мс
- RotateTransform secondArrowRotateTransform = new RotateTransform(clock.secAngle);
- RotateTransform minuteArrowRotateTransform = new RotateTransform(clock.minAngle);
- RotateTransform hourArrowRotateTransform = new RotateTransform(clock.hAngle);
- secondArrow.RenderTransform = secondArrowRotateTransform;
- minuteArrow.RenderTransform = minuteArrowRotateTransform;
- hourArrow.RenderTransform = hourArrowRotateTransform;
- oscillator.time += millisecondInterval;
- oscillator.x = oscillator.CalcX() * scaleK;
- oscillator.y = oscillator.CalcY() * scaleK;
- TranslateTransform oscillatorTranslateTransform = new TranslateTransform();
- oscillatorTranslateTransform.X = oscillator.x;
- oscillatorTranslateTransform.Y = oscillator.y;
- oscillatorWeight.RenderTransform = oscillatorTranslateTransform;
- oscillatorRope.X2 = 375 + oscillator.x;
- oscillatorRope.Y2 = 250 + oscillator.y;
- }
- }
- public class MyClock
- {
- private int sec;
- private int min;
- private int h;
- public double secAngle;
- public double minAngle;
- public double hAngle;
- public MyClock()
- {
- sec = 0;
- min = 0;
- h = 0;
- secAngle = 0;
- minAngle = 0;
- hAngle = 0;
- }
- public MyClock(int sec, int min, int h)
- {
- Second = sec;
- Minute = min;
- Hour = h;
- secAngle = sec/6;
- minAngle = min/6;
- hAngle = h*30;
- }
- public int Second
- {
- get
- {
- return sec;
- }
- set
- {
- sec = value % 60;
- }
- }
- public int Minute
- {
- get
- {
- return min;
- }
- set
- {
- min = value % 60;
- }
- }
- public int Hour
- {
- get
- {
- return h;
- }
- set
- {
- h = value % 12;
- }
- }
- }
- public class Oscillator
- {
- public double angle = 30; //Угол отклонения маятника от вертикальной оси в грудасах
- public double r; //Радиус колеса, образующего циклоиду
- public double L; //Длина нити; L = 4 * r;
- public double T; //Период колебаний
- public double phi; //Фаза колебаний
- public double decelerationK = 0; //Коэффициент затухания
- public double Amplitude;
- public int time;
- public double x, y; //Координаты маятника
- public Oscillator(double r)
- {
- this.time = 0;
- this.r = r; //[м]
- this.L = 4 * r; //[м]
- this.x = 0; //[м]
- this.y = 0; //[м]
- this.Amplitude = Math.Sin(GradToRadian(angle)) * L;
- const double g = 9.81;
- this.T = 2 * Math.PI * Math.Sqrt(L / g);
- }
- public double CalcX() //Вычисляем значение X без учёта начальных координат
- {
- double angularFrequency = (2 * Math.PI) / this.T; //Циклическая частота [рад/c]
- double deceleration = Math.Pow(Math.E, -this.decelerationK * this.time / 1000d); //Время в секундах
- //TODO: уточнить коэф. замедления
- return this.Amplitude * Math.Cos(angularFrequency * (this.time / 1000d)) * deceleration; //TODO: время в секундах
- }
- public double CalcY()
- {
- return Math.Sqrt(L * L - Math.Pow(CalcX(), 2)) - L;
- }
- private double GradToRadian(double angle)
- {
- return angle * Math.PI / 180;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement