Skip to content

Commit f7cf5c4

Browse files
committed
fix: test cases
1 parent 91e21f4 commit f7cf5c4

File tree

3 files changed

+113
-108
lines changed

3 files changed

+113
-108
lines changed
Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,84 @@
11
package com.thealgorithms.geometry;
22

3-
import java.awt.Point;
43
import java.util.ArrayList;
54
import java.util.List;
65

76
/**
8-
* The {@code MidpointCircle} class implements the Midpoint Circle algorithm,
9-
* which is an efficient way to determine the points of a circle
10-
* centered at a given point with a specified radius in a 2D space.
11-
*
12-
* <p>This algorithm uses integer arithmetic to calculate the points,
13-
* making it suitable for rasterization in computer graphics.</p>
7+
* Class to represent the Midpoint Circle Algorithm.
8+
* This algorithm calculates points on the circumference of a circle
9+
* using integer arithmetic for efficient computation.
1410
*/
1511
public final class MidpointCircle {
1612

1713
private MidpointCircle() {
18-
// Private constructor to prevent instantiation.
14+
// Private Constructor to prevent instantiation.
1915
}
2016

2117
/**
22-
* Finds the list of points that form a circle centered at (xc, yc)
23-
* with a given radius r.
24-
*
25-
* @param xc the x-coordinate of the center point
26-
* @param yc the y-coordinate of the center point
27-
* @param r the radius of the circle
28-
* @return a {@code List<Point>} containing all points on the circle
18+
* Generates points on the circumference of a circle using the midpoint circle algorithm.
19+
*
20+
* @param centerX The x-coordinate of the circle's center.
21+
* @param centerY The y-coordinate of the circle's center.
22+
* @param radius The radius of the circle.
23+
* @return A list of points on the circle, each represented as an int[] with 2 elements [x, y].
2924
*/
30-
public static List<Point> drawCircle(int xc, int yc, int r) {
31-
List<Point> circlePoints = new ArrayList<>();
25+
public static List<int[]> generateCirclePoints(int centerX, int centerY, int radius) {
26+
List<int[]> points = new ArrayList<>();
3227

33-
int x = 0;
34-
int y = r;
35-
int p = 1 - r; // Initial decision parameter
28+
// Special case for radius 0, only the center point should be added.
29+
if (radius == 0) {
30+
points.add(new int[]{centerX, centerY});
31+
return points;
32+
}
33+
34+
// Start at (radius, 0)
35+
int x = radius;
36+
int y = 0;
37+
38+
// Decision parameter
39+
int p = 1 - radius;
3640

37-
// Function to add points based on symmetry
38-
addCirclePoints(circlePoints, xc, yc, x, y);
41+
// Add the initial points in all octants
42+
addSymmetricPoints(points, centerX, centerY, x, y);
3943

40-
while (x < y) {
41-
x++;
44+
// Iterate while x > y
45+
while (x > y) {
46+
y++;
4247

43-
// Update decision parameter
44-
if (p < 0) {
45-
p += 2 * x + 1; // Midpoint is inside the circle
48+
if (p <= 0) {
49+
// Midpoint is inside or on the circle
50+
p = p + 2 * y + 1;
4651
} else {
47-
y--; // Midpoint is outside the circle
48-
p += 2 * x - 2 * y + 1;
52+
// Midpoint is outside the circle
53+
x--;
54+
p = p + 2 * y - 2 * x + 1;
4955
}
5056

51-
// Add points based on symmetry
52-
addCirclePoints(circlePoints, xc, yc, x, y);
57+
// Add points for this (x, y)
58+
addSymmetricPoints(points, centerX, centerY, x, y);
5359
}
5460

55-
return circlePoints; // Return the list of points forming the circle
61+
return points;
5662
}
5763

5864
/**
59-
* Adds points to the list based on symmetry in all octants.
60-
*
61-
* @param points the list of points to add to
62-
* @param xc the x-coordinate of the center point
63-
* @param yc the y-coordinate of the center point
64-
* @param x current x coordinate in circle drawing
65-
* @param y current y coordinate in circle drawing
65+
* Adds the symmetric points in all octants of the circle based on the current x and y values.
66+
*
67+
* @param points The list to which symmetric points will be added.
68+
* @param centerX The x-coordinate of the circle's center.
69+
* @param centerY The y-coordinate of the circle's center.
70+
* @param x The current x-coordinate on the circumference.
71+
* @param y The current y-coordinate on the circumference.
6672
*/
67-
private static void addCirclePoints(List<Point> points, int xc, int yc, int x, int y) {
68-
points.add(new Point(xc + x, yc + y));
69-
points.add(new Point(xc - x, yc + y));
70-
points.add(new Point(xc + x, yc - y));
71-
points.add(new Point(xc - x, yc - y));
72-
points.add(new Point(xc + y, yc + x));
73-
points.add(new Point(xc - y, yc + x));
74-
points.add(new Point(xc + y, yc - x));
75-
points.add(new Point(xc - y, yc - x));
73+
private static void addSymmetricPoints(List<int[]> points, int centerX, int centerY, int x, int y) {
74+
// Octant symmetry points
75+
points.add(new int[] { centerX + x, centerY + y });
76+
points.add(new int[] { centerX - x, centerY + y });
77+
points.add(new int[] { centerX + x, centerY - y });
78+
points.add(new int[] { centerX - x, centerY - y });
79+
points.add(new int[] { centerX + y, centerY + x });
80+
points.add(new int[] { centerX - y, centerY + x });
81+
points.add(new int[] { centerX + y, centerY - x });
82+
points.add(new int[] { centerX - y, centerY - x });
7683
}
77-
}
84+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.geometry;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
import java.util.List;
8+
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
/**
12+
* Test class for the {@code MidpointCircle} class
13+
*/
14+
class MidpointCircleTest {
15+
16+
/**
17+
* Parameterized test to check the generated points for different circles.
18+
* The points are checked based on the expected center and radius.
19+
*
20+
* @param centerX The x-coordinate of the circle's center.
21+
* @param centerY The y-coordinate of the circle's center.
22+
* @param radius The radius of the circle.
23+
*/
24+
@ParameterizedTest
25+
@CsvSource({
26+
"0, 0, 3", // Circle centered at (0, 0) with radius 3
27+
"10, 10, 2" // Circle centered at (10, 10) with radius 2
28+
})
29+
void testGenerateCirclePoints(int centerX, int centerY, int radius) {
30+
List<int[]> points = MidpointCircle.generateCirclePoints(centerX, centerY, radius);
31+
32+
// Ensure that all points satisfy the circle equation (x - centerX)^2 + (y - centerY)^2 = radius^2
33+
for (int[] point : points) {
34+
int x = point[0];
35+
int y = point[1];
36+
37+
int dx = x - centerX;
38+
int dy = y - centerY;
39+
int distanceSquared = dx * dx + dy * dy;
40+
41+
assertTrue(Math.abs(distanceSquared - radius * radius) <= 1,
42+
"Point (" + x + ", " + y + ") does not satisfy the circle equation.");
43+
}
44+
}
45+
46+
/**
47+
* Test to ensure the algorithm generates points for a zero-radius circle.
48+
*/
49+
@Test
50+
void testZeroRadiusCircle() {
51+
List<int[]> points = MidpointCircle.generateCirclePoints(0, 0, 0);
52+
53+
// A zero-radius circle should only have one point: (0, 0)
54+
assertTrue(points.size() == 1 && points.get(0)[0] == 0 && points.get(0)[1] == 0,
55+
"Zero-radius circle did not generate the correct point.");
56+
}
57+
}

src/test/java/com/thealgorithms/geometry/MidpointcircleTest.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)