Triangle Fun
题意:给三角形,求一些三等分点,线段交点,求面积。
简单题~
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int inf = 0x3f3f3f3f; 4 const double eps = 1e-12; 5 const double pi = acos(-1.0); 6 7 struct Point { 8 double x,y; 9 Point (double x = 0, double y = 0) : x(x), y(y) {} 10 }; 11 typedef Point Vector; 12 Vector operator + (Vector a, Vector b) { 13 return Vector (a.x + b.x, a.y + b.y); 14 } 15 Vector operator * (Vector a, double s) { 16 return Vector (a.x * s, a.y * s); 17 } 18 Vector operator / (Vector a, double p) { 19 return Vector (a.x / p, a.y / p); 20 } 21 Vector operator - (Point a, Point b) { 22 return Vector (a.x - b.x, a.y - b.y); 23 } 24 bool operator < (Point a, Point b) { 25 return a.x < b.x || (a.x == b.x && a.y < b.y); 26 } 27 int dcmp (double x) { 28 if(fabs(x) < eps) return 0; 29 return x < 0 ? -1 : 1; 30 } 31 bool operator == (const Point &a, const Point &b) { 32 return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; 33 } 34 double Angel (Vector a) { 35 return atan2(a.y, a.x); 36 } 37 double Dot(Vector a, Vector b) { 38 return a.x * b.x + a.y * b.y; 39 } 40 double Length (Vector a) { 41 return sqrt(Dot(a, a)); 42 } 43 double Angle (Vector a, Vector b) { 44 return acos(Dot(a, b) / Length(a) / Length(b)); 45 } 46 double Cross (Vector a, Vector b) { 47 return a.x * b.y - a.y * b.x; 48 } 49 //两直线交点 50 Point GetLineIntersection (Point p, Vector v, Point q, Vector w) { 51 Vector u = p - q; 52 double t1 = Cross(w, u) / Cross(v, w); 53 double t2 = Cross(v, u) / Cross(v, w); 54 return p + v * t1; // return q + w * t2; 55 } 56 //多边形面积 57 double ConvexPolygonArea(Point *p, int n) { 58 double area = 0; 59 for(int i = 1; i < n-1; i++) { 60 area += Cross(p[i] - p[0], p[i+1] - p[0]); 61 } 62 return area / 2; 63 } 64 65 Point p[3],ch[3]; 66 int main(){ 67 int t; 68 //freopen("in.txt", "r", stdin); 69 scanf("%d", &t); 70 while(t--){ 71 for(int i = 0; i < 3; i++){ 72 scanf("%lf %lf", &p[i].x, &p[i].y); 73 } 74 Point d = p[1] + (p[2] - p[1])/3; 75 Point e = p[2] + (p[0] - p[2])/3; 76 Point f = p[0] + (p[1] - p[0])/3; 77 ch[0] = GetLineIntersection(p[0], d-p[0], p[1], e-p[1]); 78 ch[1] = GetLineIntersection(p[2], f-p[2], p[1], e-p[1]); 79 ch[2] = GetLineIntersection(p[2], f-p[2], p[0], d-p[0]); 80 double area = ConvexPolygonArea(ch,3); 81 printf("%.0lf\n", area); //这里是四舍五入,如果强制转换是截断,会WA 82 } 83 }
时间: 2024-10-10 14:05:33