11package com .thealgorithms .geometry ;
22
3- import java .awt .Point ;
43import java .util .ArrayList ;
54import 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 */
1511public 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+ }
0 commit comments