HIDE NAV

Java FX - Generating Regular Polygon Vertices

Use the following function to create an array of Double values to use with the class javafx.scene.shape.Polygon.


    static Double[] makeReglarPolyPoints(int numSides, int radius) {
        double angleIncrement = Math.PI * 2 / numSides;
        Double[] points = new Double[2* numSides];
        for (int i = 0; i < numSides; i++) {
            double px = radius * Math.cos(angleIncrement * i);
            double py = radius * Math.sin(angleIncrement * i);
            points[2*i] = px;
            points[2*i+1] = py;
        }
        return points;
    }