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条直线,最多能将这一平面划分成多少个部分? 没有直线时有一个空间:( ...
随机推荐
- C# vs C++ Performance
http://www.codeproject.com/Articles/212856/Head-to-head-benchmark-Csharp-vs-NET
- CMAKE使用
http://www.cppblog.com/tx7do/archive/2010/08/19/124000.html http://blog.csdn.net/dbzhang800/article/ ...
- iOS直播点赞动画,iOS直播心型点赞动画
https://github.com/songxing10000/LikeAnimation-PraiseAnimation
- authorization配置
在 Web.config 文件的<configuration>标记的子标记<authorization>和</authorization>之间用于设置应用程序的授权 ...
- asp.net(C#)页面事件顺序
asp.net(C#)页面事件顺序 http://www.cnblogs.com/henw/archive/2012/02/09/2343994.html 1 using System.Data; ...
- CSS 简介
CSS 简介 需要具备的基础知识 在继续学习之前,你需要对下面的知识有基本的了解: HTML XHTML CSS 概述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义 ...
- linux 下 zip unzip压缩与解压
注:*压缩成限.zip格式文件 常用解压缩: [root@mysql test]# unzip -o test.zip -d tmp/ 将压缩文件test.zip在指定目录tmp下解压缩,如果已有相同 ...
- OC-基本
#import <Foundation/NSObjCRuntime.h> //import: //1,同#include一样, 拷贝文件内容 //2,可以自动防止文件的内容被重复拷贝 /* ...
- mysql存储过程对900w数据进行操作测试
新增索引:LTER TABLE `tablename` ADD INDEX `sdhid` (`createTime`) USING BTREE ;[SQL]ALTER TABLE `tablenam ...
- 今天是JQ 的slideUp 和 slideDown 的点击事件
先贴代码,再讲详细事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...