直线平分正方形

题目描述
在二维平面上,有两个正方形,请找出一条直线,能够将这两个正方形对半分。假定正方形的上下两条边与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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值