AOJ - Problem 0012 : A Point in a Triangle
三角形の内部に点があるかどうか判定する問題です。
三角形の頂点3つを角A,B,C、判定する点をPとすると角APB,BPC,CPAの角度の和が
360°なら内部にあると判定する方法でOKです。
もしかすると他の解法もあるかもしれません。
Problem 0010 : Circumscribed Circle of a Triangleを少し改良したソースになりました。
#include <iostream> #include <complex> #include <cmath> using namespace std; // 点座標を型とする typedef complex<double> P; // πの定義 #define PI 3.1415926535898 // 許容する誤差ε #define EPS (1e-10) //XY座標 #define X real() #define Y imag() //2つのdouble型の数値が等しいかどうか bool equal(double a, double b){ return ( abs( a-b ) < EPS )? true : false ; } // 三角形クラス class Triangle{ private: //三角形の3点の座標 P a, b, c; //三角形の3辺の長さ double edgeA,edgeB,edgeC; //三角形の3角の大きさ(ラジアン) double angleA,angleB,angleC; //余弦定理から3つの角度を求める関数 double LawOfCosines(double a,double b, double c){ return acos( (b*b+c*c-a*a) / (2.0*b*c) ); } public: //コンストラクタ(3つの点と辺と角度を初期化) Triangle(P p1, P p2, P p3){ a = p1; b = p2; c = p3; edgeB = abs(c-a); edgeA = abs(b-c); edgeC = abs(a-b); angleA = LawOfCosines(edgeA,edgeB,edgeC); angleB = LawOfCosines(edgeB,edgeC,edgeA); angleC = LawOfCosines(edgeC,edgeA,edgeB); } double getAngleC(){//角Cの角度を変えす return angleC; } }; int main(){ double x1, x2, x3, y1, y2, y3, xp, yp, angle; while( cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> xp >> yp ){ P a( x1 , y1 ); P b( x2 , y2 ); P c( x3 , y3 ); P p( xp , yp ); Triangle Tr1(a,b,p); Triangle Tr2(b,c,p); Triangle Tr3(c,a,p); angle = Tr1.getAngleC() + Tr2.getAngleC() + Tr3.getAngleC(); ( equal( 2.0*PI , angle) )? cout << "YES" : cout << "NO" ; cout << endl; } }