1
1
package com .thealgorithms .geometry ;
2
2
3
- import java .awt .Point ;
4
3
import java .util .ArrayList ;
5
4
import java .util .List ;
6
5
7
6
/**
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.
14
10
*/
15
11
public final class MidpointCircle {
16
12
17
13
private MidpointCircle () {
18
- // Private constructor to prevent instantiation.
14
+ // Private Constructor to prevent instantiation.
19
15
}
20
16
21
17
/**
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].
29
24
*/
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 <>();
32
27
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 ;
36
40
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 );
39
43
40
- while (x < y ) {
41
- x ++;
44
+ // Iterate while x > y
45
+ while (x > y ) {
46
+ y ++;
42
47
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 ;
46
51
} 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 ;
49
55
}
50
56
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 );
53
59
}
54
60
55
- return circlePoints ; // Return the list of points forming the circle
61
+ return points ;
56
62
}
57
63
58
64
/**
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.
66
72
*/
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 });
76
83
}
77
- }
84
+ }
0 commit comments