import java.awt.Point; import java.awt.geom.Point2D; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Random; /** * */ /** * @author zeil * */ public class polyGenerator2 { /** * A straight line in the 2-D plane. * * @author zeil * */ private static class Line { // ax + by + c = 0 private double a; private double b; private double c; /** * Create a line described by the equation ax + by + c = 0. * Note that the line ax + by + c = 0 is considered to be * the same line as (equal to) the line tax + tby + tc = 0 for * all positive t. * * @param a The coefficient of the x term * @param b The coefficient of the y term * @param c The constant term */ public Line (double a, double b, double c) { double r = Math.sqrt(a*a + b*b); this.a = a / r; this.b = b / r; this.c = c /r; } /** * Create a line passing through two points. * @param p1 a point that the line passes through * @param p2 another point that the line passes through * * @precondition: !p1.equals(p2) */ public Line (Point2D.Double p1, Point2D.Double p2) { double dx = p2.x - p1.x; double dy = p2.y - p1.y; a = dy; b = -dx; c = p1.y * dx - p1.x * dy; double r = Math.sqrt(a*a + b*b); a /= r; b /= r; c /= r; } /** * Create a string describing this line in the form "ax + by + c = 0" * where "a", "b", and "c" are replaced by appropriate floating point * values. * * @return string representation of this line. */ public String toString() { return "" + a + "x + " + b + "y + " + c + "= 0"; } /** * Compute the orthogonal distance of a point from this line (i.e., the * distance between that point and the closest point lying on this line). * * The distance value should indicate the direction of the point by its * sign. Points above and to the right of the lien should have a positive * distance. Points below and to the left should have a negative distance. * * @param p A point * @return signed distance of that point form this line */ double distance (Point2D.Double p) { return a * p.x + b * p.y + c; } public boolean equals(Object obj) { if (obj instanceof Line) { Line line2 = (Line)obj; return line2.a == a && line2.b == b && line2.c == c; } else return false; } } public polyGenerator2() { } private static void generate(int N, String fileNameBase, char key) throws IOException { N = Math.max(3, N); N = Math.min(200, N); Random rand = new Random(); Point[] vertices = new Point[N]; for (int i = 0; i < N; ++i) { int x = rand.nextInt(100); int y = rand.nextInt(100); vertices[i] = new Point(x, y); } if (key != ' ') { fileNameBase += key; } File inFile = new File(fileNameBase + ".in"); PrintWriter fout = new PrintWriter(new BufferedOutputStream(new FileOutputStream(inFile))); int start = 0; if (key == 'm') start = 1; for (int i = start; i < vertices.length; ++i) { int j = (i + 1) % vertices.length; Point p = vertices[i]; Point q = vertices[j]; if (p.x != q.x || p.y != q.y) { System.out.println("(" + p.x + "," + p.y + "),(" + q.x + "," + q.y + ");"); fout.println("(" + p.x + "," + p.y + "),(" + q.x + "," + q.y + ");"); } } if (key == 'x') { Point p = vertices[0]; Point q = new Point(p.y, p.x); System.out.println("(" + p.x + "," + p.y + "),(" + q.x + "," + q.y + ");"); fout.println("(" + p.x + "," + p.y + "),(" + q.x + "," + q.y + ");"); } fout.close(); } /** * Usage: * java polyGenerator1 N fileName key * places a random polygon with N vertices into fileName.in and * fileName.ans. If key = "x" an extra edge is attached to one point. * if key = "m", one edge is removed. * * @param args * @throws IOException */ public static void main (String[] argv) throws IOException { int n = Integer.parseInt(argv[0]); char key = ' '; if (argv.length > 2) { key = argv[2].charAt(0); } generate (n, argv[1], key); } }