UVA_10002_Center of Masses
来源:互联网 发布:2d图像转3d算法 编辑:程序博客网 时间:2024/06/06 05:40
#include<iostream> #include<sstream> #include<string> #include<vector> #include<list> #include<set> #include<map> #include<stack> #include<queue> #include<algorithm> #include<numeric> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #pragma warning(disable:4996) using namespace std;const double eps = 1e-9;const double pi = acos(-1.0);int cmp(const double &x){if (fabs(x) < eps){return 0;}if (x > 0){return 1;}return -1;}inline double sqr(const double &x){return x*x;}class Point{public:double x, y;Point() {}Point(const double &a, const double &b) :x(a), y(b) {}void input(){cin >> x >> y;}friend Point operator+(const Point &a, const Point &b){return Point(a.x + b.x, a.y + b.y);}friend Point operator-(const Point &a, const Point &b){return Point(a.x - b.x, a.y - b.y);}friend bool operator==(const Point &a, const Point &b){return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;}friend Point operator*(const Point &a, const double &b){return Point(a.x*b, a.y*b);}friend Point operator*(const double &a, const Point &b){return Point(b.x*a, b.y*a);}friend Point operator/(const Point &a, const double &b){return Point(a.x / b, a.y / b);}double norm(){return sqrt(sqr(x) + sqr(y));}};double det(const Point &a, const Point &b){return a.x*b.y - a.y * b.x;}double dot(const Point &a, const Point &b){return a.x*b.x + a.y * b.y;}double dist(const Point &a, const Point &b){return (a - b).norm();}Point rotate_point(const Point&p, const double &A){double tx = p.x, ty = p.y;return Point(tx*cos(A) - ty*sin(A), tx*sin(A) + ty*cos(A));}class Line{public:Point a, b;Line() {}Line(const Point&x, const Point &y) :a(x), b(y) {}Line point_make_line(const Point &a, const Point &b){return Line(a, b);}};double dist_point_segment(const Point &p, const Point &s, const Point &t){if (cmp(dot(p - s, t - s))<0){return (p - s).norm();}if (cmp(dot(p - t, s - t)) < 0){return (p - t).norm();}return fabs(det(s - p, t - p) / dist(s, t));}void PointProjLine(const Point &p, const Point &s, const Point&t, Point &cp){double r = dot((t - s), (p - s)) / dot(t - s, t - s);cp = s + r*(t - s);}bool PointOnSegment(const Point&p, const Point &s, const Point &t){return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t)) <= 0;}bool parallel(const Line&a, const Line&b){return !cmp(det(a.a - a.b, b.a - b.b));}bool line_make_point(const Line &a, const Line&b, Point &res){if (parallel(a, b)) return false;double s1 = det(a.a - b.a, b.b - b.a);double s2 = det(a.b - b.a, b.b - b.a);res = (s1*a.b - s2*a.a) / (s1 - s2);return true;}Line move_d(const Line &a, const double &len){Point d = a.b - a.a;d = d / d.norm();d = rotate_point(d, pi / 2);return Line(a.a + d*len, a.b + d*len);}bool comp_less(const Point &a, const Point &b){return cmp(a.x - b.x) < 0 || cmp(a.x - b.x) == 0 && cmp(a.y - b.y) < 0;}class ConvexPolygon{public:vector<Point>point;ConvexPolygon(const int &n){vector<Point>a(n);for (int i = 0; i < n; i++){cin >> a[i].x >> a[i].y;}point.resize(2 * a.size() + 5);sort(a.begin(), a.end(), comp_less);a.erase(unique(a.begin(), a.end()), a.end());int m = 0;for (int i = 0; i < a.size(); i++){while (m > 1 && cmp(det(point[m - 1] - point[m - 2], a[i] - point[m - 2])) <= 0){m--;}point[m++] = a[i];}int k = m;for (int i = int(a.size()) - 2; i >= 0; i--){while (m > k&&cmp(det(point[m - 1] - point[m - 2], a[i] - point[m - 2])) <= 0){m--;}point[m++] = a[i];}point.resize(m);if (a.size() > 1){point.resize(m - 1);}}double perimeter(){double sum = 0.0;for (int i = 0; i < (int)point.size() - 1; i++){sum += (point[i + 1] - point[i]).norm();}sum += (point[0] - point[point.size() - 1]).norm();return sum;}double area(){double sum = 0.0;for (int i = 0; i < (int)point.size() - 1; i++){sum += det(point[i + 1], point[i]);}sum += det(point[0], point[point.size() - 1]);return sum / 2.0;}int PointInPolygon(const Point &t){int num = 0, d1, d2, k, n = point.size();point.push_back(point[0]);for (int i = 0; i < n; i++){if (PointOnSegment(t, point[i], point[i + 1])){return 2;}k = cmp(det(point[i + 1] - point[i], t - point[i]));d1 = cmp(point[i].y - t.y);d2 = cmp(point[i + 1].y - t.y);if (k > 0 && d1 <= 0 && d2>0){num++;}if (k < 0 && d2 <= 0 && d1>0){num--;}}point.pop_back();return num != 0;}Point getMassCenter(){Point ans = Point(0, 0);if (cmp(area()) == 0){return ans;}point.push_back(point[0]);for (int i = 0; i < point.size() - 1; i++){ans = ans + (point[i] + point[i + 1])*det(point[i + 1], point[i]);}return ans / area() / 6.0;}};int main(){//freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int n;while (cin >> n&&n >= 3){ConvexPolygon polygon(n);Point ans = polygon.getMassCenter();if (cmp(ans.x)){printf("%.3lf ", ans.x);}else{printf("0.000 ");}if (cmp(ans.y)){printf("%.3lf\n", ans.y);}else{printf("0.000\n");}}return 0;}
0 0
- UVA_10002_Center of Masses
- uva10002 Center of Masses
- 10002 - Center of Masses
- UVA 10002 Center of Masses
- UVa 10002 - Center of Masses
- UVA 10002 Center of Masses【求凸包重心】
- UVA 10002 Center of Masses(凸包)
- UVa 10002 Center of Masses (凸包重心)
- UVa 10002 Center of Masses(Andrew或Graham_scan)
- We're on the cusp of deep learning for the masses. You can thank Google later
- Knowledge for the masses
- 众数 (masses. cpp) )
- Kernel mode sockets library for the masses
- uva 1444 - Knowledge for the masses(高效)
- UVA 1444 - Knowledge for the masses
- UVALive - 4629 Knowledge for the masses 高效
- Kernel mode sockets library for the masses[zt]
- SGU 227. The art to the broad masses!
- Word2010 Error:The name in the end tag of the element must match the element type in the start tag.
- 喵帕斯喵喵
- ListNode示例
- 数据库进阶系列之一:漫谈数据库索引
- 关于购物车计算金钱的问题
- UVA_10002_Center of Masses
- Spark读取HDFS文件
- 小米手机真机调试的设置-机型:HM-1SC
- 数据库进阶系列之二:细说数据库范式
- 练习010
- objective-c中的self和super关键字
- 关于error LNK2019::Unresolved External Symbol * Referenced In Function..解决办法 2
- 一起talk C栗子吧(第一百五十一回:C语言实例--socket通信地址)
- memcpy的函数内部实现