Пример #1
0
    public HexPoint HexCoordinates(Vector2 mouse)
    {
        HexPoint p = new HexPoint();
        p.x = (int)(0.025f * (mouse.X - ZeroGraphicCoordinates.X + 26.0f) + World.Instance.camera.x);
        p.y = (int)(0.0208333f * (mouse.Y - ZeroGraphicCoordinates.Y + 24.0f) + World.Instance.camera.y);

        float[] distances = new float[7];
        Vector2 shift = new Vector2(26, 24);
        distances[0] = (mouse - shift - GraphicCoordinates(p)).Length();
        distances[1] = (mouse - shift - GraphicCoordinates(p.Shift(HexPoint.HexDirection.N))).Length();
        distances[2] = (mouse - shift - GraphicCoordinates(p.Shift(HexPoint.HexDirection.NW))).Length();
        distances[3] = (mouse - shift - GraphicCoordinates(p.Shift(HexPoint.HexDirection.SW))).Length();
        distances[4] = (mouse - shift - GraphicCoordinates(p.Shift(HexPoint.HexDirection.S))).Length();
        distances[5] = (mouse - shift - GraphicCoordinates(p.Shift(HexPoint.HexDirection.SE))).Length();
        distances[6] = (mouse - shift - GraphicCoordinates(p.Shift(HexPoint.HexDirection.NE))).Length();

        int k = 0;
        float value = 100.0f;

        for (int i = 0; i < 7; i++)
        {
            if (distances[i] < value)
            {
                k = i;
                value = distances[i];
            }
        }

        if (k == 0) return p;
        else if (k == 1) return p.Shift(HexPoint.HexDirection.N);
        else if (k == 2) return p.Shift(HexPoint.HexDirection.NW);
        else if (k == 3) return p.Shift(HexPoint.HexDirection.SW);
        else if (k == 4) return p.Shift(HexPoint.HexDirection.S);
        else if (k == 5) return p.Shift(HexPoint.HexDirection.SE);
        else return p.Shift(HexPoint.HexDirection.NE);
    }