// Writes polygons to the saveFile private static void savePolygons() { if (!saveFile.exists()) { try { saveFile.createNewFile(); } catch (IOException ex) { // Can't create file } } try { PrintStream fileOut = new PrintStream(saveFile); for (Polygon p : polygons) { fileOut.println(p.getName()); fileOut.println(p.getPoints().size()); for (Point3Df q : p.getPoints()) { fileOut.print(q.x); fileOut.print(","); fileOut.print(q.y); fileOut.print(","); fileOut.println(q.z); } fileOut.println(p.getEdges().size()); for (Edge q : p.getEdges()) { fileOut.print(q.p1); fileOut.print(","); fileOut.println(q.p2); } } fileOut.close(); } catch (FileNotFoundException ex) { // Can't find file } }
@Test public void testMoveIntInt() throws PolygonShapeException { // Mehrere Punkte erstellen ArrayList<Point> points = new ArrayList<Point>(); for (int i = 0; i < 20; ++i) points.add(new Point(i, 2 * i)); poly.setPoints(points); // Polygon verschieben poly.move(5, 7); assertThat(points, not(equalTo(poly.getPoints()))); // Punkte auch verschieben, sollte wieder gleich sein for (Point p : points) p.move(5, 7); assertThat(points, equalTo(poly.getPoints())); }
@Test public void testSetPoints() throws PolygonShapeException { // Mehrere Punkte erstellen ArrayList<Point> points = new ArrayList<Point>(); for (int i = 0; i < 25; ++i) points.add(new Point(2 * i, i)); poly.setPoints(points); // Darf nicht dasselbe Objekt sein assertNotSame(points, poly.getPoints()); // Muss gleich groß sein assertEquals(points.size(), poly.getPoints().size()); // Und muss dieselben Elemente enthalten assertEquals(points, poly.getPoints()); }
@Test public void testEmptyPolygon() { // alle Punkte ermitteln ArrayList<Point> points = poly.getPoints(); // Darf nicht null sein assertNotNull(points); // Größe muss 0 sein assertEquals(points.size(), 0); // draw sollte auch funktionieren poly.draw(); }
/** Testet rotate */ @Test public void testRotate() throws PolygonShapeException { ArrayList<Point> points = new ArrayList<Point>(); points.add(new Point(3, 1)); points.add(new Point(5, 5)); points.add(new Point(-2, 4)); poly.setPoints(points); // Um 90° Drehen poly.rotate(new Point(0, 1), 90); points = poly.getPoints(); assertEquals(new Point(0, -2), points.get(0)); assertEquals(new Point(4, -4), points.get(1)); assertEquals(new Point(3, 3), points.get(2)); }
private static void rebound() { float f = 0.0f; float n = 0.0f; float b = 0.0f; float t = 0.0f; float l = 0.0f; float r = 0.0f; for (Polygon poly : polygons) { poly.recalc(); if (poly.getMinZ() < f) { f = poly.getMinZ(); } if (poly.getMaxZ() > n) { n = poly.getMaxZ(); } if (poly.getMinY() < b) { b = poly.getMinY(); } if (poly.getMaxY() > t) { t = poly.getMaxY(); } if (poly.getMinX() < l) { l = poly.getMinX(); } if (poly.getMaxX() > r) { r = poly.getMaxX(); } } float maxSpan = Math.max(Math.max(f - n, t - b), r - l); for (Polygon poly : polygons) { for (Point3Df p : poly.getPoints()) { p.x = p.x * (2 / maxSpan) - (r + l) / (r - l); p.y = p.y * (2 / maxSpan) - (t + b) / (t - b); p.z = p.z * (2 / maxSpan) - (n + f) / (n - f); } poly.recalc(); } }
/** * Returns a hierarchical index of a polygon. * * <p>When the polygon is clockwise, the index is computed on this polygon.<br> * When the polygon is counterclockwised, the index is computed on the complement of this polygon * * @param index Healpix index * @param shape polygon * @return the HealpixMoc; * @throws Exception Healpix Exception */ protected static HealpixMoc computePolygonIndex(final HealpixIndex index, final Shape shape) throws Exception { RangeSet rangeSet; HealpixMoc moc = new HealpixMoc(); Polygon polygon = (Polygon) shape; final Resampling resample = new Resampling(polygon); polygon = resample.processResampling(); final List<Polygon> polygons = polygon.triangulate(); rangeSet = new RangeSet(); for (Polygon p : polygons) { final RangeSet rangeSetTmp = new RangeSet(rangeSet); rangeSet.setToUnion(rangeSetTmp, computePolygon(p.getPoints(), index)); } final RangeSet.ValueIterator valueIter = rangeSet.valueIterator(); while (valueIter.hasNext()) { moc.add(new MocCell(index.getOrder(), valueIter.next())); } if (polygon.isClockwised()) { moc = moc.complement(); } return moc; }