Simple geometry (convex package) POJ 1113 Wall

Source: Internet
Author: User
Tags acos scalar

Topic Portal

Test instructions: To find the shortest route, so that any point on the line away from the castle at least L distance

Analysis: Convex hull First, answer = length of convex hull + circumference of circle with L as radius

/************************************************* author:running_time* Created time:2015/10/25 11:00:48* File N Ame:P oj_1113.cpp ************************************************/#include <cstdio> #include <algorithm > #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include < string> #include <vector> #include <queue> #include <deque> #include <stack> #include < list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime >using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e3 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const double EPS = 1e-10;     int dcmp (double x) {///three state function, reduced accuracy problem if (fabs (x) < EPS) return 0; else return x < 0? -1:1;     } struct Point {//points defined by double x, y; Point (Double x=0, double y=0):X (x), Y (y) {} point operator + (const-point &r) const {//Vector addition return point (x + r.x, y + r.y);     } Point operator-(const point &r) const {//Vector subtraction return point (x-r.x, Y-R.Y);     } Point operator * (double p) {///vector multiplied by a scalar return point (x * p, Y * p);     } point operator/(double p) {///vector divided by scalar return point (x/p, y/p); } BOOL operator < (const point &r) const {//DOT coordinate sort return x < R.x | |     (x = = r.x && y < r.y);  } bool operator = = (Const point &r) const {//judgment same dot return dcmp (x-r.x) = = 0 && dcmp (Y-     R.Y) = = 0; } };       typedef point Vector;     Vector definition point read_point (void) {//points read in double x, y;     scanf ("%lf%lf", &x, &y); Return point (x, y);      } double Polar_angle (vector a) {//Vector polar return atan2 (A.Y, a.x);} double dot (vector A, vector B) {//Vectors dot Product Return a.x * b.x +A.Y * B.Y; } Double Cross (vector A, vector B) {//vector fork product return a.x * B.Y-A.Y * b.x;} double length (vector a) {/ /vector length, dot product return sqrt (dot (A, a)); } double angle (vector a, vector b) {//Vector corner, counterclockwise, dot product return ACOs (dot (A, b)/Length (a)/length (b));} Doub Le Area_triangle (Point A, point B, point C) {//triangular area, fork product return Fabs (Cross (b-a, c-a))/2.0;} Vector Rotate (vector A, double rad) {//vector rotation, counterclockwise return vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (ra D) + a.y * cos (RAD));     } vector nomal (vector a) {//vector of unit method vector Double len = length (a); Return Vector (-a.y/len, A.x/len);     } point Point_inter (point P, Vector V, point q, Vector W) {//two line intersection, parametric equation Vector U = p-q;     Double T = Cross (w, U)/Cross (V, W); return p + V * t;     Double Dis_to_line (point P, Dot A, point B) {//points to straight line distance, two points Vector V1 = b-a, V2 = p-a; Return Fabs (Cross (V1, V2))/Length (V1); } DOUBLThe distance from {//to the line segment of the E dis_to_seg (point P, Dot a, points b), two-point 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);     else if (dcmp (dot (V1, V3)) > 0) return length (V3); else return Fabs (cross (V1, V2))/Length (V1);     } point Point_proj (points P, points A, point B) {//dots on the line projection, two-dot Vector V = b-a; Return a + v * (dot (v, p-a)/dot (V, v)); } bool Inter (point A1, O-A2, points B1, bit b2) {//Judgment segment intersection, two-dot double c1 = Cross (A2-A1, b1-a1), C2 =     Cross (A2-A1, b2-a1), C3 = Cross (B2-B1, a1-b1), C4 = Cross (b2-b1, A2-B1); Return DCMP (C1) * DCMP (C2) < 0 && dcmp (C3) * DCMP (C4) < 0; } bool On_seg (point P, Dot A1, point A2) {//Judgment points on the segment, two points return dcmp (cross (a1-p, a2-p)) = = 0 &&am P DCMP (dot (a1-p, a2-p)) < 0; } double Area_poly (point *p, int n) {//Polygon area double ret = 0;     for (int i=1; i<n-1; ++i) {ret + = Fabs (Cross (P[i]-p[0], p[i+1]-p[0])); } return RET/2;    } vector<point> Convex_hull (vector<point> &p) {sort (P.begin (), P.end ());    int n = p.size (), k = 0;    vector<point> ret (n * 2);         for (int i=0; i<n; ++i) {when (K > 1 && Cross (ret[k-1]-ret[k-2], P[i]-ret[k-1]) <= 0) k--;    ret[k++] = P[i]; } for (int i=n-2, t=k; i>=0;-I.) {while (K > t && Cross (Ret[k-1]-ret[k-2], P[i]-ret[k-1])        <= 0) k--;    ret[k++] = P[i];    } ret.resize (k-1);  return ret;}     struct Circle {point C;     Double R; Circle () {} circle (point C, Double R): C (c), R (r) {} "point" (double a) {return point (c.x + Co     S (a) * R, C.y + sin (a) * R); } };     struct line {point P;     Vector v;     Double R;   Line () {} line (const point &p, const Vector &v): P (P), V (v) {      R = Polar_angle (v);     } Point point (Double A) {return P + v * A;     } }; /* Line intersection to find the intersection, return the number of intersections, the intersection is saved in P */int line_cir_inter (lines L, Circle C, double &t1, double &t2, vector<point> & Amp     P) {Double A = l.v.x, B = l.p.x-c.c.x, C = l.v.y, d = l.p.y-c.c.y;     Double E = A * a + c * C, F = 2 * (A * b + c * d), G = b * B + d * D-C.R * C.R;     Double delta = f * f-4 * e * g;     if (dcmp (delta) < 0) return 0; if (dcmp (delta) = = 0) {T1 = t2 =-F/(2 * e);         P.push_back (L.point (t1));     return-1; } T1 = (-f-sqrt (delta))/(2 * e);     P.push_back (L.point (t1)); T2 = (-f + sqrt (delta))/(2 * e);     P.push_back (L.point (T2));     if (dcmp (T1) < 0 | | dcmp (t2) < 0) return 0; return 2; }/* Two circle intersection to find the intersection, return the number of intersections.     The intersection is saved in P */int Cir_cir_inter (Circle C1, Circle C2, vector<point> &p) {Double d = length (c1.c-c2.c); if (dcmp (d) = = 0) {if (dcmp (C1.R-C2.R) = =0) Return-1;     Two circles overlap else return 0;     } if (dcmp (C1.R + c2.r-d) < 0) return 0;     if (dcmp (Fabs (C1.R-C2.R)-D) < 0) return 0;     Double A = Polar_angle (c2.c-c1.c);        Double da = ACOs ((C1.R * C1.R + d * D-C2.R * C2.R)/(2 * C1.R * d));     C1C2 to C1p1 's horn?     Point P1 = C1.point (a-da), p2 = c2.point (A + da);     P.push_back (p1);     if (P1 = = p2) return 1;     else P.push_back (p2); return 2;    }const Double PI = ACOs ( -1.0);vector<point> p;int main (void) {int n;    Double L;        while (scanf ("%d%lf", &n, &l) = = 2) {p.clear ();        for (int i=0; i<n; ++i) {p.push_back (Read_point ());        } vector<point> q = Convex_hull (p);        Double ans = 0;        Q.push_back (Q[0]);        for (int i=0; i<q.size ()-1; ++i) {ans + = length (q[i+1]-q[i]);        } ans + = 2 * PI * L;    printf ("%.0f\n", ans); } return 0;}

Simple geometry (convex package) POJ 1113 Wall

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.