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;
}
}
7.5---两个正方形分成对半的直线(CC150)的更多相关文章
- H5一行显示两个正方形
1)有时候一些图片会是正方形或者长方形,对于这样的图片一般都是居中显示到正方体内,代码如下: .exhibition_list img{width:100%;position: relative;t ...
- [CareerCup] 7.5 A Line Cut Two Squares in Half 平均分割两个正方形的直线
7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in ha ...
- [Mac]如何让两个窗口各占半个屏幕
OS X中的拆分视图El Capitan或更高版本允许您使用两个应用程序填充Mac屏幕,而无需手动移动和调整窗口大小. 进入拆分视图 按住 窗口左上角的全屏按钮 . 当您按住按钮时,窗口会缩小,您可 ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目5
2014-03-20 02:20 题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分. 解法:穿过对称中心的线会将面积等分,所以连接两个中心即可.如果两个中心恰好重合,那么任意穿 ...
- 如何从二维平面n个点中寻找距离最近两个点?
如何理解分治算法 什么是分治算法?简单来说就是"分而治之",也就是将原问题划分成n个规模较小的,并且结构与原问题相似的子问题,然后去递归地解决这些子问题,最后再合并其结果,就得到原 ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- [LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 用dx实现半透遮挡效果
四种技术方案: 1.两次绘制role实现半透遮挡2.背景中已经包含building, 则额外绘制一次半透的building即可实现半透遮挡3.利用building的遮罩实现半透遮挡, 利用rtt技术4 ...
- 平面上画n条直线,最多能将平面分成多少部分?
转自:http://blog.csdn.net/cywosp/article/details/6724522 在一个平面上画1999条直线,最多能将这一平面划分成多少个部分? 没有直线时有一个空间:( ...
随机推荐
- cmake 编译 c++ dll 的一个例子
CMakeLists.txt project(xxx) add_library(xxx SHARED xxx.cpp) add_executable(yyy yyy.cpp) target_link_ ...
- 分享一个控制JS 浏览器缓存的解决办法。
JS 缓存的问题一直都是我们又爱又恨的东西.也是我们比较头痛的问题, 一方面为了提高网站响应速度,减少服务器的负担,和节省带宽,将需要将静态资源缓存在客户端, 但是另一方面,当js 文件有改动的时候 ...
- draw9的使用说明
转载来自:http://isux.tencent.com/android-ui-9-png.html 在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压缩,这样就出现了可以 ...
- spring boot入门例子
最近学习spring boot,总结一下入门的的基础知识 1新建maven项目,修改pom.xml <project xmlns="http://maven.apache.org/PO ...
- iOS 在UILabel显示不同的字体和颜色(转)
转自:http://my.oschina.net/CarlHuang/blog/138363 在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串, ...
- HBase中的压缩算法比较 GZIP、LZO、Zippy、Snappy [转]
网址: http://www.cnblogs.com/panfeng412/archive/2012/12/24/applications-scenario-summary-of-compressio ...
- 浅谈JavaScript中的Function引用类型
引言 在JavaScript中最有意思的就是函数了,这一切的根源在于函数实际上是一个对象.每一个函数都是Function类型的实例,而且都和其他引用类型的实例一样具有属性和方法.函数作为一个对象,因此 ...
- svn branch and merge(svn切换分支和合并)详解
下文的实践主要是参考了TortoiseSVN的帮助文档和Subversion的在线文档,Subversion的在线文档:http://svnbook.red-bean.com/en/1.5/svn-b ...
- Eclipse插件checkstyle安装使用
方法一: 1.Eclipse中,选择Help->Software Updates->Find and Install 2.选择 Search for new features to ...
- Java 线程Thread.Sleep详解
我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢? 思考下面这两个问题: 1.假设现在是 2008-4-7 12:00:00.000,如果 ...