C++で三角形の外接円を見つけるプログラム
このチュートリアルでは、三角形の外接円を見つけるプログラムについて説明します。
このために、3つの非同一直線上の点が提供されます。私たちの仕事は、それらの点によって形成される三角形の外接円を見つけることです。
例
#include <iostream>
#include <cfloat>
using namespace std;
//storing X and Y values
#define pdd pair<double, double>
void lineFromPoints(pdd P, pdd Q, double &a, double &b, double &c){
a = Q.second - P.second;
b = P.first - Q.first;
c = a*(P.first)+ b*(P.second);
}
void perpendicularBisectorFromLine(pdd P, pdd Q, double &a, double &b, double &c){
pdd mid_point = make_pair((P.first + Q.first)/2, (P.second + Q.second)/2);
c = -b*(mid_point.first) + a*(mid_point.second);
double temp = a;
a = -b;
b = temp;
}
pdd lineLineIntersection(double a1, double b1, double c1, double a2, double b2, double c2){
double determinant = a1*b2 - a2*b1;
if (determinant == 0){
return make_pair(FLT_MAX, FLT_MAX);
} else {
double x = (b2*c1 - b1*c2)/determinant;
double y = (a1*c2 - a2*c1)/determinant;
return make_pair(x, y);
}
}
void findCircumCenter(pdd P, pdd Q, pdd R){
double a, b, c;
lineFromPoints(P, Q, a, b, c);
double e, f, g;
lineFromPoints(Q, R, e, f, g);
perpendicularBisectorFromLine(P, Q, a, b, c);
perpendicularBisectorFromLine(Q, R, e, f, g);
pdd circumcenter = lineLineIntersection(a, b, c, e, f, g);
if (circumcenter.first == FLT_MAX && circumcenter.second == FLT_MAX){
cout << "The two perpendicular bisectors "
"found come parallel" << endl;
cout << "Thus, the given points do not form "
"a triangle and are collinear" << endl;
} else {
cout << "The circumcenter of the triangle PQR is: ";
cout << "(" << circumcenter.first << ", "
<< circumcenter.second << ")" << endl;
}
}
int main(){
pdd P = make_pair(6, 0);
pdd Q = make_pair(0, 0);
pdd R = make_pair(0, 8);
findCircumCenter(P, Q, R);
return 0;
} 出力
The circumcenter of the triangle PQR is: (3, 4)
-
C++で三角形の周囲を検索
この問題では、三角形の周囲長、さまざまなタイプの三角形の周囲長の式、およびそれらを見つけるためのプログラムを確認します。 境界 フィギュアの周りの合計距離として定義されます。基本的に、それは与えられた図のすべての辺の合計です。 三角形の周囲 三角形の周囲は、その3つの辺すべての合計です(三角形は3つの辺の図です)。 式、 Perimeter = sum of all sides Perimeter = x + y + z 三角形の周囲を見つけるプログラム 例 #include <iostream> using namespace std; int calcPe
-
C++で三角形の図心を見つけるプログラム
この問題では、三角形の3つの頂点の座標を示す2D配列が与えられます。私たちのタスクは、C++で三角形のセントロイドを見つけるプログラムを作成することです。 セントロイド 三角形の3つの中央値は、三角形の3つの中央値が交差する点です。 中央値 三角形の頂点は、三角形の頂点とその反対側の線の中心点を結ぶ線です。 問題を理解するために例を見てみましょう 入力 (-3, 1), (1.5, 0), (-3, -4) 出力 (-3.5, -1) 説明 Centroid (x, y) = ((-3+2.5-3)/3, (1 + 0 - 4)/3) = (-3.5, -1) ソリューションアプロ