7.5---两个正方形分成对半的直线(CC150)

最主要的思路:1,这条直线就是要把两个正方形的中点链接。

2,注意的特殊情况:中心点重合。

 

答案:

public class Solution {
    
    public static void main(String[] args){
        Point[] a = {new Point(136,6278),new Point(3958,6278),new Point(3958,2456),new Point(136,2456)};
        Point[] b = {new Point(-3898,11132),new Point(7238,11132),new Point(7238,-4),new Point(-3898,-4)};
        System.out.println(Arrays.toString(getBipartition(a,b)));
    }
    public static  double[] getBipartition(Point[] a, Point[] b) {
        // write code here
        double[] res = new double[2];
        Center c1 = new Center((a[0].x + a[1].x)/2.0,(a[0].y + a[3].y)/2.0);
        Center c2 = new Center((b[0].x + b[1].x)/2.0,(b[0].y + b[3].y)/2.0);
        
        System.out.println(c2.x);
        if(c1.x == c2.x && c1.y == c2.y){
            res[0] = (a[2].y-a[0].y)*1.0 / (a[2].x - a[0].x);
            res[1] = a[0].y - res[0] * a[1].x;
        }
        else{
            
            res[0] = (c2.y - c1.y) / (c2.x - c1.x);
            res[1] = c2.y - res[0] * c2.x;
        }
        
        return res;
    
    }
}

class Center{
    double x;
    double y;
    Center(double x, double y){
        this.x = x;
        this.y = y;
    }
}
class Point {
    int x;
    int y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public Point() {
        this.x = 0;
        this.y = 0;
    }
}

 

posted @ 2015-12-30 14:28  仔细思考一切  阅读(232)  评论(0)    收藏  举报