/* Subject: Contest submission for problem #1, file 1.java */ /* cs61a-mb@imail.EECS.Berkeley.EDU */ /* Thu Nov 20 22:58:08 PST 2003 */ /*___CONTEST_SUBMISSION___ cs61a-mb 1 */ /* * Created on Nov 20, 2003 * * CS 198-1 Generic Geometry Algorithm for Programming Contests * */ import javax.swing.JOptionPane; import java.io.IOException; import java.io.FileReader; import java.io.FileWriter; import java.io.StreamTokenizer; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.PrintWriter; import java.util.LinkedList; import java.util.Iterator; // java.lang.StringBuffer /** * @author Vik Singh * * IntersectionTool.java reads from an input .txt file and generates code * according to the instructions/user given specifications listed in that file. The format * for the input file is as follows (not-dependent on character case): * * "numberOfPts" number/infinity * "databaseLines" yes/no * "databasePolygons" yes/no * "drawLineMethods" yes/no * "writeToFile" no/.txt filename * */ class IntersectionTool { public static void main(String[] args) { try { String file; file = JOptionPane.showInputDialog("Enter file name"); scan(file); } catch (Exception e) { } finally { System.exit(0); } } static void scan(String file) { try { Templates codeGen = new Templates(); ReadTokensFromInput readFile = new ReadTokensFromInput(file); String tempNext; while (readFile.hasMore()) { if (readFile .current() .equalsIgnoreCase(new String("numberOfPts"))) { tempNext = readFile.next(); codeGen.putSize(tempNext); } else if ( readFile.current().equalsIgnoreCase( new String("databaseLines"))) { tempNext = readFile.next(); if (tempNext.equalsIgnoreCase(new String("yes"))) { codeGen.record(true, 1); } else codeGen.record(false, 1); } else if ( readFile.current().equalsIgnoreCase( new String("databasePolygons"))) { tempNext = readFile.next(); if (tempNext.equalsIgnoreCase(new String("yes"))) { codeGen.record(true, 2); } else codeGen.record(false, 2); } else if ( readFile.current().equalsIgnoreCase( new String("writeToFile"))) { tempNext = readFile.next(); if (tempNext.equalsIgnoreCase(new String("no"))) { codeGen.record(false, 4); } else codeGen.putFile(tempNext, 4); } else if ( readFile.current().equalsIgnoreCase( new String("drawLineMethods"))) { tempNext = readFile.next(); if (tempNext.equalsIgnoreCase(new String("no"))) { codeGen.record(false, 3); } else codeGen.record(true, 3); } } codeGen.iterate(); } catch (IOException e) {} } } class ReadTokensFromInput { private StreamTokenizer st; private String fileName; public ReadTokensFromInput(String fileName) throws IOException { st = new StreamTokenizer(new BufferedReader(new FileReader(fileName))); } public String current() throws IOException { return st.sval; } public String next() throws IOException { st.nextToken(); return st.sval; } public int nextCheck() throws IOException { st.nextToken(); return (int) st.nval; } public boolean hasMore() throws IOException { return (this.nextCheck() != StreamTokenizer.TT_EOF); } } class Templates { try { private LinkedList storage; private StringBuffer classCode = new StringBuffer(" "); private StringBuffer mainCode = new StringBuffer(" "); private StringBuffer methodCode = new StringBuffer(" "); public Templates () { } void record(boolean trigger, int index) { Boolean val = new Boolean(trigger); storage.add(index, val); } void putSize(String size) { storage.add(0, size); } void putFile(String file, int index) { storage.add(index, file); } void iterate() { int size; Iterator st = storage.iterator(); String firstElement = (String) storage.get(0); if (firstElement.equalsIgnoreCase("infinity")) { size = 0; } else size = Integer.parseInt(firstElement); generatePtSize(size); st.next(); for (int i = 1; i < 5; i++) { switch (i) { case 1 : if (storage.get(1).equals(new Boolean(true))) { mainCode.append( "EveryLine lines = new EveryLine(pts);\n"); classCode.append( "class EveryLine { \n" + "private LinkedList listOfLines; \n" + "private Vector ofIntersections; \n" + "private double totalArea = 0; \n " + "public EveryLine(Vector ofPoints) { \n" + "Iterator itv = ofPoints.iterator(); \n" + "try { \n" + "lab1: while (itv.hasNext()) { \n" + "listOfLines.add( \n" + "new Line2D.Double( \n" + "(Point2D.Double) itv.next(), \n" + "(Point2D.Double) itv.next())); \n" + "}\n" + "Line2D.Double first = (Line2D.Double) listOfLines.getFirst();\n" + "Line2D.Double last = (Line2D.Double) listOfLines.getLast();\n" + "Point2D.Double pt1 = new Point2D.Double(first.x1, first.y1);\n" + "Point2D.Double pt2 = new Point2D.Double(last.x2, last.y2);\n" + "listOfLines.add(new Line2D.Double(pt1, pt2)); //forms last line\n" + "} catch (NullPointerException npe) {\n" + "listOfLines.add(new Line2D.Double(-1000, 0, 0, 0));\n" + "}\n" + "}\n" + "public boolean doTheyIntersect(Line2D.Double lineobj) {\n" + "Iterator listEach = listOfLines.iterator();\n" + "while (listEach.hasNext()) {\n" + "Line2D.Double currentLine = (Line2D.Double) listEach.next();\n" + "if (lineobj.intersectsLine(currentLine)) {\n" + "ofIntersections.add(currentLine);\n" + "return true;\n" + "} else\n" + "continue;\n" + "}\n" + "return false;\n" + "}\n" + "public Line2D.Double getIntersection() {\n" + "Iterator intersections = ofIntersections.iterator();\n" + "return (Line2D.Double) intersections.next();\n" + "}\n" + "public void addArea(double area) {\n" + "totalArea = totalArea + area;\n" + "}\n" + "public double getArea() {\n" + "return totalArea;\n" + "}\n" + "} //class EveryLine \n"); break; } else break; case 2 : if (storage.get(2).equals(new Boolean(true))) { mainCode.append( "Polygon poly = new Polygon;\n" + "Iterator ipts = pts.iterator();\n" + "while (ipts.hasNext()) { \n" + "Point2D.Double tempPt = (Point2D.Double) ipts.next();\n" + "poly.addPoint((int)tempPt.x, (int)tempPt.y);\n" + "}\n"); break; } else break; case 3 : if (storage.get(3).equals(new Boolean(true))) { methodCode.append( "static void makeLeftLine(Point2D.Double ptl, EveryLine lines) {\n" + "if (lines\n" + ".doTheyIntersect(new Line2D.Double(ptl.x, ptl.y, -10000, ptl.y))) {\n" + "lines.addArea(ptl.y * (lines.getIntersection().ptLineDist(ptl)));\n" + "makeDownLineFromLeft(\n" + "(new Point2D\n" + ".Double(\n" + "ptl.x - lines.getIntersection().ptLineDist(ptl),\n" + "ptl.y)),\n" + "lines);\n" + "}\n" + "}\n" + "static void makeRightLine(Point2D.Double ptr, EveryLine lines) {\n" + "Point2D.Double start = ptr;\n" + "if (lines\n" + ".doTheyIntersect(new Line2D.Double(ptr.x, ptr.y, 10000, ptr.y))) {\n" + "lines.addArea(ptr.y * (lines.getIntersection().ptLineDist(ptr)));\n" + "makeDownLineFromRight(\n" + "(new Point2D\n" + ".Double(\n" + "ptr.x + lines.getIntersection().ptLineDist(ptr),\n" + "ptr.y)),\n" + "lines);\n" + "}\n" + "}\n" + "static void makeDownLineFromLeft(Point2D.Double ptst, EveryLine lines) {\n" + "if (lines\n" + ".doTheyIntersect(new Line2D.Double(ptst.x, ptst.y, ptst.x, -10000))\n" + "&& ptst.y - lines.getIntersection().ptLineDist(ptst) == 0) {\n" + "lines.addArea(\n" + "((lines.getIntersection().x1 - ptst.x)\n" + "* lines.getIntersection().ptLineDist(ptst))\n" + "/ 2);\n" + "}\n" + "}\n" + "static void makeDownLineFromRight(\n" + "Point2D.Double ptst,\n" + "EveryLine linesAll) {\n" + "if (linesAll\n" + ".doTheyIntersect(new Line2D.Double(ptst.x, ptst.y, ptst.x, -10000))\n" + "&& ptst.y - linesAll.getIntersection().ptLineDist(ptst) == 0) {\n" + "linesAll.addArea(\n" + "((linesAll.getIntersection().x1 + ptst.x)\n" + "* linesAll.getIntersection().ptLineDist(ptst))\n" + "/ 2);\n" + "}\n" + "}\n" + "static void downLeft(Point2D.Double point, EveryLine lines) {\n" + "if (lines\n" + ".doTheyIntersect(\n" + "new Line2D.Double(\n" + "point.x,\n" + "point.y,\n" + "point.x - 10000,\n" + "point.y))) {\n" + "lines.addArea(\n" + "((point.x - lines.getIntersection().x2)\n" + "* lines.getIntersection().ptLineDist(point))\n" + "/ 2);\n" + "System.out.print(+ lines.getArea() +);\n" + "}\n" + "}\n" + "static void downRight(Point2D.Double point, EveryLine lines) {\n" + "if (lines\n" + ".doTheyIntersect(\n" + "new Line2D.Double(\n" + "point.x,\n" + "point.y,\n" + "point.x + 10000,\n" + "point.y))) {\n" + "lines.addArea(\n" + "((point.x + lines.getIntersection().x2)\n" + "* lines.getIntersection().ptLineDist(point))\n" + "/ 2); \n" + "System.out.print(+ lines.getArea() +); \n" + "} \n" + "} \n"); break; } else break; case 4 : if (storage.get(4).equals("no")) { StringBuffer tempResult = mainCode.append("}\n").append(methodCode); System.out.println(tempResult.append(classCode)); } else { StringBuffer tempResult = mainCode.append("}\n").append(methodCode); PrintWriter output = new PrintWriter( new BufferedWriter( new FileWriter((String) storage.get(3)))); output.print(tempResult.append(classCode)); System.out.println(tempResult.append(classCode)); } } } } catch (IOException e) {} } void generatePtSize(int size) { if (size == 0) { mainCode.append( "import java.io.*;\n" + "import java.util.Vector;\n" + "import java.awt.*;\n" + "import java.awt.geom.*;\n" + "public static void main(String [] args) {\n" + "Point2D.Double point;\n " + "Vector pts = new Vector( );\n" + "StreamTokenizer st= " + "new StreamTokenizer(new BufferedReader " + "(new InputStreamReader (System.in)));\n" + "while (st.nextToken != StreamTokenizer.TT_EOF) {\n" + "point = new Point2D.Double(st.next(), st.next());\n " + "}\n"); } else { mainCode.append( "import java.io.*;\n" + "import java.util.Vector;\n" + "import java.awt.geom.*;\n" + "public static void main(String [] args) {\n" + "Point2D.Double point;\n " + "Vector pts = new Vector(" + size + ");\n" + "StreamTokenizer st= " + "new StreamTokenizer(new BufferedReader " + "(new InputStreamReader (System.in)));\n" + "while (st.nextToken != StreamTokenizer.TT_EOF) {\n" + "point = new Point2D.Double(st.next(), st.next());\n " + "}\n"); } } }