タイトルの説明
整数のデータメンバーx、y座標値を使用して、平面座標系でポイントクラスポイントを定義します。メンバー関数には、(1)デフォルト値のコンストラクター、デフォルト値は0、(2)コンストラクターのコピー、(3)x、y座標値の設定、(4)x、y座標値の取得、パラメーターは2つの整数が含まれます。参照は、xおよびy座標値を取得するために使用されます。(5)出力関数。x、y座標値を出力するために使用されます。(6)2点間の距離を求める関数。パラメータはPointクラスのオブジェクト参照です。
平面座標系で三角形タイプTriangleを定義し、データメンバーは3つのPointオブジェクトp1、p2、p3です。メンバー関数は次のとおりです。(1)パラメーターを持つコンストラクター。パラメーターは整数x1、y1、x2、y2、x3、y3であり、三角形の3つの頂点の座標です。(2)パラメーターを持つコンストラクター。パラメーターは、3つのPointクラスオブジェクトへの参照です。(3)三角形の周囲を見つけます。(4)三角形の領域を見つけます。(5)三角形の3つの頂点の座標、周囲長、面積を出力します。
通常の関数を定義します。3つの頂点の座標が三角形を形成できるかどうかを判断します。
main()で、キーボードから3つのポイントの座標を入力して、3つのポイントが三角形を形成できるかどうかを判断します。そうでない場合は、ポイントの座標を再入力して再入力するように求めます。はいの場合は、の座標を出力します。 3つの頂点、周囲と面積。
入力の説明
3点の座標。三角形を形成できない場合は、3点の座標を再入力します。
出力の説明
3つの頂点の座標
、三角形の円周、および三角形の面積。
入力例
00
1 1
2 2
0 0
5 6
3 0
出力例
頂点座標が正しくなく、三角形を形成できません!座標を再入力してください!
三角形の3つの頂点の座標は次のとおりです。
(0,0)(5,6)(3,0)
三角形の周囲は17.1348で、面積は9です。
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
Point(int a=0,int b=0)
{
x=a;
y=b;
}
Point(const Point &a)//拷贝构造函数
{
x=a.x;
y=a.y;
}
void input()
{
cin>>x>>y;
}
int getx(int &x)
{
return x;
}
int gety(int &y)
{
return y;
}
void output()
{
cout<<"("<<x<<","<<y<<")";
}
double distance(Point &a)
{
double dx=x-a.x,dy=y-a.y;
return sqrt(dx*dx+dy*dy);
}
private:
int x,y;
};
class Triangle
{
public:
Triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
A=(x1,y1);
B=(x2,y2);
C=(x3,y3);
}
Triangle(Point &a,Point &b,Point &c)
{
A=a;
B=b;
C=c;
}
double Len()
{
return A.distance(B)+B.distance(C)+C.distance(A);
}
double Area()
{
double s=Len()/2.0;
return sqrt(s*(s-A.distance(B))*(s-B.distance(C))*(s-C.distance(A)));
}
void output()
{
cout<<endl<<"三角形周长为:"<<Len()<<",面积为:"<<Area();
}
private:
Point A,B,C;
};
bool ifTriangle(Point &p1,Point &p2,Point &p3)
{
here:
if((p1.distance(p2)+p2.distance(p3)>p3.distance(p1))&& (p1.distance(p2) + p3.distance(p1) > p2.distance(p3)) && (p2.distance(p3) + p3.distance(p1) > p1.distance(p2)))
{
cout << "三角形三个顶点坐标为:" << endl;
p1.output();
p2.output();
p3.output();
Triangle T(p1,p2,p3);
T.output();
}
else
{
cout << "顶点坐标不正确,不能构成三角形!请重新输入坐标!" << endl;
return false;
}
}
int main()
{
Point A,B,C;
here:
A.input();
B.input();
C.input();
if (ifTriangle(A, B, C) == false)
{
goto here;
}
return 0;
}
三角形かどうかを判断する
2つの辺の合計が3番目の辺よりも大きい、
または(2つの辺の差
が3番目の辺よりも小さい)が
、
a + b> c && a + c> b && b + c> aのように3つをリストする必要があります
if((p1.distance(p2)+p2.distance(p3)>p3.distance(p1))&& (p1.distance(p2) + p3.distance(p1) > p2.distance(p3)) && (p2.distance(p3) + p3.distance(p1) > p1.distance(p2)))
```