私の最後の質問は 2 分後に閉じられました。私は単にメンバー関数の助けを求めています。以下をチェックするメンバー関数が必要です。
- 正方形が別の正方形の外部にあるかどうか。
- 正方形に別の正方形が含まれているかどうか。
- 正方形が別の正方形に含まれているかどうか。
- 正方形が別の正方形に外部的に接しているかどうか (つまり、それらの境界が接触しているかどうか、しかしそれらの境界点を除いて、それらは互いに外部にあるかどうか);
- 正方形が別の正方形に内部的に接しているかどうか (つまり、境界上に共通の点がありますが、それらの境界点を除いて、1 つの正方形が他の正方形に含まれています)。
- 正方形の境界線が別の正方形の境界線と交差するかどうか。
私のプライベートメンバーは次のとおりです。
次に、x と y の両方を使用して周長と面積を計算するパブリック メンバー関数を使用する必要がありますか?
これは私がこれまでに持っているものです:ヘッダーファイル
#include <iostream>
#include <cmath>
class Square
{
int x, y;
int size;
public:
Square(int x, int y, int size) : x(x), y(y), size(size) { }
~Square() {};
bool isExternal(const Square& rhs);
bool contains(const Square& otherSquare);
bool iscontained(const Square& otherSquare);
bool borderintersect (const Square& otherSquare);
bool bordertangent (const Square& otherSquare);
}
実装
#include "Square.h"
bool Square::isExternal(const Square& rhs) const {
return (((x < rhs.x) || ((x + size) > (rhs.x + rhs.size)) && ((y < rhs.y) || ((y + size) > (rhs.y + rhs.size))
};
bool Square::contains(const Square& otherSquare)const {
};
bool Square::iscontained(const Square& otherSquare)const {
};
bool borderintersect(const Square& othersquare)
{
// If this square's bottom is greater than the other square's top
if ((this->y - (this->size / 2)) > (othersquare->y + (othersquare->size / 2)))
{
return (false);
}
// the reverse
if ((this->y + (this->size / 2)) < (othersquare->y - (othersquare->size / 2)))
{
return (false);
}
// If this square's left is greater than the other square's right
if ((this->x - (this->size / 2)) > (othersquare->x + (othersquare->size / 2)))
{
return (false);
}
if ((this->x + (this->size / 2)) < (othersquare->x - (othersquare->size / 2)))
{
return (false);
}
return (true);
bool Square::bordertangent (const Square& otherSquare)const {
};
テストプログラム
#include "Square.h"
#include <iostream>
using namespace std;
int main() {
Square sq1(0, 0, 10);
Square sq2(1, 1, 10);
if(sq1.isExternal(sq2)) {
cout<<"Square 1 is external to square 2"<<endl;
}
if(sq1.contains(sq2){
cout<<"Square 1 contains square 2"<<endl;
return 0;
}
座標とサイズの x と y の両方を取得するために、これをヘッダー ファイルに含める必要がありますか?
double getX( ) const { return x; }
double getY( ) const { return y; }
double getSize( ) const { return size; }