-
Notifications
You must be signed in to change notification settings - Fork 0
/
HGBHexBase.java
109 lines (87 loc) · 4.02 KB
/
HGBHexBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package hgb;
//import android.util.Log;
// This class defines the origins or the vertices which form a base
// hexagon located at origin (0,0), and the origin of all the petals
// around that hexagon. All hexagons are drawn
// using these base vertex coordinates. Simply translating them to new
// locations. And the origin of all petals of all roses are a simple
// translation of the base petal origins found within this class.
// The results are stored in HGBShared. See HGBShared.[set,get]BaseVertices()
// and HGBShared.[set,get]BasePetalOrigins().
public class HGBHexBase
{
public HGBHexBase(HGBShared hgbShared)
{
this.hgbShared = hgbShared;
baseVertices = new float[HGBShared.SIDES][];
basePetalOrigins = new float[HGBShared.SIDES][];
hgbRectBase = new HGBRectBase(hgbShared);
}
// To calculate the base rectangles
private HGBRectBase hgbRectBase = null;
//private static final String TAG = "TAG_HexBase";
private HGBShared hgbShared = null;
// Base vertices are the vertices of a hexagon with an origin of (0,0)
private float[][] baseVertices;
// Base origins are the origins center of a hexagon who's origin is (0,0)
// and the origins of all 6 hexagons about the that center origin.
private float[][] basePetalOrigins;
protected void setVertexRadius(double vertexRadius)
{
// Decimals below 1 will work; but TouchLocate() will fail to
// correctly identify hexagons as the rounded int magnitudes can
// no longer identify unique true hexagon rings below 2f.
// Hence, treat 2f as your minimum, unless touchLocate() is not used.
if (vertexRadius < 2f) vertexRadius = 2f;
defineCell(vertexRadius);
}
// Creates a hexagon per class members ANGLES and vertexRadius
// and returns via class member vertices.
// These are used to draw all hexagons
private void defineCell(double vertexRadius)
{
double[] vertexRadiansAry = hgbShared.getVertexRadiansAry();
for (int inx = 0; inx < HGBShared.SIDES; inx++)
{
//float[] coordinate = new float[2];
// x = r cos theta, y = r sin theta
baseVertices[inx] = new float[2];
baseVertices[inx][0] = (float)(vertexRadius * Math.cos(vertexRadiansAry[inx]));
baseVertices[inx][1] = (float)(vertexRadius * Math.sin(vertexRadiansAry[inx]));
}
// Calculate the length of the normal (perpendicular) to a side.
// Find the height (altitude) of an equilateral triangle of 60 degrees.
// sin theta = opposite / hypotenuse
// hypotenuse = sin theta * opposite
// height = sin 60 * length of side
// normalRadius = vertexRadius * sin 60
// 60 degrees = 1.047197551196598 radians
double normalRadius = (float)(vertexRadius * Math.sin(HGBStatics.sixtyDegreesInRadians));
defineBasePetalOrigins(vertexRadius, normalRadius);
// base vertices are stored in class HGBShared.
// (get them via HGBShared.getBaseVertices())
hgbShared.setBaseVertices(baseVertices, vertexRadius, normalRadius);
// To define the base inscribed and superscribed base rectangles.
hgbRectBase.defineRectangles(normalRadius, baseVertices);
}
// The center is (0,0) and finds all origins of adjacent hexagons
// The return is via HGBShared.setBaseRoseOrigins()
// These are used to find the origins of all petals around all roses.
private void defineBasePetalOrigins(double vertexRadius, double normalRadius)
{
basePetalOrigins[0] = new float[2];
// Account for the distance between the center of the rose and its petals.
// A distance of two radii.
normalRadius *= 2;
double[] normalRadiansAry = hgbShared.getNormalRadiansAry();
for (int inx = 0; inx < HGBShared.SIDES; inx++)
{
basePetalOrigins[inx]= new float[2];
basePetalOrigins[inx][0] = (float)(normalRadius * Math.cos(normalRadiansAry[inx]));
basePetalOrigins[inx][1] = (float)(normalRadius * Math.sin(normalRadiansAry[inx]));
}
// base origins are stored in class HGBShared.
// (get them via HGBShared.getBaseOrigins())
hgbShared.setBasePetalOrigins(basePetalOrigins);
}
}