In general, the essence of the problem is as follows:
I want to draw an n square (or rather a circle) of its elements on a given array, where n is the length of the array.

x0,y0 is the center of the circle, r is the radius

 context.setFill(Paint.valueOf("red")); String[] elements = ..возвращает массив.. ; int n = elements.length; for (int i = 1; i < n+1; i++) { float x = (float) (x0 + Math.cos((180 * (i * 180/n)) / Math.PI) * r); float y = (float) (y0 + Math.sin((180 * (i * 180/n)) / Math.PI) * r); context.fillOval(x, y, 4, 4); context.fillText(elements[i-1], x - 8, y - 8); } 

But I have it is not displayed correctly, and with distortion. Those. it goes along the line of the circle itself, but not evenly. I can not understand where I made a mistake in the formula, or how it is better to draw?

    1 answer 1

    Try this:

     int n = elements.length; /* вычисляем угол одной грани */ float angleBase = (float)(2 * Math.PI / n); for (int i = 0; i < n; i++) { /* текущий угол поворота */ float angle = angleBase * i; float x = (float) (x0 + Math.cos(angle) * r); float y = (float) (y0 + Math.sin(angle) * r); /* ... */ } 

    The angle must be set in radians, so there was an error.

    The function can still be optimized, in theory it should work faster:

     for (int i = 0, angle = 0; i < n; i++, angle += angleBase)