/******************************************************************************* Polar.java Generates coordinates for a "Polar diagram". Given that T stands for angle theta, the program will generate values of a function of the type: f(T) = a sin(bT + c) or f(T) = a cos(bT + c) This value is projected into x/y coordinates, which are the output. arguments are: a - scaling multiplier b - sin/cos multiplier c - offset function name - sin or cos startAngle endAngle increment Copyright (C) 2001 J. David Eisenberg under GNU Public License. ********************************************************************************/ import java.io.*; import java.text.*; public class Polar { public static void main( String args[] ) { double scale, coeff, offset; int whichFunction; // 0 = sin, 1 = cos int startAngle, endAngle, increment; int angle; double radians, r; double x, y; DecimalFormat df = new DecimalFormat( "###0.00" ); scale = 1.0; coeff = 1.0; offset = 0.0; whichFunction = 0; startAngle = 0; endAngle = 0; increment = 0; if (args.length != 7) { System.err.println("Usage: polar scale coeff offset function " + "start end incr"); System.exit(0); } try { scale = Double.parseDouble( args[0] ); coeff = Double.parseDouble( args[1] ); offset = Double.parseDouble( args[2] ); } catch (Exception e) { System.err.println("Invalid number for scale, " + "coefficient, or offset"); System.exit(0); } if (args[3].equalsIgnoreCase("sin")) { whichFunction = 0; } else if (args[3].equalsIgnoreCase("cos")) { whichFunction = 1; } else { System.err.println("Function must be sin or cos"); System.exit(0); } try { startAngle = Integer.parseInt( args[4] ); endAngle = Integer.parseInt( args[5] ); increment = Integer.parseInt( args[6] ); } catch (Exception e) { System.err.println("Invalid integer for start angle, " + "end angle, or angle increment."); } for (angle = startAngle; angle <= endAngle; angle += increment) { radians = angle * Math.PI / 180.0; r = (whichFunction == 0) ? scale * Math.sin( coeff * radians + offset ) : scale * Math.cos( coeff * radians + offset ); x = r * (Math.cos( angle * Math.PI / 180.0 ) ); y = r * (Math.sin( angle * Math.PI / 180.0 ) ); System.out.println(df.format(x) + " " + df.format(y) + ", " ); } } }