]> pd.if.org Git - hexagon/blobdiff - hexagon.c
add polar coordinate calculation
[hexagon] / hexagon.c
index 61bb47b0fd80429ddd49e57df1e72ebb450cfc12..2dd1f3a3e5e6094b5633e8ebc93e08d0a6ba8d14 100644 (file)
--- a/hexagon.c
+++ b/hexagon.c
@@ -12,7 +12,7 @@ static int inversecantor(int cantor, int *x, int *y);
  * domain
  */
 
-double HL_vertexv[] = {
+double HL_vertexv[12] = {
        .577350269189625764509148780502, 0.0,
        .288675134594812882254574390251, 0.5,
        -.288675134594812882254574390251, 0.5,
@@ -20,6 +20,54 @@ double HL_vertexv[] = {
        -.288675134594812882254574390251, -0.5,
        .288675134594812882254574390251, -0.5};
 
+double HL_fand[16] = {
+       0.0, 0.0,
+       .577350269189625764509148780502, 0.0,
+       .288675134594812882254574390251, 0.5,
+       -.288675134594812882254574390251, 0.5,
+       -.577350269189625764509148780502, 0.0,
+       -.288675134594812882254574390251, -0.5,
+       .288675134594812882254574390251, -0.5,
+       .577350269189625764509148780502, 0.0
+};
+
+double HL_hfand[16] = {
+       0.0, 0.0,
+       0.0, .577350269189625764509148780502,
+       0.5, .288675134594812882254574390251,
+       0.5, -.288675134594812882254574390251,
+       0.0, -.577350269189625764509148780502,
+       -0.5, -.288675134594812882254574390251,
+       -0.5, .288675134594812882254574390251,
+       0.0, .577350269189625764509148780502
+};
+
+float HL_fanf[16] = {
+       0.0f, 0.0f,
+       .577350269189625764509148780502f, 0.0f,
+       .288675134594812882254574390251f, 0.5f,
+       -.288675134594812882254574390251f, 0.5f,
+       -.577350269189625764509148780502f, 0.0f,
+       -.288675134594812882254574390251f, -0.5f,
+       .288675134594812882254574390251f, -0.5f,
+       .577350269189625764509148780502f, 0.0f
+};
+
+float HL_hfanf[16] = {
+       0.0f, 0.0f,
+       0.0f, .577350269189625764509148780502f,
+       0.5f, .288675134594812882254574390251f,
+       0.5f, -.288675134594812882254574390251f,
+       0.0f, -.577350269189625764509148780502f,
+       -0.5f, -.288675134594812882254574390251f,
+       -0.5f, .288675134594812882254574390251f,
+       0.0f, .577350269189625764509148780502f
+};
+
+/* size of a square that will exactly fit in a hexagon */
+/* 2.0/(1+sqrt(3.0)) */
+double HL_square = .73205080756887729352;
+
 /* these all are for a hex one unit across */
 static double          hexptvd[6][2] = {
        {.577350269189625764509148780502, 0.0}, /* 1.0/sqrt3 */
@@ -53,6 +101,85 @@ static double          hexpthd[6][2] = {
 
 #endif
 
+/*
+c = 1.0 = distance from center to vertex
+b = distance to hex side
+a = distance along hex side
+
+A = angle at point center, this is the polar coord angle
+B = angle from vertex, this is 60 degrees
+C = Other angle, which is 180 - 60 - A = 120 - A
+
+sin B = sin 60 = sqrt(3) / 2
+cos B = cos 60 = 1/2
+
+b = c * sin B / ( sin A cos B + sin B cos A)
+
+c * sin B / ( sin A cos B + sin B cos A)
+
+0.5 * sqrt3
+-------------
+sinA 0.5 + sqrt3 * 0.5 cosA
+
+sqrt3
+-------------
+sinA + sqrt3 cosA
+
+b=
+3
+-------------
+cosA + sqrt3 sinA
+
+x = b cosA
+y = b sinA
+*/
+
+#define RD (180.0 / M_PI)
+/* angle ranges from 0-6 */
+/* distance is 0 at origin, 1.0 at the hex side */
+
+/* return the polar distance ? */
+double HL_polar(double angle, double distance, double *x, double *y) {
+       double A, B, C, b, c;
+       double sinB, sinC;
+#if 0
+       double sqrt3;
+
+       sqrt3 = sqrt(3.0);
+#endif
+
+       /* convert angle to radians */
+       angle = angle * M_PI / 3.0;
+
+       /* calculate the distance along the ray to the hex side */
+
+       A = fmod(angle, M_PI/3.0); /* constrain to within an equilateral */
+       B = M_PI / 3.0;
+       C = M_PI - A - B;
+
+       sinB = sin(B);
+       sinC = sin(C);
+
+       c = 1.0;
+       c = HL_vertexv[0];
+#if 0
+       /* provided for completeness, but we don't use the value */
+       sinA = sin(A);
+       a = c * sinA / sinC;
+#endif
+       b = c * sinB / sinC;
+
+       if (x) {
+               *x = b * cos(angle);
+       }
+       
+       if (y) {
+               *y = b * sin(angle);
+       }
+
+       return b;
+}
+
 void HL_vertices(int cantor, double *vc) {
        int i;
        double xc, yc;
@@ -437,14 +564,16 @@ static int hex_iso(struct hex *h) {
 
 #endif
 
+#define COS30 (.866025403784438646763723170752)
+
 int HL_hexbin(double width, double x, double y, int *i, int *j) {
        double z, rx, ry, rz;
        double abs_dx, abs_dy, abs_dz;
        int ix, iy, iz, s;
        struct hex h;
 
-       /* TODO just hard-code this cosine */
-       x = x / cos(30 * M_PI / 180.0); /* rotated X coord */
+       /*x = x / cos(30 * M_PI / 180.0); */ /* rotated X coord */
+       x = x / COS30;
        y = y - x / 2.0; /* adjustment for rotated X */
 
        /* adjust for actual hexwidth */