Repository: TheAlgorithms/Java
Pull request #5868 (2024-10-16 04:23:59)

feat: Add midpoint circle algorithm
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5868?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 66.77%. Comparing base [(`2a518e3`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/2a518e3c9a44c4333c5ec9283895132264773afb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`f7cf5c4`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f7cf5c44114da9a4af48c496d26d4ed08a0b42ed?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5868      +/-   ##
============================================
+ Coverage     66.70%   66.77%   +0.06%     
- Complexity     4482     4489       +7     
============================================
  Files           608      609       +1     
  Lines         16913    16938      +25     
  Branches       3264     3267       +3     
============================================
+ Hits          11282    11310      +28     
+ Misses         5182     5181       -1     
+ Partials        449      447       -2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5868?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/geometry/MidpointCircle.java b/src/main/java/com/thealgorithms/geometry/MidpointCircle.java
new file mode 100644
index 00000000..803e8bb4
--- /dev/null
+++ b/src/main/java/com/thealgorithms/geometry/MidpointCircle.java
@@ -0,0 +1,85 @@
+package com.thealgorithms.geometry;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Class to represent the Midpoint Circle Algorithm.
+ * This algorithm calculates points on the circumference of a circle
+ * using integer arithmetic for efficient computation.
+ */
+public final class MidpointCircle {
+
+    private MidpointCircle() {
+        // Private Constructor to prevent instantiation.
+    }
+
+    /**
+     * Generates points on the circumference of a circle using the midpoint circle algorithm.
+     *
+     * @param centerX The x-coordinate of the circle's center.
+     * @param centerY The y-coordinate of the circle's center.
+     * @param radius  The radius of the circle.
+     * @return A list of points on the circle, each represented as an int[] with 2 elements [x, y].
+     */
+    public static List<int[]> generateCirclePoints(int centerX, int centerY, int radius) {
+        List<int[]> points = new ArrayList<>();
+
+        // Special case for radius 0, only the center point should be added.
+        if (radius == 0) {
+            points.add(new int[] {centerX, centerY});
+            return points;
+        }
+
+        // Start at (radius, 0)
+        int x = radius;
+        int y = 0;
+
+        // Decision parameter
+        int p = 1 - radius;
+
+        // Add the initial points in all octants
+        addSymmetricPoints(points, centerX, centerY, x, y);
+
+        // Iterate while x > y
+        while (x > y) {
+            y++;
+
+            if (p <= 0) {
+                // Midpoint is inside or on the circle
+                p = p + 2 * y + 1;
+            } else {
+                // Midpoint is outside the circle
+                x--;
+                p = p + 2 * y - 2 * x + 1;
+            }
+
+            // Add points for this (x, y)
+            addSymmetricPoints(points, centerX, centerY, x, y);
+        }
+
+        return points;
+    }
+
+    /**
+     * Adds the symmetric points in all octants of the circle based on the current x and y values.
+     *
+     * @param points  The list to which symmetric points will be added.
+     * @param centerX The x-coordinate of the circle's center.
+     * @param centerY The y-coordinate of the circle's center.
+     * @param x       The current x-coordinate on the circumference.
+     * @param y       The current y-coordinate on the circumference.
+     */
+    private static void addSymmetricPoints(Collection<int[]> points, int centerX, int centerY, int x, int y) {
+        // Octant symmetry points
+        points.add(new int[] {centerX + x, centerY + y});
+        points.add(new int[] {centerX - x, centerY + y});
+        points.add(new int[] {centerX + x, centerY - y});
+        points.add(new int[] {centerX - x, centerY - y});
+        points.add(new int[] {centerX + y, centerY + x});
+        points.add(new int[] {centerX - y, centerY + x});
+        points.add(new int[] {centerX + y, centerY - x});
+        points.add(new int[] {centerX - y, centerY - x});
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/geometry/MidpointCircleTest.java b/src/test/java/com/thealgorithms/geometry/MidpointCircleTest.java
new file mode 100644
index 00000000..24370cd4
--- /dev/null
+++ b/src/test/java/com/thealgorithms/geometry/MidpointCircleTest.java
@@ -0,0 +1,55 @@
+package com.thealgorithms.geometry;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+/**
+ * Test class for the {@code MidpointCircle} class
+ */
+class MidpointCircleTest {
+
+    /**
+     * Parameterized test to check the generated points for different circles.
+     * The points are checked based on the expected center and radius.
+     *
+     * @param centerX The x-coordinate of the circle's center.
+     * @param centerY The y-coordinate of the circle's center.
+     * @param radius  The radius of the circle.
+     */
+    @ParameterizedTest
+    @CsvSource({
+        "0, 0, 3", // Circle centered at (0, 0) with radius 3
+        "10, 10, 2" // Circle centered at (10, 10) with radius 2
+    })
+    void
+    testGenerateCirclePoints(int centerX, int centerY, int radius) {
+        List<int[]> points = MidpointCircle.generateCirclePoints(centerX, centerY, radius);
+
+        // Ensure that all points satisfy the circle equation (x - centerX)^2 + (y - centerY)^2 = radius^2
+        for (int[] point : points) {
+            int x = point[0];
+            int y = point[1];
+
+            int dx = x - centerX;
+            int dy = y - centerY;
+            int distanceSquared = dx * dx + dy * dy;
+
+            assertTrue(Math.abs(distanceSquared - radius * radius) <= 1, "Point (" + x + ", " + y + ") does not satisfy the circle equation.");
+        }
+    }
+
+    /**
+     * Test to ensure the algorithm generates points for a zero-radius circle.
+     */
+    @Test
+    void testZeroRadiusCircle() {
+        List<int[]> points = MidpointCircle.generateCirclePoints(0, 0, 0);
+
+        // A zero-radius circle should only have one point: (0, 0)
+        assertTrue(points.size() == 1 && points.get(0)[0] == 0 && points.get(0)[1] == 0, "Zero-radius circle did not generate the correct point.");
+    }
+}