Add ellipse API

This commit is contained in:
pop4959 2020-09-22 00:39:58 -07:00
parent 161fc1c968
commit 66edc5654b
1 changed files with 37 additions and 12 deletions

View File

@ -123,7 +123,42 @@ public class Shape {
public static Shape createRect(double x1, double y1, double x2, double y2) {
return createRect(new Vector2d(x1, y1), new Vector2d(x2, y2));
}
/**
* Creates a {@link Shape} representing an ellipse.
* @param centerPos the center of the ellipse
* @param radiusX the x radius of the ellipse
* @param radiusY the y radius of the ellipse
* @param points the amount of points used to create the ellipse (at least 3)
* @return the created {@link Shape}
*/
public static Shape createEllipse(Vector2d centerPos, double radiusX, double radiusY, int points) {
if (points < 3) throw new IllegalArgumentException("A shape has to have at least 3 points!");
Vector2d[] pointArray = new Vector2d[points];
double segmentAngle = 2 * Math.PI / points;
double angle = 0d;
for (int i = 0; i < points; i++) {
pointArray[i] = centerPos.add(Math.sin(angle) * radiusX, Math.cos(angle) * radiusY);
angle += segmentAngle;
}
return new Shape(pointArray);
}
/**
* Creates a {@link Shape} representing an ellipse.
* @param centerX the x-position of the center of the ellipse
* @param centerY the y-position of the center of the ellipse
* @param radiusX the x radius of the ellipse
* @param radiusY the y radius of the ellipse
* @param points the amount of points used to create the ellipse (at least 3)
* @return the created {@link Shape}
*/
public static Shape createEllipse(double centerX, double centerY, double radiusX, double radiusY, int points) {
return createEllipse(new Vector2d(centerX, centerY), radiusX, radiusY, points);
}
/**
* Creates a {@link Shape} representing a circle.
* @param centerPos the center of the circle
@ -132,17 +167,7 @@ public class Shape {
* @return the created {@link Shape}
*/
public static Shape createCircle(Vector2d centerPos, double radius, int points) {
if (points < 3) throw new IllegalArgumentException("A shape has to have at least 3 points!");
Vector2d[] pointArray = new Vector2d[points];
double segmentAngle = 2 * Math.PI / points;
double angle = 0d;
for (int i = 0; i < points; i++) {
pointArray[i] = centerPos.add(Math.sin(angle) * radius, Math.cos(angle) * radius);
angle += segmentAngle;
}
return new Shape(pointArray);
return createEllipse(centerPos, radius, radius, points);
}
/**