0

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);

    }
}
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
  • 3
    That doesn't semm easy to do since the algorithm sems to first find a certain number of points that are the right distance from the center and then connects them via lines. Since not all points in a house shape will have a common center, I would suggest wirting your own algorithm that maybe draws the house based on cartesian coordinates rather than in this radial way. – Gumbo Feb 09 '17 at 11:14
  • Also, the program you show is exactly the same as the one from [this question](http://stackoverflow.com/q/20734438/4125191). On StackOverflow, when you use someone else's work, you have to give credit, say what the source is, and link if possible. Otherwise it is considered plagiarism. – RealSkeptic Feb 09 '17 at 13:02

0 Answers0