I have a script that draws a hexagon from the center of a given point. If I change the "sides" variable to 5 it draws a pentagon fine. The problem is that I want the left and right sides of the pentagon to be 90 degree angles so that the pentagon forms a house shape. How can I modify this script so that it forms a house? I was never very good at math.
public class Hexagon extends Polygon {
private static final long serialVersionUID = 1L;
public int SIDES = 6;
public Point[] points = new Point[SIDES];
private Point center = new Point(0, 0);
private int radius;
private int rotation = 90;
public Hexagon(Point center, int radius, int sides) {
SIDES = sides;
npoints = SIDES;
xpoints = new int[SIDES];
ypoints = new int[SIDES];
this.center = center;
this.radius = radius;
updatePoints();
}
public Hexagon(Point center, int radius) {
npoints = SIDES;
xpoints = new int[SIDES];
ypoints = new int[SIDES];
this.center = center;
this.radius = radius;
updatePoints();
}
public Hexagon(int x, int y, int radius) {
this(new Point(x, y), radius);
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
updatePoints();
}
public int getRotation() {
return rotation;
}
public void setRotation(int rotation) {
this.rotation = rotation;
updatePoints();
}
public void setCenter(Point center) {
this.center = center;
updatePoints();
}
public void setCenter(int x, int y) {
setCenter(new Point(x, y));
}
private double findAngle(double fraction) {
return fraction * Math.PI * 2 + Math.toRadians((rotation + 180) % 360);
}
private Point findPoint(double angle) {
int x = (int) (center.x + Math.cos(angle) * radius);
int y = (int) (center.y + Math.sin(angle) * radius);
return new Point(x, y);
}
protected void updatePoints() {
for (int p = 0; p < SIDES; p++) {
double angle = findAngle((double) p / SIDES);
Point point = findPoint(angle);
xpoints[p] = point.x;
ypoints[p] = point.y;
points[p] = point;
}
}
public void draw(GraphicsContext gc) {
gc.setFill(Color.RED);
gc.fillPolygon(xpoints, ypoints, SIDES);
}
}