summaryrefslogtreecommitdiff
path: root/ws2015/eip/blaetter/06/Polygon.java
diff options
context:
space:
mode:
Diffstat (limited to 'ws2015/eip/blaetter/06/Polygon.java')
-rw-r--r--ws2015/eip/blaetter/06/Polygon.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/ws2015/eip/blaetter/06/Polygon.java b/ws2015/eip/blaetter/06/Polygon.java
new file mode 100644
index 0000000..461fcc6
--- /dev/null
+++ b/ws2015/eip/blaetter/06/Polygon.java
@@ -0,0 +1,106 @@
1import java.util.ArrayList;
2import java.util.Arrays;
3
4import java.util.List;
5import java.util.ListIterator;
6
7import java.util.Collection;
8
9import java.awt.Point;
10
11import gameoflifetest.GraphicsWindow;
12
13class Polygon {
14 private final List<Position> points;
15
16 public Polygon (Position[] pointsPrime)
17 {
18 points = Arrays.asList(pointsPrime);
19 }
20
21 public Polygon (Collection<Position> pointsPrime)
22 {
23 points = new ArrayList<Position>(pointsPrime);
24 }
25
26 public Polygon translate (int dx, int dy)
27 {
28 ListIterator<Position> it = points.listIterator();
29 ArrayList<Position> translated = new ArrayList<Position>();
30 while (it.hasNext())
31 translated.add(it.next().translate(dx, dy));
32 return new Polygon(translated);
33 }
34
35 public Polygon erweitere (Position pos)
36 {
37 ArrayList<Position> extended = new ArrayList<Position>(points);
38 extended.add(pos);
39 return new Polygon(extended);
40 }
41
42 public Polygon reduziere (int n)
43 {
44 if (n == 0)
45 return new Polygon(new ArrayList<Position>());
46
47 List<Position> reduced = new ArrayList<Position>(points);
48
49 int c = 0;
50
51 for (int i = 0; i < points.size(); i++)
52 {
53 if (i % n == 1)
54 {
55 reduced.remove(i - c);
56 c++;
57 }
58 }
59
60 return new Polygon(reduced);
61 }
62
63 public boolean istGleichZu(Polygon other)
64 {
65 ListIterator<Position> itThis = points.listIterator();
66 ListIterator<Position> itOther = other.points.listIterator();
67
68 while (itThis.hasNext() && itOther.hasNext())
69 if (! itThis.next().istGleichZu(itOther.next()))
70 return false;
71
72 if (itThis.hasNext() || itOther.hasNext())
73 return false;
74
75 return true;
76 }
77
78 public double berechneFlaeche()
79 {
80 if (points.size() < 3)
81 return 0;
82
83 double area = 0;
84
85 for (int i = 0; i <= points.size(); i++)
86 {
87 Position r0 = points.get(i % points.size());
88 Position r1 = points.get((i + 1) % points.size());
89
90 area += 0.5 * ((r0.getX() + r1.getX()) * (r1.getY() - r0.getY()));
91 }
92
93 return area;
94 }
95
96 public void zeichneDich (GraphicsWindow gw)
97 {
98 for (int i = 0; i <= points.size(); i++)
99 {
100 Position r0 = points.get(i % points.size());
101 Position r1 = points.get((i + 1) % points.size());
102
103 gw.drawLine(new Point(r0.getX(), r0.getY()), new Point(r1.getX(), r1.getY()));
104 }
105 }
106}