
// TimesTable.java
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

/* Allocate NUM_PINS 'pins' evenly spaced around a circle and then 
   connect some of those pins with line segments to generate
   'string' diagrams of curves. The spacing between used pins is
   specified with the spacing variable.

   Based on The Mathologer video:
   "Times Tables, Mandelbrot and the Heart of Mathematics"
   https://www.youtube.com/watch?v=qhbuKbxJsk8&t=6s

   Usage:
     > compile TimesTable.java
     > run TimesTable n
   e.g.
     > run TimesTable 2   // creates a cardoid
     > run TimesTable 34
     > run TimesTable 51
     > run TimesTable 99
*/


import java.util.*;


public class TimesTable
{
  private static final int NUM_PINS = 200;


  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: java TimesTable <spacing>");
      return;
    }
    int spacing = Integer.parseInt(args[0]);
    if (spacing <= 1)
      spacing = 2;

    StdDraw.setCanvasSize(512, 512);
    StdDraw.setScale(-1.0, 1.0);

    // calculate pin positions around circle
    double[][] pins = new double[NUM_PINS][2];  // for the (x,y) coords
    for (int i = 0; i < NUM_PINS; i++) {
      pins[i][0] = Math.cos(2 * Math.PI * i / NUM_PINS);
      pins[i][1] = Math.sin(2 * Math.PI * i / NUM_PINS);
      // StdDraw.point(pins[i][0], pins[i][1]);
    }

    // draw lines between the spaced pins
    StdDraw.setPenColor(StdDraw.GRAY);
    int endPin;
    for (int i = 0; i < NUM_PINS; i++) {
      endPin = (i*spacing)%NUM_PINS;
      StdDraw.line(pins[i][0], pins[i][1], pins[endPin][0], pins[endPin][1]);
    }
  } /* main */

}  // end of TimesTable class
