题目描述
在二维平面上,有两个正方形,请找出一条直线,能够将这两个正方形对半分。假定正方形的上下两条边与x轴平行。
给定两个vecotrA和B,分别为两个正方形的四个顶点。请返回一个vector,代表所求的平分直线的斜率和截距,保证斜率存在。
测试样例:
[(0,0),(0,1),(1,1),(1,0)],[(1,0),(1,1),(2,0),(2,1)]
返回:[0.0,0.5]
/*
struct Point {
int x;
int y;
Point() :
x(0), y(0) {
}
Point(int xx, int yy) {
x = xx;
y = yy;
}
};*/
class Bipartition {
public:
static bool cmp(const Point &A, const Point &B) {
if (A.x != B.x) {
return A.x < B.x;
}
return A.y < B.y;
}
vector<double> getBipartition(vector<Point> A, vector<Point> B) {
// write code here
sort(A.begin(), A.end(), cmp);
sort(B.begin(), B.end(), cmp);
double x1 = (A[3].x + A[0].x)/2.0;
double y1 = (A[3].y + A[0].y)/2.0;
double x2 = (B[3].x + B[0].x)/2.0;
double y2 = (B[3].y + B[0].y)/2.0;
vector<double> res;
double res1 =(y2- y1)/(x2-x1);
double res2 = y2 - x2*res1;
res.push_back(res1);
res.push_back(res2);
return res;
}
};