Joystick code

Joystick code

enum Direction {
        CENTER = 0,
        UP,
        DOWN,
        LEFT,
        RIGHT
};

Direction JudgeUpDownLeftRight(CPoint pt, CPoint center, int nCenterR)
{
    // excluding boundary calculation 
    int absToY = abs (pt.x- center.x);
     int absToX = abs (pt.y- center.y);
     if ((absToX <nCenterR) && 
        (absToY < nCenterR))
    {
        return CENTER;
    }
    if (pt.x >= center.x && pt.y < center.y)
    {
        if (absToX < absToY)
        {
            return RIGHT;
        }
        else
        {
            return UP;
        }
    }
    else if (pt.x < center.x && pt.y < center.y)
    {
        if (absToX < absToY)
        {
            return LEFT;
        }
        else
        {
            return UP;
        }
    }
    else if (pt.x < center.x && pt.y >= center.y)
    {
        if (absToX < absToY)
        {
            return LEFT;
        }
        else
        {
            return DOWN;
        }
    }
    else /*if (pt.x >= center.x && pt.y >= center.y)*/
    {
        if (absToX < absToY)
        {
            return RIGHT;
        }
        else
        {
            return DOWN;
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/2018shawn/p/12750132.html