在正方形和十字的周长内和周长上找到 (x,y) 的算法

Algorithm for finding a (x,y) within and on perimeter of a square and cross

我有一个学校作业,要求我找出某个点 (x,y) 是否在一个形状内,例如一个正方形。

Class Square
private:
    int coordX[4];
    int coordY[4];

然而十字有12分,

Class Cross
private:
    int coordX[12];
    int coordY[12];

这些是正方形的属性。那么假设正方形坐标是(1,1)(1,4)(4,1)(4,4),我应该如何找出点(2,2)是否在正方形内?

bool isPointInShape (int, int)

该函数旨在接受两个整数,这是您要检查的点,它将 return 真或假。

bool isPointOnShape (int, int);

检查点是否在形状的周长上的函数相同。

谁能帮忙弄清楚计算这两个函数的算法是什么?

class Square {
private:
    int coordX[4];
    int coordY[4];
    int xMin, xMax, yMin, yMax;

public:
    Square();
    bool is_point_on_shape (int, int);
};

Square::Square(int coordXIn[], int coordYIn[])
{
    coordX = coordXIn;
    coordY = coordYIn;

    // determine extreme x and y values, which define the box
    xMin = coordX[0];
    xMax = coordX[0];
    yMin = coordY[0];
    yMax = coordY[0];

    for (int i=1; i < 4; ++i)
    {
        if (coordX[i] < xMin)
        {
            xMin = coordX[i];
        }

        if (coordX[i] > xMax)
        {
            xMax = coordX[i];
        }
        if (coordY[i] < YMin)
        {
            yMin = coordY[i];
        }

        if (coordY[i] > yMax)
        {
            yMax = coordY[i];
        }
    }
}

// since we have the range of x and y values, checking to see if a point be
// inside the square just means checking that this point lies within the range
bool Square::is_point_on_shape (int x, int y)
{
    if (xMin + x > xMax) return false;
    if (yMin + y > yMax) return false;

    return true;
}