From c3904bc3af74e6b6f5b17018c6533520747d73d9 Mon Sep 17 00:00:00 2001 From: nickzerjeski Date: Mon, 13 Apr 2026 10:50:49 +0200 Subject: [PATCH] feat: add delta x motion equation implementation Fixes: #191 --- maths/delta_x.rb | 11 +++++++++++ maths/delta_x_test.rb | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 maths/delta_x.rb create mode 100644 maths/delta_x_test.rb diff --git a/maths/delta_x.rb b/maths/delta_x.rb new file mode 100644 index 0000000..b58fd6f --- /dev/null +++ b/maths/delta_x.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# Computes displacement from uniformly accelerated motion: +# delta_x = v0 * t + 0.5 * a * t^2 +class DeltaX + class << self + def call(initial_velocity:, time:, acceleration:) + (initial_velocity * time) + (0.5 * acceleration * time * time) + end + end +end diff --git a/maths/delta_x_test.rb b/maths/delta_x_test.rb new file mode 100644 index 0000000..424c784 --- /dev/null +++ b/maths/delta_x_test.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'minitest/autorun' +require_relative './delta_x' + +class DeltaXTest < Minitest::Test + def test_zero_acceleration + assert_in_delta 15.0, DeltaX.call(initial_velocity: 3.0, time: 5.0, acceleration: 0.0), 1E-12 + end + + def test_positive_acceleration + assert_in_delta 20.0, DeltaX.call(initial_velocity: 2.0, time: 4.0, acceleration: 1.5), 1E-12 + end + + def test_negative_acceleration + assert_in_delta 2.0, DeltaX.call(initial_velocity: 5.0, time: 2.0, acceleration: -4.0), 1E-12 + end +end