Skip to content

Commit 39359a0

Browse files
author
Priyanshu1303d
committed
[FEAT] Add Newton's Law of Gravitation algorithm
1 parent 8678c63 commit 39359a0

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/main/java/com/thealgorithms/physics/Gravitation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ public static double[] calculateGravitationalForce(double m1, double x1, double
3636

3737
// If bodies are at the same position, force is zero to avoid division by zero.
3838
if (distanceSq == 0) {
39-
return new double[]{0, 0};
39+
return new double[] {0, 0};
4040
}
4141

4242
double distance = Math.sqrt(distanceSq);
43-
double forceMagnitude = (GRAVITATIONAL_CONSTANT * m1 * m2) / distanceSq;
43+
double forceMagnitude = GRAVITATIONAL_CONSTANT * m1 * m2 / distanceSq;
4444

4545
// Calculate the components of the force vector
4646
double fx = forceMagnitude * (dx / distance);
4747
double fy = forceMagnitude * (dy / distance);
4848

49-
return new double[]{fx, fy};
49+
return new double[] {fx, fy};
5050
}
5151

5252
/**
@@ -61,6 +61,6 @@ public static double calculateCircularOrbitVelocity(double centralMass, double r
6161
if (centralMass <= 0 || radius <= 0) {
6262
throw new IllegalArgumentException("Mass and radius must be positive.");
6363
}
64-
return Math.sqrt((GRAVITATIONAL_CONSTANT * centralMass) / radius);
64+
return Math.sqrt(GRAVITATIONAL_CONSTANT * centralMass / radius);
6565
}
6666
}

src/test/java/com/thealgorithms/physics/GravitationTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ final class GravitationTest {
2121
void testSimpleForceCalculation() {
2222
// Force on body 2 should be F = G*1*1 / 1^2 = G, directed towards body 1 (negative x)
2323
double[] forceOnB = Gravitation.calculateGravitationalForce(1.0, 0, 0, 1.0, 1, 0);
24-
assertArrayEquals(new double[]{-G, 0.0}, forceOnB, DELTA);
24+
assertArrayEquals(new double[] {-G, 0.0}, forceOnB, DELTA);
2525

2626
// Force on body 1 should be equal and opposite (positive x)
2727
double[] forceOnA = Gravitation.calculateGravitationalForce(1.0, 1, 0, 1.0, 0, 0);
28-
assertArrayEquals(new double[]{G, 0.0}, forceOnA, DELTA);
28+
assertArrayEquals(new double[] {G, 0.0}, forceOnA, DELTA);
2929
}
3030

3131
@Test
@@ -34,20 +34,20 @@ void test2DForceCalculation() {
3434
// Body 1 at (0,0) with mass 2kg
3535
// Body 2 at (3,4) with mass 1kg
3636
// Distance is sqrt(3^2 + 4^2) = 5 meters
37-
double magnitude = (2.0 * G) / 25.0; // G * 2 * 1 / 5^2
37+
double magnitude = 2.0 * G / 25.0; // G * 2 * 1 / 5^2
3838
// Unit vector from 2 to 1 is (-3/5, -4/5)
39-
double expectedFx = magnitude * (-3.0 / 5.0); // -6G / 125
40-
double expectedFy = magnitude * (-4.0 / 5.0); // -8G / 125
39+
double expectedFx = magnitude * -3.0 / 5.0; // -6G / 125
40+
double expectedFy = magnitude * -4.0 / 5.0; // -8G / 125
4141

4242
double[] forceOnB = Gravitation.calculateGravitationalForce(2.0, 0, 0, 1.0, 3, 4);
43-
assertArrayEquals(new double[]{expectedFx, expectedFy}, forceOnB, DELTA);
43+
assertArrayEquals(new double[] {expectedFx, expectedFy}, forceOnB, DELTA);
4444
}
4545

4646
@Test
4747
@DisplayName("Test overlapping bodies should result in zero force")
4848
void testOverlappingBodies() {
4949
double[] force = Gravitation.calculateGravitationalForce(1000.0, 1.5, -2.5, 500.0, 1.5, -2.5);
50-
assertArrayEquals(new double[]{0.0, 0.0}, force, DELTA);
50+
assertArrayEquals(new double[] {0.0, 0.0}, force, DELTA);
5151
}
5252

5353
@Test

0 commit comments

Comments
 (0)