diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-11-26 07:21:47 +0000 | 
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-11-26 07:21:47 +0000 | 
| commit | da7761a4269b0f0d277be02fa1b5cc82e35295f4 (patch) | |
| tree | 98e8ec04ad802867edb3865af937fd364ad283d9 | |
| parent | cde46f1dd104e3f214272a5cf33e389ac74096c1 (diff) | |
| download | uni-da7761a4269b0f0d277be02fa1b5cc82e35295f4.tar uni-da7761a4269b0f0d277be02fa1b5cc82e35295f4.tar.gz uni-da7761a4269b0f0d277be02fa1b5cc82e35295f4.tar.bz2 uni-da7761a4269b0f0d277be02fa1b5cc82e35295f4.tar.xz uni-da7761a4269b0f0d277be02fa1b5cc82e35295f4.zip | |
EiP H6-1
| -rw-r--r-- | ws2015/eip/blaetter/06/Polygon.java | 106 | ||||
| -rw-r--r-- | ws2015/eip/blaetter/06/Test.java | 40 | ||||
| -rw-r--r-- | ws2015/eip/blaetter/06/manifest | 1 | 
3 files changed, 147 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 @@ | |||
| 1 | import java.util.ArrayList; | ||
| 2 | import java.util.Arrays; | ||
| 3 | |||
| 4 | import java.util.List; | ||
| 5 | import java.util.ListIterator; | ||
| 6 | |||
| 7 | import java.util.Collection; | ||
| 8 | |||
| 9 | import java.awt.Point; | ||
| 10 | |||
| 11 | import gameoflifetest.GraphicsWindow; | ||
| 12 | |||
| 13 | class 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 | } | ||
| diff --git a/ws2015/eip/blaetter/06/Test.java b/ws2015/eip/blaetter/06/Test.java new file mode 100644 index 0000000..c8232a2 --- /dev/null +++ b/ws2015/eip/blaetter/06/Test.java | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | import gameoflifetest.GraphicsWindow; | ||
| 2 | |||
| 3 | class Test { | ||
| 4 | public static void main (String[] args) | ||
| 5 | { | ||
| 6 | GraphicsWindow gw = new GraphicsWindow(); | ||
| 7 | |||
| 8 | Position[] points = { new Position(10, 10) | ||
| 9 | , new Position(20, 10) | ||
| 10 | , new Position(30, 10) | ||
| 11 | , new Position(30, 20) | ||
| 12 | , new Position(20, 20) | ||
| 13 | , new Position(10, 20) | ||
| 14 | }; | ||
| 15 | |||
| 16 | Polygon p = new Polygon(points); | ||
| 17 | |||
| 18 | p.zeichneDich(gw); | ||
| 19 | gw.mouseClick(); | ||
| 20 | |||
| 21 | p = p.translate(10, 10); | ||
| 22 | gw.clear(); | ||
| 23 | p.zeichneDich(gw); | ||
| 24 | gw.mouseClick(); | ||
| 25 | |||
| 26 | p = p.erweitere(new Position(5, 20)); | ||
| 27 | gw.clear(); | ||
| 28 | p.zeichneDich(gw); | ||
| 29 | gw.mouseClick(); | ||
| 30 | |||
| 31 | p = p.reduziere(2); | ||
| 32 | gw.clear(); | ||
| 33 | p.zeichneDich(gw); | ||
| 34 | gw.mouseClick(); | ||
| 35 | |||
| 36 | System.out.format("%.2f\n", p.berechneFlaeche()); | ||
| 37 | |||
| 38 | System.exit(0); | ||
| 39 | } | ||
| 40 | } | ||
| diff --git a/ws2015/eip/blaetter/06/manifest b/ws2015/eip/blaetter/06/manifest new file mode 100644 index 0000000..f55b77f --- /dev/null +++ b/ws2015/eip/blaetter/06/manifest | |||
| @@ -0,0 +1 @@ | |||
| Polygon.java \ No newline at end of file | |||
