|
| 1 | +package com.thealgorithms.geometry; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * The MidpointEllipse class implements the Midpoint Ellipse Drawing Algorithm. |
| 9 | + * This algorithm efficiently computes the points on an ellipse by dividing it into two regions |
| 10 | + * and using decision parameters to determine the next point to plot. |
| 11 | + */ |
| 12 | +public final class MidpointEllipse { |
| 13 | + |
| 14 | + private MidpointEllipse() { |
| 15 | + // Private constructor to prevent instantiation |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Draws an ellipse using the Midpoint Ellipse Algorithm. |
| 20 | + * |
| 21 | + * @param centerX the x-coordinate of the center of the ellipse |
| 22 | + * @param centerY the y-coordinate of the center of the ellipse |
| 23 | + * @param a the length of the semi-major axis (horizontal radius) |
| 24 | + * @param b the length of the semi-minor axis (vertical radius) |
| 25 | + * @return a list of points (represented as int arrays) that form the ellipse |
| 26 | + */ |
| 27 | + public static List<int[]> drawEllipse(int centerX, int centerY, int a, int b) { |
| 28 | + List<int[]> points = new ArrayList<>(); |
| 29 | + |
| 30 | + // Handle degenerate cases with early returns |
| 31 | + if (a == 0 && b == 0) { |
| 32 | + points.add(new int[] {centerX, centerY}); // Only the center point |
| 33 | + return points; |
| 34 | + } |
| 35 | + |
| 36 | + if (a == 0) { |
| 37 | + // Semi-major axis is zero, create a vertical line |
| 38 | + for (int y = centerY - b; y <= centerY + b; y++) { |
| 39 | + points.add(new int[] {centerX, y}); |
| 40 | + } |
| 41 | + return points; // Early return |
| 42 | + } |
| 43 | + |
| 44 | + if (b == 0) { |
| 45 | + // Semi-minor axis is zero, create a horizontal line |
| 46 | + for (int x = centerX - a; x <= centerX + a; x++) { |
| 47 | + points.add(new int[] {x, centerY}); |
| 48 | + } |
| 49 | + return points; // Early return |
| 50 | + } |
| 51 | + |
| 52 | + // Normal case: Non-degenerate ellipse |
| 53 | + computeEllipsePoints(points, centerX, centerY, a, b); |
| 54 | + |
| 55 | + return points; // Return all calculated points of the ellipse |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Computes points of a non-degenerate ellipse using the Midpoint Ellipse Algorithm. |
| 60 | + * |
| 61 | + * @param points the list to which points will be added |
| 62 | + * @param centerX the x-coordinate of the center of the ellipse |
| 63 | + * @param centerY the y-coordinate of the center of the ellipse |
| 64 | + * @param a the length of the semi-major axis (horizontal radius) |
| 65 | + * @param b the length of the semi-minor axis (vertical radius) |
| 66 | + */ |
| 67 | + private static void computeEllipsePoints(Collection<int[]> points, int centerX, int centerY, int a, int b) { |
| 68 | + int x = 0; // Initial x-coordinate |
| 69 | + int y = b; // Initial y-coordinate |
| 70 | + |
| 71 | + // Region 1: Initial decision parameter |
| 72 | + double d1 = (b * b) - (a * a * b) + (0.25 * a * a); // Decision variable for region 1 |
| 73 | + double dx = 2.0 * b * b * x; // Change in x |
| 74 | + double dy = 2.0 * a * a * y; // Change in y |
| 75 | + |
| 76 | + // Region 1: When the slope is less than 1 |
| 77 | + while (dx < dy) { |
| 78 | + addEllipsePoints(points, centerX, centerY, x, y); |
| 79 | + |
| 80 | + // Update decision parameter and variables |
| 81 | + if (d1 < 0) { |
| 82 | + x++; |
| 83 | + dx += (2 * b * b); // Update x change |
| 84 | + d1 += dx + (b * b); // Update decision parameter |
| 85 | + } else { |
| 86 | + x++; |
| 87 | + y--; |
| 88 | + dx += (2 * b * b); // Update x change |
| 89 | + dy -= (2 * a * a); // Update y change |
| 90 | + d1 += dx - dy + (b * b); // Update decision parameter |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Region 2: Initial decision parameter for the second region |
| 95 | + double d2 = b * b * (x + 0.5) * (x + 0.5) + a * a * (y - 1) * (y - 1) - a * a * b * b; |
| 96 | + |
| 97 | + // Region 2: When the slope is greater than or equal to 1 |
| 98 | + while (y >= 0) { |
| 99 | + addEllipsePoints(points, centerX, centerY, x, y); |
| 100 | + |
| 101 | + // Update decision parameter and variables |
| 102 | + if (d2 > 0) { |
| 103 | + y--; |
| 104 | + dy -= (2 * a * a); // Update y change |
| 105 | + d2 += (a * a) - dy; // Update decision parameter |
| 106 | + } else { |
| 107 | + y--; |
| 108 | + x++; |
| 109 | + dx += (2 * b * b); // Update x change |
| 110 | + dy -= (2 * a * a); // Update y change |
| 111 | + d2 += dx - dy + (a * a); // Update decision parameter |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Adds points for all four quadrants of the ellipse based on symmetry. |
| 118 | + * |
| 119 | + * @param points the list to which points will be added |
| 120 | + * @param centerX the x-coordinate of the center of the ellipse |
| 121 | + * @param centerY the y-coordinate of the center of the ellipse |
| 122 | + * @param x the x-coordinate relative to the center |
| 123 | + * @param y the y-coordinate relative to the center |
| 124 | + */ |
| 125 | + private static void addEllipsePoints(Collection<int[]> points, int centerX, int centerY, int x, int y) { |
| 126 | + points.add(new int[] {centerX + x, centerY + y}); |
| 127 | + points.add(new int[] {centerX - x, centerY + y}); |
| 128 | + points.add(new int[] {centerX + x, centerY - y}); |
| 129 | + points.add(new int[] {centerX - x, centerY - y}); |
| 130 | + } |
| 131 | +} |
0 commit comments