Draw Regular Polygons inscribed in a circle with Canvas in Javafx

Asked

Viewed 433 times

2

Mind you, this is a method I’ve developed to create the vertices of regular polygons inscribed in a radius circumference r

@Override
    public float calcularRaio(float lado, int numeroLados) {// calcular raio
        float raio = (float) (lado / (2 * Math.sin(Math.PI / this.getmNLados())));
        return raio;
    }

    @Override
    public float getRaio() {//obter o valor do raio
        return calcularRaio(this.getLados(), this.getmNLados());
    }

public List<Ponto2D> getVertices() {//obter os vertices

        vertices = new ArrayList<>(mNLados);

        double calcularAngulo = 360.0 / mNLados;
        Ponto2D pontoCriado = new Ponto2D(super.getmCentro().getmX(), super.getmCentro().getmY() + this.getRaio());//(0,r)

        if (mNLados % 2 == 0) //numero par de lados tem que rodar para ficar com o lado de baixo horizontal
        {
            pontoCriado = rodarPonto2D(pontoCriado, super.getmCentro(), calcularAngulo / 2.0F);
        }
        vertices.add(pontoCriado);          //adiciona pontos/vertices á lista de arrays pra completar a figura
        for (int i = 1; i < mNLados; i++) {
            pontoCriado = rodarPonto2D(pontoCriado, super.getmCentro(), calcularAngulo);
            vertices.add(pontoCriado);
        }
        return vertices;
    }

    public Ponto2D rodarPonto2D(Ponto2D pontoInicial, Ponto2D pontoCentral, double angulo) {

        double anguloRadiano = Math.toRadians(angulo);
        double coseno = Math.cos(anguloRadiano);
        double seno = Math.sin(anguloRadiano);

        double dx = (pontoInicial.getmX() - pontoCentral.getmX());
        double dy = (pontoInicial.getmY() - pontoCentral.getmY());

        float mX = (float) (pontoCentral.getmX() + (dx * coseno - dy * seno));
        float mY = (float) (pontoCentral.getmY() + (dx * seno + dy * coseno));

        pontoInicial = new Ponto2D(mX, mY);

        return pontoInicial;
    }

And my doubt is here in this method drawing Linux using Canvas, I used the same logic I used in the getVertices() method and it’s not working

public void desenharPoligonosRegulares(GraphicsContext gc) {

        float raio = (float) (this.lados / (2 * Math.sin(Math.PI / this.mNLados)));//calcula o raio
        double calcularAngulo = 360.0 / mNLados;//calcula o angulo

        gc.beginPath();
        gc.moveTo(raio * Math.cos(0), raio * Math.sin(0));//é o mesmo que ter (raio, 0)
        gc.setFill(Color.Black);
        gc.setStroke(Color.Black);
        gc.setLineWidth(1.5);

        for (int i = 1; i < this.mNLados; i++) {
            gc.lineTo(raio * Math.cos(calcularAngulo * i), raio * Math.sin(calcularAngulo * i));
        }

        gc.closePath();
        gc.fill();
        gc.stroke();
    }

1 answer

2


The methods Math.sin and Math.cos accept parameters in radians. You’re passing them on degrees. In your first code this is taken into account:

double anguloRadiano = Math.toRadians(angulo);

But not the second.

double calcularAngulo = 360.0 / mNLados;//calcula o angulo

...

for (int i = 1; i < this.mNLados; i++) {
    gc.lineTo(raio * Math.cos(calcularAngulo * i), raio * Math.sin(calcularAngulo * i));
}

To solve, just do the same in the second code:

double calcularAngulo = 360.0 / mNLados;//calcula o angulo

...

for (int i = 1; i < this.mNLados; i++) {
    gc.lineTo( raio * Math.cos(Math.toRadians(calcularAngulo * i)), 
               raio * Math.sin(Math.toRadians(calcularAngulo * i)) );
}

or, alternatively, use radians in principle:

double calcularAngulo = Math.toRadians(360.0 / mNLados);//calcula o angulo

// ou:

double calcularAngulo = 2*Math.PI / mNLados;//calcula o angulo
  • Thanks mgibsonbr, sometimes small mistakes are what are hard to find

Browser other questions tagged

You are not signed in. Login or sign up in order to post.