reference:
http://en.wikipedia.org/wiki/Rotating_calipers
http://cgm.cs.mcgill.ca/~orm/rotcal.frame.html
P代表一个凸多边形
支撑线(lines of support):
与P相交的直线,使得多边形的内部(即除边界外的部分)全部在
该直线的一侧。
対踵点对(anti-podal pair):
P上一对定点,使得过这两点存在一对平行的支撑线。
根据支撑线与P相交的情况分类:
対踵点对的对数
1)点-点 1
2)点-边 2
3)边-边 4
只有在 2) 3) 两种情况下才会发生対踵点对的改变
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <limits>
#include <set>
#include <map>
using namespace std;
#define MIN(a, b) a < b ? a : b
#define MAX(a, b) a > b ? a : b
#define F(i, n) for (int i=0;i<(n);++i)
#define REP(i, s, t) for(int i=s;i<=t;++i)
#define IREP(i, s, t) for(int i=s;i>=t;--i)
#define REPOK(i, s, t, o) for(int i=s;i<=t && o;++i)
#define MEM0(addr, size) memset(addr, 0, size)
#define LBIT(x) x&-x
#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398
#define MAXN 100000
#define MAXM 100
#define MOD 20071027
typedef long long LL;
const double maxdouble = numeric_limits<double>::max();
const double eps = 1e-10;
const int INF = 0x7FFFFFFF;
struct Point {
double x, y;
Point(){};
Point(double x, double y):x(x),y(y){};
};
typedef Point Vector;
Vector operator - (Point A, Point 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);
}
// 用以避免精度误差
int dcmp(double x) {
if (fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator == (const Point& a, const Point& b) {
if (dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0)
return true;
return false;
}
// 需要更高精度,使用dcmp
bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
// 内积
double Dot(Vector A, Vector B) {
return A.x*B.x + A.y*B.y;
}
// 外积 axb
// theta 为 a 逆时针转到 b 所需角度
// theta < pi : axb>0
// theta > pi : axb<0
// theta = pi : axb=0
double Cross(Vector A, Vector B) {
return A.x*B.y-A.y*B.x;
}
// 模
double Length(Vector A) {
return sqrt(Dot(A, A));
}
// 模的平方
double Length2(Vector A) {
return Dot(A, A);
}
double Area2(Point A, Point B, Point C) {
return Cross(B-A, C-A);
}
// 两向量夹角
double Angle(Vector A, Vector B) {
return acos(Dot(A, B) / Length(A) / Length(B));
}
// 将向量A]逆时针旋转rad度
Vector Rotate(Vector A, double rad) {
return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}
// 单位法线 左转90度后归一化
Vector Normal(Vector A) {
double L = Length(A);
return Vector(-A.y/L, A.x/L);
}
// 相交直线求交点(点向式)
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;
}
// 判断线段相交(不含端点)
bool SeqmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {
double c1 = Cross(a2-a1, b2-a1), c2 = Cross(a2-a1, b1-a1),
c3 = Cross(b2-b1, a2-b1), c4 = Cross(b2-b1, a1-b1);
if (dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0)
return true;
return false;
}
// 点在线上
bool OnSegment(Point p, Point a1, Point a2) {
return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}
// 点到线段距离
double DistanceToSegment(Point P, Point A, Point B) {
if (A == B)
return Length(P - A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if (dcmp(Dot(v1, v2)) < 0) // 投影在A的外侧
return Length(v2); // 投影在B的外侧
else if (dcmp(Dot(v1, v3)) > 0)
return Length(v3);
else
return fabs(Cross(v1, v2)) / Length(v1);
}
// 余弦定理 1,2为临边, 3为对边
double cos_theorem(double edge1, double edge2, double edge3) {
return acos((pow(edge1, 2.0) + pow(edge2, 2.0) - pow(edge3, 2.0)) / (2*edge1*edge2));
}
// 多边形有向面积
// 输入为逆时针定点序列
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;
}
// Graham凸包算法nlogn
// p中元素顺序会被破坏
// 返回逆时针的定点序列
// 如果不希望在凸包的边上有输入点,把两个 <= 改成 <
int ConvexHull(Point *p, int n, Point *ch) {
sort(p, p+n);
int m = 0;
for (int i=0;i<n;++i) {
while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0)
--m;
ch[m++] = p[i];
}
int k = m;
for (int i=n-2;i>=0;--i) {
while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0)
--m;
ch[m++] = p[i];
}
if (n > 1)
--m;
return m;
}
// 旋转卡壳 rotating calipers
// return: 点集直径的平方
double diameter2(Point *p, int num, Point *v) {
int n = ConvexHull(p, num, v);
// 特判退化为点或线段的情况
if(n == 1) return 0;
if(n == 2) return Length2(p[0] - p[1]);
int i = 0, j = 0;
for (int k=0;k<n-1;++k) {
if (v[k] < v[i])
i = k;
if (v[j] < v[k])
j = k;
}
double _max = Length2(v[i]-v[j]);
int si = i, sj = j; // 保存原来的位置
while(i != sj || j != si) { // 方向旋转180
_max = max(_max, Length2(v[i]-v[j]));
if (Cross(v[(i+1)%n]-v[i], v[(j+1)%n]- v[j]) < 0) {
i = (i+1)%n;
} else
j = (j+1)%n;
}
return _max;
}
// ccw = (B-A) X (C-A)
// ret > 0 : C 在AB左侧
// ret < 0 : C 在AB右侧
#define ccw(A, B, C) (Cross((B)-(A),(C)-(A)))
Point pts[4*MAXN + 1];
Point v[4*MAXN + 1];
int main()
{
freopen("input.in", "r", stdin);
int T, n, cnt;
double x, y, w;
cin >> T;
F(ncases, T) {
cin >> n;
cnt = 0;
F(i, n) {
cin >> x >> y >> w;
pts[cnt++] = Point(x, y);
pts[cnt++] = Point(x, y+w);
pts[cnt++] = Point(x+w, y);
pts[cnt++] = Point(x+w, y+w);
}
double d2 = diameter2(pts, cnt, v);
printf("%.0f\n", d2);
}
return 0;
}