static void Main() { // Create a SpringODE object that represents // a 1.0 kg spring with a spring constant of // 20 N/m and a damping coefficient of 1.5 N-s/m double mass = 1.0; double mu = 1.5; double k = 20.0; double x0 = -0.2; SpringODE ode = new SpringODE(mass, mu, k, x0); // Solve the ODE over a range of 7 seconds // using a 0.1 second time increment. double dt = 0.1; Console.WriteLine("t x v"); Console.WriteLine("" + ode.GetTime() + " " + (float)ode.GetX() + " " + (float)ode.GetVx()); while (ode.GetTime() <= 7.0) { ode.UpdatePositionAndVelocity(dt); Console.WriteLine("" + ode.GetTime() + " " + (float)ode.GetX() + " " + (float)ode.GetVx()); } return; }
// Event handling method for the "Start" button public void StartButtonClicked(object source, EventArgs e) { // Get the initial values from the textfield double mass = Convert.ToDouble(massTextBox.Text); double mu = Convert.ToDouble(muTextBox.Text); double k = Convert.ToDouble(kTextBox.Text); double x0 = Convert.ToDouble(x0TextBox.Text); // Create a SpringODE object spring = new SpringODE(mass, mu, k, x0); // Start the box sliding using a Timer object // to slow down the action. gameTimer.Start(); }
public SpringSimulator() { // Create a SpringODE object with default values so // the display can be updated the first time. spring = new SpringODE(1.0, 0.5, 20.0, 0.4); // Create a Timer object that will be used // to slow the action down. gameTimer = new Timer(); gameTimer.Interval = 50; // delay in milliseconds. gameTimer.Tick += new EventHandler(ActionPerformed); // Create some Labels massLabel = new Label(); massLabel.Text = "End mass, kg"; massLabel.Font = new Font(massLabel.Font, FontStyle.Bold); massLabel.Top = 50; massLabel.Left = 10; massLabel.Width = 100; muLabel = new Label(); muLabel.Text = "Damping coefficient, kg/s"; muLabel.Font = new Font(muLabel.Font, FontStyle.Bold); muLabel.Top = 80; muLabel.Left = 10; muLabel.Width = 130; kLabel = new Label(); kLabel.Text = "Spring constant, N/m"; kLabel.Font = new Font(kLabel.Font, FontStyle.Bold); kLabel.Top = 110; kLabel.Left = 10; kLabel.Width = 120; x0Label = new Label(); x0Label.Text = "Initial location, m"; x0Label.Font = new Font(x0Label.Font, FontStyle.Bold); x0Label.Top = 140; x0Label.Left = 10; x0Label.Width = 120; // Create TextBox objects to display the outcome. massTextBox = new TextBox(); massTextBox.Width = 50; massTextBox.Text = "1.0"; massTextBox.AutoSize = true; massTextBox.Top = massLabel.Top; massTextBox.Left = 150; muTextBox = new TextBox(); muTextBox.Width = 50; muTextBox.Text = "0.5"; muTextBox.AutoSize = true; muTextBox.Top = muLabel.Top; muTextBox.Left = 150; kTextBox = new TextBox(); kTextBox.Width = 50; kTextBox.Text = "20.0"; kTextBox.AutoSize = true; kTextBox.Top = kLabel.Top; kTextBox.Left = 150; x0TextBox = new TextBox(); x0TextBox.Width = 50; x0TextBox.Text = "0.4"; x0TextBox.AutoSize = true; x0TextBox.Top = x0Label.Top; x0TextBox.Left = 150; // Create Button objects int buttonHeight = 30; int buttonWidth = 50; int buttonLeft = 20; startButton = new Button(); startButton.Text = "Start"; startButton.Height = buttonHeight; startButton.Width = buttonWidth; startButton.Top = 200; startButton.Left = buttonLeft; startButton.Click += new EventHandler(StartButtonClicked); resetButton = new Button(); resetButton.Text = "Reset"; resetButton.Height = buttonHeight; resetButton.Width = buttonWidth; resetButton.Top = 250; resetButton.Left = buttonLeft; resetButton.Click += new EventHandler(ResetButtonClicked); // Create a drawing panel. drawingPanel = new Panel(); drawingPanel.Width = 151; drawingPanel.Height = 301; drawingPanel.Left = 230; drawingPanel.Top = 50; drawingPanel.BorderStyle = BorderStyle.FixedSingle; // Add the GUI components to the Form this.Controls.Add(massLabel); this.Controls.Add(muLabel); this.Controls.Add(kLabel); this.Controls.Add(x0Label); this.Controls.Add(massTextBox); this.Controls.Add(muTextBox); this.Controls.Add(kTextBox); this.Controls.Add(x0TextBox); this.Controls.Add(startButton); this.Controls.Add(resetButton); this.Controls.Add(drawingPanel); // Set the size and title of the form this.Height = 450; this.Width = 400; this.Text = "Spring Simulator"; // Center the form on the screen and make // it visible. this.StartPosition = FormStartPosition.CenterScreen; this.Visible = true; // Update the GUI display UpdateDisplay(); }