summaryrefslogtreecommitdiff
path: root/ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Hexagon.java
diff options
context:
space:
mode:
Diffstat (limited to 'ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Hexagon.java')
-rw-r--r--ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Hexagon.java76
1 files changed, 76 insertions, 0 deletions
diff --git a/ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Hexagon.java b/ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Hexagon.java
new file mode 100644
index 0000000..12f1031
--- /dev/null
+++ b/ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Hexagon.java
@@ -0,0 +1,76 @@
1package de.lmu.tcs;
2
3import java.awt.*;
4
5class Hexagon {
6 private final Point center;
7 private final int sideLength;
8
9 public Hexagon(Point center2, int sideLength2)
10 {
11 this.center = center2;
12 this.sideLength = sideLength2;
13 }
14
15 public Point[] vertices()
16 {
17 Point vertex = new Point((int) (Math.sqrt(3) * ((double) sideLength) / 2), sideLength / 2);
18 Point[] relative = { new Point(0, sideLength)
19 , vertex
20 , mirrory(vertex)
21 , new Point(0, -1 * sideLength)
22 , mirrorx(mirrory(vertex))
23 , mirrorx(vertex)
24 };
25
26 for (Point r : relative)
27 r.translate(center.x, center.y);
28
29 return relative;
30 }
31
32 private static Point mirrorx(Point r)
33 {
34 return new Point(r.x * -1, r.y);
35 }
36
37 private static Point mirrory(Point r)
38 {
39 return new Point(r.x, r.y * -1);
40 }
41
42 public int height()
43 {
44 return 2 * sideLength;
45 }
46
47 public int width()
48 {
49 return (int) (Math.sqrt(3) * (double) sideLength);
50 }
51
52 public boolean contains(Point r)
53 { // clever maths is clever (and very hexagon-specific)
54 int rx = Math.abs(r.x - center.x);
55 int ry = Math.abs(r.y - center.y);
56
57 if (rx > width() / 2 || ry > height())
58 return false;
59 return width() * height() - height() * rx - height() * ry >= 0;
60 }
61
62 public Rectangle boundingBox()
63 {
64 Point uL = new Point(center);
65 uL.translate(-1 * width() / 2, -1 * height() / 2);
66 return new Rectangle(uL, new Dimension(width(), height()));
67 }
68
69 public Polygon asPolygon()
70 {
71 Polygon ret = new Polygon();
72 for (Point r : vertices())
73 ret.addPoint(r.x, r.y);
74 return ret;
75 }
76}