Dividing a Circle

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Two Pi radians.

Dividing a Circle

The Java trigonometric functions are static methods of the Math. They take radians for their arguments (as do such functions in most programming languages). Here is the complete method:

  private void drawStar( int x, int y, int size )
  {
    int endX, endY ;
    
    // Six lines radiating from (x,y)
    for ( int i = 0; i<6; i++ )
    {
      endX = x + (int)(size*Math.cos( (2*Math.PI/6)*i ));
      endY = y - (int)(size*Math.sin( (2*Math.PI/6)*i ));  // Note "-"
      graph.drawLine( x, y, endX, endY );
    }
  }

The circle is divided into six pieces. The constant pi is available in Java as Math.PI. There are two pi radians per circle. One sixth of a circle is (2*Math.PI/6).

The minus sign is used in the calculation of endY because y increases in value going down. Using a "+" would also work because of symmetry.

QUESTION 8:

What does (int) do in the statement

endY = y - (int)(size*Math.sin( (2*Math.PI/6)*i ));