Learning algorithm: Computational Geometry stuck rotation

【definition】

[] Of heel point is present on the two parallel-tangent polygon

] [Polygon radius maximum length between any two points on the polygon

 


[Stuck] rotation

 Select the y-axis, the two highest and lowest points, so that two lines parallel to the x-axis cut through these two points

 Then we started to make two lines of rotation

 When a first line and a line segment parallel to the polygons, one side will stop further rotation

 This time, we need to judge by the cross product now meets the requirements made on the spot

 That is, with parallel tangents

Here we use another method to find the current point corresponding to the farthest point (the vector will not prove QAQ)

Determining an edge, and then find the point counterclockwise and that the edge area of ​​the triangle formed

When the maximum area, this point is the furthest point

(Here we find that from this segment furthest point)

 

 It should be noted at the initial comparison selected, the assignment of each value (because of this I wasted two hours QAQ)

l max_dis(P* p)
{
    int maxp = 1, minp = 1;
    ll  maxy =-1001, miny = 1001;
    for (int i = 1; i <=n; i++)
    {
        if (p[i].y > maxy) maxy = p[i].y, maxp = i;
        if (p[i].y < miny )miny = p[i].y, minp = i;
    }
    ll ans = max(dis(p[minp],p[maxp]),dis(p[(minp%n)+1],p[maxp]));
    for (int i = 1; i <=n; i++,minp=(minp+1>n)?1:(minp+1))
    {
        while (cross(p[(minp%n) + 1], p[(maxp % n)+1], p[minp]) > cross(p[(minp%n) + 1], p[maxp], p[minp]))
            maxp = (maxp+1) %n;
        ans = max(ans, dis(p[minp], p[maxp]));
        ans = max(ans, dis(p[(minp % n) +1], p[maxp]));
    }
    return ans;
}
View Code

 


Template title:

2178】 【CONCEPT

 A given polygon, the polygon radius seek

 

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
#include<cmath>
#define ll int 
using namespace std;
const ll eps = 0;
const int MAXN = 50010;
const ll INF = (1<<31)-1;
const ll lim = 100010;
int n;
struct V
{
    ll x, y;
    V(ll a = 0, ll b = 0) :x(a), y(b) {}
};
typedef V P;
V    operator+(V a, V b) { return V(a.x + b.x, b.y + a.y); }
V    operator-(V a, V b) { return V(a.x - b.x, b.y - a.y); }
V    operator*(V a, ll b) { return V(a.x*b, a.y*b); }
V    operator*(ll a, V b) { return V(a*b.x, b.y*a); }
V    operator/(V a, ll b) { return V(a.x / b, a.y / b); }
V    operator/(ll a, V b) { return V(b.x / a, b.y / a); }
ll   operator^(V a, V b) { return a.x*b.x + a.y*b.y; }
bool operator<(V a, V b) { return (a.x == b.x) ? a.y < b.y : a.x < b.x; }
int sgn(ll x)
{
    return (x > eps) - (x < eps);
}
ll  cross(V a, V b)
{
    return a.x*b.y - b.x*a.y;
}
ll cross(P a, P b, P c)
{
    return cross(b - a, c - a);
}
ll dis(V a)
{
    return a ^ a;
}
ll dis(P a, P b)
{
    return (dis(b - a));
}
P ans[MAXN], s[MAXN];
P p[MAXN];
P* convex(P* l)
{
    
    sort(l+1, l+1+n);
    P tmp(lim, lim);
    int pos = 0;
    int top = 0;
    for (int i = 1; i <=n; i++)
        if (l[i] < tmp)
            tmp = l[i], pos = i;
    for (int i = pos, cnt_ = 0; cnt_ < n; cnt_++, i =(i+1>n)?1:i+1)
    {
        while (top >= 2 && sgn(cross(s[top-2], s[top-1], l[i])) <= 0)
            top--;
        s[top] = l[i]; top++;
        pos = i;
    }
    int cnt = 0;
    for (int i = 0; i < top; i++)
    {
        cnt++;
        ans[cnt] = s[i];
    }
    top = 0;
    for (int i = pos, cnt_ = 0; cnt_ < n; cnt_++, i = (i - 1<1)?n:i-1)
    {
        while (top >= 2 && sgn(cross(s[top - 2], s[top - 1], l[i])) <= 0)
            top--;
        s[top] = l[i]; top++;
        pos = i;
    }
    for (int i = 1; i + 1 < top; i++)
    {
        cnt++;
        ans[cnt] = s[i];
    }
    n = cnt;
    return ans;
}
ll max_dis(P* p)
{
    int maxp = 1, minp = 1;
    ll  maxy =-1001, miny = 1001;
    for (int i = 1; i <=n; i++)
    {
        if (p[i].y > maxy) maxy = p[i].y, maxp = i;
        if (p[i].y < miny )miny = p[i].y, minp = i;
    }
    ll ans = max(dis(p[minp],p[maxp]),dis(p[(minp%n)+1],p[maxp]));
    for (int i = 1; i <=n; i++,minp=(minp+1>n)?1:(minp+1))
    {
        while (cross(p[(minp%n) + 1], p[(maxp % n)+1], p[minp]) > cross(p[(minp%n) + 1], p[maxp], p[minp]))
            maxp = (maxp+1) %n;
        ans = max(ans, dis(p[minp], p[maxp]));
        ans = max(ans, dis(p[(minp % n) +1], p[maxp]));
    }
    return ans;
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        P tmp;
        scanf("%d%d", &tmp.x, &tmp.y);
        p[i] = tmp;
    }
    printf("%d", max_dis(convex(p)));
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/rentu/p/11272517.html