2016-04-27 16 views
1

我有一個學校作業,它要求我找出某個點(x,y)是否在一個形狀內,例如讓說一個正方形。找到一個正方形和十字的周邊內(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) 

該函數是爲了取兩個整數,這是你想要檢查的點,它會返回true或false。

bool isPointOnShape (int, int); 

相同的功能,檢查點是否在形狀的周長。

有人可以幫助確定如何計算這2個函數的算法是什麼?

+0

爲什麼一個正方形有8個點與它關聯,什麼是十字的意思? –

+0

x的4個點和y的4個點,所以如果你真的考慮x和y tgt它的x,y的4個點。十字會就像一個醫療中心標誌? – user6235245

回答

0
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; 
} 
+0

嗨,謝謝你的回答,但對於一個交叉,你認爲應該怎麼做? – user6235245

+0

你甚至沒有告訴我們你的問題中有什麼交叉。 –

+0

十字架有12點非常像醫療標誌 – user6235245

相關問題