// AddKGon.java // Command to add a regular k-gon to the drawing. // Written by Scot Drysdale to // use for VoronoiTool import java.awt.Point; import java.awt.geom.Point2D; import javax.swing.JOptionPane; public class AddKGon extends Command { private PointShape center = null; // starting point for polygon // When the mouse is clicked the first time, record the original point. // Each successive click create a new segment until get back to original // point. public void executeClick(Point p, Drawing dwg) { if(center == null) // Starting a polygon? { center = new PointShape(p); dwg.setTempShape(center); } else { // Get a point on the circle and create the polygon createKGon(center, Math.sqrt(center.euclideanSq(p)), dwg); center = null; } } public static void createKGon(PointShape center, double radius, Drawing dwg) { int sides = -1; // Get the number of sides from a dialog box String input = JOptionPane.showInputDialog("Enter number of sides (> 2):"); while(input != null && sides < 3) { try { sides = Integer.parseInt(input); } catch (NumberFormatException exception) { JOptionPane.showMessageDialog(null, "Invalid integer"); sides = -1; } if(sides < 3) input = JOptionPane.showInputDialog("Re-enter number of sides (> 2):"); } if(input == null) // Cancel command selected? { dwg.setTempShape(null); return; } PointShape pOrig = new PointShape( new Point2D.Double(center.getX() + radius, center.getY())); PointShape last = pOrig; double arc = 2*Math.PI/sides; // double length = arc*radius; // remove later - not exact // double angle = Math.PI/2; // remove later // double alpha = arc; // Remove later // for common centers // double newLength = length*1.01; // double cDist = 2*length*(newLength/length)/(Math.pow(newLength/length, 2) - 1); // PointShape currentO = new PointShape( // new Point2D.Double(last.getX(), last.getY() + length)); // dwg.add(new Segment(last, currentO)); // last = currentO; // length = newLength; // cDist += length; // Shift center by the length to make new point origin for(int j = 1; j < sides; j++) { double alpha = j * arc; // arc *= 1.01; // REMOVE later PointShape current = new PointShape( new Point2D.Double(center.getX() + radius*Math.cos(alpha), center.getY() + radius*Math.sin(alpha))); /* // Fixed angle and length ratios // length *= 1.001; length += arc*radius/sides; PointShape current = new PointShape( new Point2D.Double(last.getX() + length*Math.cos(angle), last.getY() + length*Math.sin(angle))); */ /* // Straight line with constant length ratios // length *= 1.01; length += arc*radius/sides; PointShape current = new PointShape( new Point2D.Double(last.getX(), last.getY() + length)); */ // Straight line with same centers /* PointShape current = new PointShape( new Point2D.Double(last.getX(), last.getY() + length)); dwg.add(new Segment(last, current)); newLength = length * Math.sqrt(2*newLength/cDist + 1); length = newLength; cDist = cDist + length; */ // Shared by all dwg.add(new Segment(last, current)); last = current; // angle += arc; // alpha += arc; // REMOVE later } dwg.add(new Segment(last, pOrig)); dwg.setTempShape(null); } }