From f0e6f8743310384da6933247f8c1dd346a3dddee Mon Sep 17 00:00:00 2001 From: Priyanshu1303d Date: Mon, 20 Oct 2025 22:15:58 +0530 Subject: [PATCH 1/3] [FEAT] Add Newton's Law of Gravitation algorithm --- .../thealgorithms/physics/Gravitation.java | 66 +++++++++++++++ .../physics/GravitationTest.java | 83 +++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 src/main/java/com/thealgorithms/physics/Gravitation.java create mode 100644 src/test/java/com/thealgorithms/physics/GravitationTest.java diff --git a/src/main/java/com/thealgorithms/physics/Gravitation.java b/src/main/java/com/thealgorithms/physics/Gravitation.java new file mode 100644 index 000000000000..961938d58226 --- /dev/null +++ b/src/main/java/com/thealgorithms/physics/Gravitation.java @@ -0,0 +1,66 @@ +package com.thealgorithms.physics; + +/** + * Implements Newton's Law of Universal Gravitation. + * Provides simple static methods to calculate gravitational force and circular orbit velocity. + * + * @author [Priyanshu Kumar Singh](https://github.com/Priyanshu1303d) + * @see Wikipedia + */ +public final class Gravitation { + + /** Gravitational constant in m^3 kg^-1 s^-2 */ + public static final double GRAVITATIONAL_CONSTANT = 6.67430e-11; + + /** + * Private constructor to prevent instantiation of this utility class. + */ + private Gravitation() { + } + + /** + * Calculates the gravitational force vector exerted by one body on another. + * + * @param m1 Mass of the first body (kg). + * @param x1 X-position of the first body (m). + * @param y1 Y-position of the first body (m). + * @param m2 Mass of the second body (kg). + * @param x2 X-position of the second body (m). + * @param y2 Y-position of the second body (m). + * @return A double array `[fx, fy]` representing the force vector on the second body. + */ + public static double[] calculateGravitationalForce(double m1, double x1, double y1, double m2, double x2, double y2) { + double dx = x1 - x2; + double dy = y1 - y2; + double distanceSq = dx * dx + dy * dy; + + // If bodies are at the same position, force is zero to avoid division by zero. + if (distanceSq == 0) { + return new double[]{0, 0}; + } + + double distance = Math.sqrt(distanceSq); + double forceMagnitude = (GRAVITATIONAL_CONSTANT * m1 * m2) / distanceSq; + + // Calculate the components of the force vector + double fx = forceMagnitude * (dx / distance); + double fy = forceMagnitude * (dy / distance); + + return new double[]{fx, fy}; + } + + /** + * Calculates the speed required for a stable circular orbit. + * + * @param centralMass The mass of the central body (kg). + * @param radius The radius of the orbit (m). + * @return The orbital speed (m/s). + * @throws IllegalArgumentException if mass or radius are not positive. + */ + public static double calculateCircularOrbitVelocity(double centralMass, double radius) { + if (centralMass <= 0 || radius <= 0) { + throw new IllegalArgumentException("Mass and radius must be positive."); + } + return Math.sqrt((GRAVITATIONAL_CONSTANT * centralMass) / radius); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/physics/GravitationTest.java b/src/test/java/com/thealgorithms/physics/GravitationTest.java new file mode 100644 index 000000000000..f93794b7ffca --- /dev/null +++ b/src/test/java/com/thealgorithms/physics/GravitationTest.java @@ -0,0 +1,83 @@ +package com.thealgorithms.physics; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the Gravitation utility class. + */ +final class GravitationTest { + + // A small tolerance (delta) for comparing floating-point numbers + private static final double DELTA = 1e-9; + private static final double G = Gravitation.GRAVITATIONAL_CONSTANT; + + @Test + @DisplayName("Test gravitational force between two bodies on the x-axis") + void testSimpleForceCalculation() { + // Force on body 2 should be F = G*1*1 / 1^2 = G, directed towards body 1 (negative x) + double[] forceOnB = Gravitation.calculateGravitationalForce(1.0, 0, 0, 1.0, 1, 0); + assertArrayEquals(new double[]{-G, 0.0}, forceOnB, DELTA); + + // Force on body 1 should be equal and opposite (positive x) + double[] forceOnA = Gravitation.calculateGravitationalForce(1.0, 1, 0, 1.0, 0, 0); + assertArrayEquals(new double[]{G, 0.0}, forceOnA, DELTA); + } + + @Test + @DisplayName("Test gravitational force in a 2D plane") + void test2DForceCalculation() { + // Body 1 at (0,0) with mass 2kg + // Body 2 at (3,4) with mass 1kg + // Distance is sqrt(3^2 + 4^2) = 5 meters + double magnitude = (2.0 * G) / 25.0; // G * 2 * 1 / 5^2 + // Unit vector from 2 to 1 is (-3/5, -4/5) + double expectedFx = magnitude * (-3.0 / 5.0); // -6G / 125 + double expectedFy = magnitude * (-4.0 / 5.0); // -8G / 125 + + double[] forceOnB = Gravitation.calculateGravitationalForce(2.0, 0, 0, 1.0, 3, 4); + assertArrayEquals(new double[]{expectedFx, expectedFy}, forceOnB, DELTA); + } + + @Test + @DisplayName("Test overlapping bodies should result in zero force") + void testOverlappingBodies() { + double[] force = Gravitation.calculateGravitationalForce(1000.0, 1.5, -2.5, 500.0, 1.5, -2.5); + assertArrayEquals(new double[]{0.0, 0.0}, force, DELTA); + } + + @Test + @DisplayName("Test circular orbit velocity with simple values") + void testCircularOrbitVelocity() { + // v = sqrt(G*1/1) = sqrt(G) + double velocity = Gravitation.calculateCircularOrbitVelocity(1.0, 1.0); + assertEquals(Math.sqrt(G), velocity, DELTA); + } + + @Test + @DisplayName("Test orbital velocity with real-world-ish values (LEO)") + void testEarthOrbitVelocity() { + // Mass of Earth ~5.972e24 kg + // Radius of LEO ~6,771,000 m (Earth radius + 400km) + double earthMass = 5.972e24; + double leoRadius = 6.771e6; + // FIX: Updated expected value to match the high-precision calculation + double expectedVelocity = 7672.4904; + + double velocity = Gravitation.calculateCircularOrbitVelocity(earthMass, leoRadius); + assertEquals(expectedVelocity, velocity, 0.0001); // Use a larger delta for big numbers + } + + @Test + @DisplayName("Test invalid inputs for orbital velocity throw exception") + void testInvalidOrbitalVelocityInputs() { + assertThrows(IllegalArgumentException.class, () -> Gravitation.calculateCircularOrbitVelocity(0, 100)); + assertThrows(IllegalArgumentException.class, () -> Gravitation.calculateCircularOrbitVelocity(-1000, 100)); + assertThrows(IllegalArgumentException.class, () -> Gravitation.calculateCircularOrbitVelocity(1000, 0)); + assertThrows(IllegalArgumentException.class, () -> Gravitation.calculateCircularOrbitVelocity(1000, -100)); + } +} From 8678c63226acb0f95ce0cf21e1631ba8c268b8d2 Mon Sep 17 00:00:00 2001 From: Priyanshu1303d Date: Mon, 20 Oct 2025 22:18:00 +0530 Subject: [PATCH 2/3] [FEAT] Add Newton's Law of Gravitation algorithm --- src/main/java/com/thealgorithms/physics/Gravitation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/physics/Gravitation.java b/src/main/java/com/thealgorithms/physics/Gravitation.java index 961938d58226..22c5a19a1f6c 100644 --- a/src/main/java/com/thealgorithms/physics/Gravitation.java +++ b/src/main/java/com/thealgorithms/physics/Gravitation.java @@ -63,4 +63,4 @@ public static double calculateCircularOrbitVelocity(double centralMass, double r } return Math.sqrt((GRAVITATIONAL_CONSTANT * centralMass) / radius); } -} \ No newline at end of file +} From 39359a035a062ff786ae0f6cd077372a540c8a13 Mon Sep 17 00:00:00 2001 From: Priyanshu1303d Date: Mon, 20 Oct 2025 22:28:16 +0530 Subject: [PATCH 3/3] [FEAT] Add Newton's Law of Gravitation algorithm --- .../com/thealgorithms/physics/Gravitation.java | 8 ++++---- .../com/thealgorithms/physics/GravitationTest.java | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/physics/Gravitation.java b/src/main/java/com/thealgorithms/physics/Gravitation.java index 22c5a19a1f6c..292fdc195f85 100644 --- a/src/main/java/com/thealgorithms/physics/Gravitation.java +++ b/src/main/java/com/thealgorithms/physics/Gravitation.java @@ -36,17 +36,17 @@ public static double[] calculateGravitationalForce(double m1, double x1, double // If bodies are at the same position, force is zero to avoid division by zero. if (distanceSq == 0) { - return new double[]{0, 0}; + return new double[] {0, 0}; } double distance = Math.sqrt(distanceSq); - double forceMagnitude = (GRAVITATIONAL_CONSTANT * m1 * m2) / distanceSq; + double forceMagnitude = GRAVITATIONAL_CONSTANT * m1 * m2 / distanceSq; // Calculate the components of the force vector double fx = forceMagnitude * (dx / distance); double fy = forceMagnitude * (dy / distance); - return new double[]{fx, fy}; + return new double[] {fx, fy}; } /** @@ -61,6 +61,6 @@ public static double calculateCircularOrbitVelocity(double centralMass, double r if (centralMass <= 0 || radius <= 0) { throw new IllegalArgumentException("Mass and radius must be positive."); } - return Math.sqrt((GRAVITATIONAL_CONSTANT * centralMass) / radius); + return Math.sqrt(GRAVITATIONAL_CONSTANT * centralMass / radius); } } diff --git a/src/test/java/com/thealgorithms/physics/GravitationTest.java b/src/test/java/com/thealgorithms/physics/GravitationTest.java index f93794b7ffca..9094e289e79a 100644 --- a/src/test/java/com/thealgorithms/physics/GravitationTest.java +++ b/src/test/java/com/thealgorithms/physics/GravitationTest.java @@ -21,11 +21,11 @@ final class GravitationTest { void testSimpleForceCalculation() { // Force on body 2 should be F = G*1*1 / 1^2 = G, directed towards body 1 (negative x) double[] forceOnB = Gravitation.calculateGravitationalForce(1.0, 0, 0, 1.0, 1, 0); - assertArrayEquals(new double[]{-G, 0.0}, forceOnB, DELTA); + assertArrayEquals(new double[] {-G, 0.0}, forceOnB, DELTA); // Force on body 1 should be equal and opposite (positive x) double[] forceOnA = Gravitation.calculateGravitationalForce(1.0, 1, 0, 1.0, 0, 0); - assertArrayEquals(new double[]{G, 0.0}, forceOnA, DELTA); + assertArrayEquals(new double[] {G, 0.0}, forceOnA, DELTA); } @Test @@ -34,20 +34,20 @@ void test2DForceCalculation() { // Body 1 at (0,0) with mass 2kg // Body 2 at (3,4) with mass 1kg // Distance is sqrt(3^2 + 4^2) = 5 meters - double magnitude = (2.0 * G) / 25.0; // G * 2 * 1 / 5^2 + double magnitude = 2.0 * G / 25.0; // G * 2 * 1 / 5^2 // Unit vector from 2 to 1 is (-3/5, -4/5) - double expectedFx = magnitude * (-3.0 / 5.0); // -6G / 125 - double expectedFy = magnitude * (-4.0 / 5.0); // -8G / 125 + double expectedFx = magnitude * -3.0 / 5.0; // -6G / 125 + double expectedFy = magnitude * -4.0 / 5.0; // -8G / 125 double[] forceOnB = Gravitation.calculateGravitationalForce(2.0, 0, 0, 1.0, 3, 4); - assertArrayEquals(new double[]{expectedFx, expectedFy}, forceOnB, DELTA); + assertArrayEquals(new double[] {expectedFx, expectedFy}, forceOnB, DELTA); } @Test @DisplayName("Test overlapping bodies should result in zero force") void testOverlappingBodies() { double[] force = Gravitation.calculateGravitationalForce(1000.0, 1.5, -2.5, 500.0, 1.5, -2.5); - assertArrayEquals(new double[]{0.0, 0.0}, force, DELTA); + assertArrayEquals(new double[] {0.0, 0.0}, force, DELTA); } @Test