|
| 1 | +package com.thealgorithms.geometry; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class to represent the Midpoint Circle Algorithm. |
| 9 | + * This algorithm calculates points on the circumference of a circle |
| 10 | + * using integer arithmetic for efficient computation. |
| 11 | + */ |
| 12 | +public final class MidpointCircle { |
| 13 | + |
| 14 | + private MidpointCircle() { |
| 15 | + // Private Constructor to prevent instantiation. |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Generates points on the circumference of a circle using the midpoint circle algorithm. |
| 20 | + * |
| 21 | + * @param centerX The x-coordinate of the circle's center. |
| 22 | + * @param centerY The y-coordinate of the circle's center. |
| 23 | + * @param radius The radius of the circle. |
| 24 | + * @return A list of points on the circle, each represented as an int[] with 2 elements [x, y]. |
| 25 | + */ |
| 26 | + public static List<int[]> generateCirclePoints(int centerX, int centerY, int radius) { |
| 27 | + List<int[]> points = new ArrayList<>(); |
| 28 | + |
| 29 | + // Special case for radius 0, only the center point should be added. |
| 30 | + if (radius == 0) { |
| 31 | + points.add(new int[] {centerX, centerY}); |
| 32 | + return points; |
| 33 | + } |
| 34 | + |
| 35 | + // Start at (radius, 0) |
| 36 | + int x = radius; |
| 37 | + int y = 0; |
| 38 | + |
| 39 | + // Decision parameter |
| 40 | + int p = 1 - radius; |
| 41 | + |
| 42 | + // Add the initial points in all octants |
| 43 | + addSymmetricPoints(points, centerX, centerY, x, y); |
| 44 | + |
| 45 | + // Iterate while x > y |
| 46 | + while (x > y) { |
| 47 | + y++; |
| 48 | + |
| 49 | + if (p <= 0) { |
| 50 | + // Midpoint is inside or on the circle |
| 51 | + p = p + 2 * y + 1; |
| 52 | + } else { |
| 53 | + // Midpoint is outside the circle |
| 54 | + x--; |
| 55 | + p = p + 2 * y - 2 * x + 1; |
| 56 | + } |
| 57 | + |
| 58 | + // Add points for this (x, y) |
| 59 | + addSymmetricPoints(points, centerX, centerY, x, y); |
| 60 | + } |
| 61 | + |
| 62 | + return points; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Adds the symmetric points in all octants of the circle based on the current x and y values. |
| 67 | + * |
| 68 | + * @param points The list to which symmetric points will be added. |
| 69 | + * @param centerX The x-coordinate of the circle's center. |
| 70 | + * @param centerY The y-coordinate of the circle's center. |
| 71 | + * @param x The current x-coordinate on the circumference. |
| 72 | + * @param y The current y-coordinate on the circumference. |
| 73 | + */ |
| 74 | + private static void addSymmetricPoints(Collection<int[]> points, int centerX, int centerY, int x, int y) { |
| 75 | + // Octant symmetry points |
| 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 - x, centerY - y}); |
| 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}); |
| 83 | + points.add(new int[] {centerX - y, centerY - x}); |
| 84 | + } |
| 85 | +} |
0 commit comments