import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.ListIterator; import java.util.Collection; import java.awt.Point; import gameoflifetest.GraphicsWindow; class Polygon { private final List points; public Polygon (Position[] pointsPrime) { points = Arrays.asList(pointsPrime); } public Polygon (Collection pointsPrime) { points = new ArrayList(pointsPrime); } public Polygon translate (int dx, int dy) { ListIterator it = points.listIterator(); ArrayList translated = new ArrayList(); while (it.hasNext()) translated.add(it.next().translate(dx, dy)); return new Polygon(translated); } public Polygon erweitere (Position pos) { ArrayList extended = new ArrayList(points); extended.add(pos); return new Polygon(extended); } public Polygon reduziere (int n) { if (n == 0) return new Polygon(new ArrayList()); List reduced = new ArrayList(points); int c = 0; for (int i = 0; i < points.size(); i++) { if (i % n == 1) { reduced.remove(i - c); c++; } } return new Polygon(reduced); } public boolean istGleichZu(Polygon other) { ListIterator itThis = points.listIterator(); ListIterator itOther = other.points.listIterator(); while (itThis.hasNext() && itOther.hasNext()) if (! itThis.next().istGleichZu(itOther.next())) return false; if (itThis.hasNext() || itOther.hasNext()) return false; return true; } public double berechneFlaeche() { if (points.size() < 3) return 0; double area = 0; for (int i = 0; i <= points.size(); i++) { Position r0 = points.get(i % points.size()); Position r1 = points.get((i + 1) % points.size()); area += 0.5 * ((r0.getX() + r1.getX()) * (r1.getY() - r0.getY())); } return area; } public void zeichneDich (GraphicsWindow gw) { for (int i = 0; i <= points.size(); i++) { Position r0 = points.get(i % points.size()); Position r1 = points.get((i + 1) % points.size()); gw.drawLine(new Point(r0.getX(), r0.getY()), new Point(r1.getX(), r1.getY())); } } }