7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in half. Assume that the top and the bottom sides of the square run parallel to the x-axis

这道题给了我们两个正方形,让我们求一条可以讲这两个正方形平均分为两个部分的直线,前提是两个正方形都和x轴平行。那么我们首先要知道啥样的直线才能将一个正方形平均分为两部分呢,答案是任意一条过正方形中心的直线,那么将两个正方形平均分为两部分就是连接两个正方形中心的直线。这道题需要我们建立多个类,点,直线,和正方形,是一道考察面向对象程序设计的好题目。其中点类由两个double型的变量表示,直线类由两个点类表示,正方形类由左上顶点和右下顶点还有边长表示。比较重要的两个子函数extend和cut,其中extend是求从一个中心连向另一个中心,且交另一个中心的外边缘的点,至于是内边缘还是外边缘由第三个参数的正负所决定。cut函数是求连接两个中心的直线分别和两个正方形的外边缘的交点,两点确定一条直线即可。可能作为读者的你会问,为啥这么麻烦,直接将两个中点一连不就是直线了,为啥要这么麻烦,这么装B。没错,楼主就是要带你装B带你飞,这解法是为了防止人家要求满足题意的线段,那么线段两个端点正好在正方形的边上,没错,就是这么拉风,参见代码如下:

class Point {
public:
double _x, _y;
Point(double x, double y): _x(x), _y(y) {};
}; class Line {
public:
Point _start, _end;
Line(Point start, Point end): _start(start), _end(end) {};
}; /*
(left, top)_________
| |
| | size
|_________|
(right, down)
*/
class Square {
public:
double _left, _top, _right, _down, _size;
Square(double left, double top, double right, double down, double size): _left(left), _top(top), _right(right), _down(down), _size(size) {};
Point middle() {
return Point((_left + _right) * 0.5, (_top + _down) * 0.5);
}
// Return the point whrere the line connecting mid1 and mid2 intercepts the edge of square 1.
Point extend(Point mid1, Point mid2, double size) {
double xdir = mid1._x < mid2._x ? - : ;
double ydir = mid1._y < mid2._y ? - : ;
if (mid1._x == mid2._x) return Point(mid1._x, mid1._y + ydir * size * 0.5);
double slope = (mid1._y - mid2._y) / (mid1._x - mid2._x);
double x1 = , y1 = ;
if (fabs(slope) == ) {
x1 = mid1._x + xdir * size * 0.5;
y1 = mid1._y + ydir * size * 0.5;
} else if (fabs(slope) < ) {
x1 = mid1._x + xdir * size * 0.5;
y1 = slope * (x1 - mid1._x) + mid1._y;
} else {
y1 = mid1._y + ydir * size * 0.5;
x1 = (y1 - mid1._y) / slope + mid1._x;
}
return Point(x1, y1);
}
// Calculate the line that connecting two mids
Line cut(Square other) {
Point p1 = extend(middle(), other.middle(), _size);
Point p2 = extend(middle(), other.middle(), - * _size);
Point p3 = extend(other.middle(), middle(), other._size);
Point p4 = extend(other.middle(), middle(), - * other._size);
Point start = p1, end = p1;
vector<Point> points = {p2, p3, p4};
for (int i = ;i < points.size(); ++i) {
if (points[i]._x < start._x || (points[i]._x == start._x && points[i]._y < start._y)) {
start = points[i];
} else if (points[i]._x > end._x || (points[i]._x == end._x && points[i]._y > end._y)) {
end = points[i];
}
}
return Line(start, end);
}
};

[CareerCup] 7.5 A Line Cut Two Squares in Half 平均分割两个正方形的直线的更多相关文章

  1. [CareerCup] 7.6 The Line Passes the Most Number of Points 经过最多点的直线

    7.6 Given a two-dimensional graph with points on it, find a line which passes the most number of poi ...

  2. codeforces 425D Sereja and Squares n个点构成多少个正方形

    输入n个点,问可以构成多少个正方形.n,xi,yi<=100,000. 刚看题的时候感觉好像以前见过╮(╯▽╰)╭最近越来越觉得以前见过的题偶尔就出现类似的,可是以前不努力啊,没做出来的没认真研 ...

  3. [LeetCode] Magic Squares In Grid 网格中的神奇正方形

    A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...

  4. BZOJ 题目1036: [ZJOI2008]树的统计Count(Link Cut Tree,改动点权求两个最大值和最大值)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 8421  Solved: 3439 [Submi ...

  5. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  6. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  7. Shell split character line by line

    while read line      do            account=`echo "$line"| cut -c1-9`'|'            account ...

  8. poj3347 Kadj Squares【计算几何】

    Kadj Squares Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3594   Accepted: 1456 Desc ...

  9. shell中与运算 cut切分行 if while综合在一起的一个例子

    前言: 公司要统计 treasury库hive表磁盘空间,写了个脚本,如下: 查询hive仓库表占用hdfs文件大小: hadoop fs -du -h  /user/hive/warehouse/t ...

随机推荐

  1. 在Asp.net MVC中使用Authorization Manager (AzMan)进行Windows用户身份认证

    背景 创建需要通过Windows用户进行身份认证的Asp.net MVC应用 要点 在Asp.net MVC应用基于Windows用户进行身份认证的方法有很多,如MVC自带的Windows认证就经常被 ...

  2. 长文件名导致的0x80070057

    今天遇到件怪事. 把一个视频集(86G)从电脑硬盘转移动硬盘的时候里面时报里面的两个文件夹里的视频和字幕不能复制 错误代码0x80070057 这个视频集是从校内PT是下下来的,电脑是联想Y560-w ...

  3. 谈谈Java的集合组件

    让我们一起谈谈Java的集合组件 我们在使用Java的时候,都会遇到并使用到Java的集合.在这里通过自己的理解和网上的资源对Java的集合方面的使用做一个简单的讲解和总结. Java主要分为3个集合 ...

  4. 大数据架构-使用HBase和Solr将存储与索引放在不同的机器上

    大数据架构-使用HBase和Solr将存储与索引放在不同的机器上 摘要:HBase可以通过协处理器Coprocessor的方式向Solr发出请求,Solr对于接收到的数据可以做相关的同步:增.删.改索 ...

  5. 烂泥: KVM虚拟机Linux系统增加硬盘

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. Linux虚拟机在使用过程中,硬盘空间不够使用.由于前期没有做LVM,所以只能手动添加新的硬盘. 给虚拟机添加硬盘有两种方法: 1.通过virsh at ...

  6. kvm解决1000M网卡问题

    1.当我们安装完虚拟机, 发现虚拟机竟然是 100M 网络, 传输速率很低, 那是怎么导致的呢,如何来解决呢? 需要我们修改 vm01.xml 配置文件网卡段,添加如下红色标记行,改 为 e1000, ...

  7. 开启mysql慢查询

    Linux查看mysql 安装路径一.查看文件安装路径由于软件安装的地方不止一个地方,所有先说查看文件安装的所有路径(地址).这里以mysql为例.比如说我安装了mysql,但是不知道文件都安装在哪些 ...

  8. Lighttpd

    一.简介 Lighttpd 是一个德国人领导的开源Web服务器软件,其根本的目的是提供一个专门针对高性能网站,安全.快速.兼容性好并且灵活的web server环境.具有非常低的内存开销,cpu占用率 ...

  9. mrunit for wordcount demo

    import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.had ...

  10. java常用方法总结

    最近打算换工作,还是需要补一下面试的基础知识,写了一些面试中可能会用到的常用算法.方法,以便复习 //99乘法表 /** * 1*1 * 1*1 1*2 * 1*1 1*2 1*3 * …… * */ ...