求面积根据叉乘:不懂可以先看一下我的博客里计算机几何,叉乘2016-11-1016:11:01
1 #include <iostream> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 6 struct point { 7 double x, y; 8 }; 9 10 struct v { 11 point start, end; 12 }; 13 14 15 double dotProduct(v v1, v v2) { 16 return (v1.end.x - v1.start.x)*(v2.end.x - v2.start.x) + (v1.end.y - v1.start.y)*(v2.end.y - v2.start.y); 17 } 18 double crossProduct(v v1, v v2) { 19 return (v1.end.x - v1.start.x)*(v2.end.y - v2.start.y) - (v2.end.x - v2.start.y)*(v1.end.y - v1.start.y); 20 } 21 22 bool inTriangle(point a,point b,point c, point p) { 23 v ab, ac, bc; 24 ab.start = a, ab.end = b; 25 ac.start = a, ac.end = c; 26 bc.start = b, bc.end = c; 27 double Sabc = fabs(crossProduct(ab, ac)); 28 29 v pb; 30 pb.start = p, pb.end= b; 31 double Sabp = fabs(crossProduct(ab, pb)); 32 33 v pc; 34 pc.start = p, pc.end = c; 35 double Sacp = fabs(crossProduct(ac, pc)); 36 37 double Sbcp = fabs(crossProduct(bc, pc)); 38 39 if (fabs(Sabc - Sabp - Sacp - Sbcp) < 1e-5) 40 return true; 41 return false; 42 } 43 44 45 int main() { 46 point p1, p2, p3,p0; 47 while (cin >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y >> p0.x >> p0.y) { 48 cout << inTriangle(p1, p2, p3, p0); 49 } 50 }