Пример #1
0
        /*
         * Determine the containing hex in ijk+ coordinates for a 2D cartesian
         * coordinate vector (from DGGRID).
         *
         * @param v The 2D cartesian coordinate vector.
         * @param h The ijk+ coordinates of the containing hex.
         */
        public static CoordIJK FromHex2d(Vec2d v)
        {
            var h = new CoordIJK();

            double a1, a2;
            double x1, x2;
            int    m1, m2;
            double r1, r2;

            // quantize into the ij system and then normalize
            h.k = 0;

            a1 = Math.Abs(v.x);
            a2 = Math.Abs(v.y);

            // first do a reverse conversion
            x2 = a2 / M_SIN60;
            x1 = a1 + x2 / 2.0;

            // check if we have the center of a hex
            m1 = (int)x1;
            m2 = (int)x2;

            // otherwise round correctly
            r1 = x1 - m1;
            r2 = x2 - m2;

            if (r1 < 0.5)
            {
                if (r1 < 1.0 / 3.0)
                {
                    if (r2 < (1.0 + r1) / 2.0)
                    {
                        h.i = m1;
                        h.j = m2;
                    }
                    else
                    {
                        h.i = m1;
                        h.j = m2 + 1;
                    }
                }
                else
                {
                    if (r2 < (1.0 - r1))
                    {
                        h.j = m2;
                    }
                    else
                    {
                        h.j = m2 + 1;
                    }

                    if ((1.0 - r1) <= r2 && r2 < (2.0 * r1))
                    {
                        h.i = m1 + 1;
                    }
                    else
                    {
                        h.i = m1;
                    }
                }
            }
            else
            {
                if (r1 < 2.0 / 3.0)
                {
                    if (r2 < (1.0 - r1))
                    {
                        h.j = m2;
                    }
                    else
                    {
                        h.j = m2 + 1;
                    }

                    if ((2.0 * r1 - 1.0) < r2 && r2 < (1.0 - r1))
                    {
                        h.i = m1;
                    }
                    else
                    {
                        h.i = m1 + 1;
                    }
                }
                else
                {
                    if (r2 < (r1 / 2.0))
                    {
                        h.i = m1 + 1;
                        h.j = m2;
                    }
                    else
                    {
                        h.i = m1 + 1;
                        h.j = m2 + 1;
                    }
                }
            }

            // now fold across the axes if necessary

            if (v.x < 0.0)
            {
                if ((h.j % 2) == 0)  // even
                {
                    long axisi = h.j / 2;
                    long diff  = h.i - axisi;
                    h.i = (int)(h.i - 2.0 * diff);
                }
                else
                {
                    long axisi = (h.j + 1) / 2;
                    long diff  = h.i - axisi;
                    h.i = (int)(h.i - (2.0 * diff + 1));
                }
            }

            if (v.y < 0.0)
            {
                h.i = h.i - (2 * h.j + 1) / 2;
                h.j = -1 * h.j;
            }

            h.Normalize();

            return(h);
        }
Пример #2
0
 public CoordIJK(Vec2d v) => FromHex2d(v);