Advertisement
CherMi

Маятник

Dec 15th, 2020
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Oscillator o = new Oscillator(1);
  14.             o.startOscillator();
  15.             Console.ReadKey(true);
  16.         }
  17.     }
  18.  
  19.     public class Oscillator
  20.     {
  21.         private double r; //Радиус колеса, образующего циклоиду
  22.         private double L;
  23.         private double T; //Период колебаний
  24.         private double phi; //Фаза колебаний
  25.         private System.Timers.Timer timer; //Таймер, чтобы считать положение маятника в зависимости от времени
  26.         private DateTime startTime; //Время запуска маятника
  27.         private double maxSkewAngle;
  28.         private double p;
  29.  
  30.         public Oscillator(double r)
  31.         {
  32.             this.r = r;
  33.             this.L = 4 * r;
  34.             T = 2 * Math.PI * Math.Sqrt(L / 9.81); //T = 2 * Pi * Sqrt(L/g), L = 4a
  35.             timer = new System.Timers.Timer();
  36.             if (T < 1)
  37.             {
  38.                 timer.Interval = T * 1000; //В милисекундах
  39.             }
  40.             else
  41.             {
  42.                 timer.Interval = 100;
  43.             }
  44.             timer.Elapsed += oscillatorCoord;
  45.             timer.AutoReset = true;
  46.             timer.Enabled = true;
  47.  
  48.         }
  49.  
  50.         void oscillatorCoord(object sender, System.Timers.ElapsedEventArgs e)
  51.         {
  52.             double phase = (e.SignalTime - startTime).TotalSeconds / T;
  53.             //Привести фазу к фи[0; 2Pi]
  54.             double n = phase / (2 * Math.PI);
  55.             phi = (n - Math.Truncate(n)) * 2 * Math.PI;
  56.             phi -= Math.PI;
  57.  
  58.             double x = r * (phi - Math.Sin(phi));
  59.             double y = r * (1 + Math.Cos(phi));
  60.  
  61.             Console.Write("e.SignalTime - startTime = ");
  62.             Console.Write((e.SignalTime - startTime).TotalSeconds);
  63.             Console.Write("; phase = ");
  64.             Console.Write(phase);
  65.             Console.Write("; phi = ");
  66.             Console.Write(phi);
  67.             Console.Write("; x = ");
  68.             Console.Write(x);
  69.             Console.Write("; y = ");
  70.             Console.WriteLine(y);
  71.         }
  72.  
  73.         public void startOscillator()
  74.         {
  75.             startTime = DateTime.Now;
  76.             timer.Start();
  77.             p = 10;
  78.         }
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement