Rotate the Shell Part Template

Source: Internet
Author: User
Tags mul

Convex hull diameter

Detailed description of convex hull diameter

// Calculate the convex hull diameter. Input convex hull Ch. The number of vertices is N, which is arranged counterclockwise. The square of the output diameter

Int rotating_calipers (int n) {int q = 1; int ans = 0; ch [N] = CH [0]; for (INT I = 0; I <N; I ++) {While (MUL (CH [I + 1], CH [q + 1], CH [I])> MUL (CH [I + 1], ch [Q], CH [I]) // enumerate an edge of a convex hull and scan other points. Calculate the triangle area to find the farthest point Q = (q + 1) % N; ans = max (ANS, max (DIS (CH [I]-ch [Q]), DIS (CH [I + 1]-ch [q + 1]);} return ans ;}
 

Minimum distance between convex compartment

Struct point {Double X, Y; point (double x = 0, Double Y = 0): x (x), y (y) {} // constructor for easy coding} p [N], Q [N]; typedef point pointt; pointt operator + (point a, point B) {return point (. X + B. x,. Y + B. y);} pointt operator-(point a, point B) {return point (. x-b.x,. 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 (. x-b.x) = 0 & DCMP (. y-b.y) = 0;} double dot (point a, point B) {return. x * B. X +. y * B. y;} double DIS (point a) {return SQRT (dot (A, A);} double cross (point a, point B) {return. x * B. y-a.y * B. x;} void Anticlock (point P [], int N) // sort the {for (INT I = 0; I <N-2; I ++) {Double K = cross (P [I + 1]-P [0], p [I + 2]-P [0]); If (DCMP (k)> 0) return; else if (DCMP (k) <0) {reverse (p, p + n); Return ;}} double distoline (point a, point B, point C) // The shortest distance from the point to the line AB {If (DCMP (DIS (a-B) = 0) return DIS (a-c ); if (DCMP (dot (a-B, a-c) <0) return DIS (a-c); If (DCMP (dot (B-, b-c) <0) return DIS (B-c); Return FABS (Cross (a-B, a-c)/DIS (a-B );} double dist (point a, point B, point C, point D) // The shortest distance between line AB and Cd {double ans = distoline (A, B, C ); ans = min (ANS, distoline (A, B, D); ans = min (ANS, distoline (c, d, A); ans = min (ANS, distoline (c, d, B); Return ans;} double MUL (point a, point B, point C) {return cross (B-a, c-);} double solve (point P [], int N, point Q [], int m) {int I; int miny = 0, Maxy = 0; for (I = 0; I <n; I ++) {If (P [I]. Y <p [miny]. y) miny = I ;}for (I = 0; I <m; I ++) if (Q [I]. y> q [Maxy]. y) Maxy = I; double ans = DIS (P [miny]-Q [Maxy]); for (I = 0; I <n; I ++) {double TMP; while (TMP = MUL (P [miny], p [miny + 1], Q [Maxy + 1])-Mul (P [miny], p [miny + 1], Q [Maxy])> EPS) Maxy = (Maxy + 1) % m; If (DCMP (TMP)> 0) ans = min (ANS, distoline (P [miny], P [miny + 1], Q [Maxy]); else ans = min (ANS, dist (P [miny], p [miny + 1], Q [Maxy], Q [Maxy + 1]); // edge-edge miny = (miny + 1) % N;} return ans ;}

 

Related Article

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.