-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from Divyesh000/PolygonCalculator
create a PolygonCalculator class
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/com/github/creme332/algorithms/PolygonCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.creme332.algorithms; | ||
|
||
import java.awt.Polygon; | ||
import java.awt.Shape; | ||
import java.awt.geom.Point2D; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class PolygonCalculator { | ||
|
||
private final Map<Integer, Shape> polygonCache = new HashMap<>(); | ||
|
||
public Point2D.Double rotateVector(Point2D.Double point, double radAngle) { | ||
return new Point2D.Double( | ||
point.x * Math.cos(radAngle) - point.y * Math.sin(radAngle), | ||
point.x * Math.sin(radAngle) + point.y * Math.cos(radAngle) | ||
); | ||
} | ||
|
||
private Shape getRegularPolygon(int sidesCount, int length) { | ||
if (polygonCache.containsKey(sidesCount)) { | ||
return polygonCache.get(sidesCount); | ||
} | ||
|
||
double rotationAngle = Math.toRadians(360.0 / sidesCount); | ||
Point2D.Double[] points = new Point2D.Double[sidesCount]; | ||
|
||
points[0] = new Point2D.Double( | ||
length * Math.sin(rotationAngle / 2), | ||
length * Math.cos(rotationAngle / 2) | ||
); | ||
|
||
for (int i = 1; i < sidesCount; i++) { | ||
Point2D.Double vector = new Point2D.Double(points[i - 1].x, points[i - 1].y); | ||
Point2D.Double rotatedVector = rotateVector(vector, rotationAngle); | ||
points[i] = new Point2D.Double(rotatedVector.x, rotatedVector.y); | ||
} | ||
|
||
int[] x = new int[sidesCount]; | ||
int[] y = new int[sidesCount]; | ||
for (int i = 0; i < sidesCount; i++) { | ||
x[i] = (int) Math.round(points[i].x); | ||
y[i] = (int) Math.round(points[i].y); | ||
} | ||
|
||
Polygon polygon = new Polygon(x, y, sidesCount); | ||
polygonCache.put(sidesCount, polygon); | ||
return polygon; | ||
} | ||
|
||
public int[][] getOrderedPoints(int sidesCount, int length) { | ||
Shape polygon = getRegularPolygon(sidesCount, length); | ||
Polygon poly = (Polygon) polygon; | ||
|
||
int[] xPoints = poly.xpoints; | ||
int[] yPoints = poly.ypoints; | ||
int nPoints = poly.npoints; | ||
|
||
int[] xOrdered = new int[nPoints]; | ||
int[] yOrdered = new int[nPoints]; | ||
for (int i = 0; i < nPoints; i++) { | ||
xOrdered[i] = xPoints[i]; | ||
yOrdered[i] = yPoints[i]; | ||
} | ||
|
||
return new int[][] { xOrdered, yOrdered }; | ||
} | ||
} |