cf198 B. Maximal Area Quadrilateral
300个点,取4个点组成一个四边形,求四边形最大面积?
解法:枚举四边形的对角线,再求所有其它点的叉积,取最大最小值,再绝对值相加即可
(几何向量解法中常见的和面积相关的就是三角形的有向面积,即叉积!!!)
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <cstdlib> #include <fstream> #include <vector> #include <set> using namespace std; const double eps = 1e-12; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} void In() { scanf("%lf%lf", &x, &y); } }; typedef Point Vector; Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator-(Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }/// Vector operator*(Vector A, double p) { return Vector(A.x * p, A.y * p); } Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); } bool operator<(const Point &a, const Point &b) { return a.x < b.x || (a.x == b.x && a.y < b. y); } int dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } bool operator==(const Point &a, const Point &b) { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } ///(x, y) 极角atan2(y, x);单位:弧度 double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }///? double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); }///有向 Vector Rotate(Vector A, double rad) ///zhengming { return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); } Vector Normal(Vector A)///法向量 { double L = Length(A); return Vector(-A.y / L, A.x / L); } ///直线表示P=P0 + tV; P = A + (B-A)t; t 为参数 ///直线相交,求之前保证P + tv 和 Q + tv 有唯一的交点。当且仅当Cross(V, W)非0 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) ///? { Vector u = P - Q; double t = Cross(w, u) / Cross(v, w); return P + v * t; } ///点到直线距离 double DistanceToLine(Point P, Point A, Point B)///!!!!!!!!!Cross { Vector v1 = B - A, v2 = P - A; return fabs(Cross(v1, v2)) / Length(v1);///不取绝对值为有向距离 } ///点到线段距离 double DistanceToSegment(Point P, Point A, Point B)///Dot/Cross { if (A == B) return Length(P - A); Vector v1 = B - A, v2 = P - A, v3 = P - B; if (dcmp(Dot(v1, v2)) < 0) return Length(v2);///判断点在线段的位置和 Dot、Cross的四个象限的图联系 else if (dcmp(Dot(v1, v3)) > 0) return Length(v3); else return fabs(Cross(v1, v2)) / Length(v1);///不取绝对值为有向距离 } ///点到直线的投影,Dot的分配率 Point GetLineProjection(Point P, Point A, Point B) { Vector v = B - A; return A + v * (Dot(v, P - A) / Dot(v, v)); } ///线段相交3种情况 ///1.线段相交判定(规范相交) bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) { double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1); double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } ///2.c1 c2 都为0,想段共线(不平行),可能部分重合 ///3.c1 c2 不都为0, ///点在线上的判断 bool Onsegment(Point p, Point a1, Point a2) { return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; } double PolygonArea(Point* p, int n) { double area = 0; for (int i = 1; i < n - 1; i++) area += Cross(p[i] - p[0], p[i + 1] - p[0]); return area/2; } Point t[320]; int main() { int n; cin >> n; double ans = 0.0; for (int i = 0; i < n; i++) { t[i].In(); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j == i) continue;///保证枚举所有的对角线 Vector p1 = t[i] - t[j]; double area_max = 0.0; double area_min = 0.0; for (int r = 0; r < n; r++) { if (i == r || j == r) continue;///保证枚举所有的对角线 Vector p2 = t[i] - t[r]; double now = Cross(p1, p2); area_max = max(now, area_max); area_min = min(now, area_min); } if (area_max == 0 || area_min == 0) continue;///保证两点在对角线的两边 ans = max(ans, fabs(area_max) + fabs(area_min)); } } printf("%.10lf\n", ans / 2.0); return 0; }
简单几何 cf198 B. Maximal Area Quadrilateral,布布扣,bubuko.com
简单几何 cf198 B. Maximal Area Quadrilateral
原文:http://blog.csdn.net/guognib/article/details/20792557