zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    HTML+CSS简单实现导航栏二级下拉菜单
    原创 | 我的个人微信公众号
    原创 | 喂,在吗?
    NodeJs实现邮箱验证
    JS排序算法(二)冒泡排序
    JS排序算法(一) 快速排序
    前端常见的布局方式
    JS继承方式
    前端Node实现简易的文件上传下载
    原生js实现深度克隆
  • 原文地址:https://www.cnblogs.com/yueyebigdata/p/5088731.html
Copyright © 2011-2022 走看看