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);
}
};

最新文章

  1. 游戏编程系列[2]--游戏编程中RPC与OpLog协议的结合--序
  2. Linux CentOS6.8下解压安装mysql-5.7.14完整介绍
  3. 你需要知道的MySQL开源存储引擎TokuDB
  4. Hive Over HBase
  5. [Java Web] 2、Web开发中的一些架构
  6. VS2013无法启动 IIS Express Web解决办法
  7. jQuery基础之(四)jQuery创建DOM元素
  8. spark1.4加载mysql数据 创建Dataframe及join操作连接方法问题
  9. VS2012编译出来的程序,在XP上运行,出现“.exe 不是有效的 win32 应用程序” “not a valid win32 application”
  10. 【问题备注】VS2012不能输入代码,文字&hellip;
  11. 微软源代码管理工具TFS2013安装与使用图文教程
  12. C#Excel导出导入
  13. php扩展类开发实例
  14. Linux IO控制命令生成
  15. 各个浏览器开启CSS Grid Layout的方式
  16. fastcgi的介绍,原理及配置
  17. SVN的branch合并到trunk的过程思考
  18. Angular4.0引入laydate.js日期插件方法
  19. JavaFile、递归、字节流、字符流整理
  20. 类似于PLC上升沿的TRIO代码示例

热门文章

  1. struts2.3.24 + spring4.1.6 + hibernate4.3.11+ mysql5.5.25开发环境搭建及相关说明
  2. JMeter源码集成到Eclipse
  3. js动态切换图片
  4. java PKCS7Padding 加密Cannot find any provider supporting AES/CBC/PKCS7Padding 解决办法
  5. [瞎JB写] C++多态
  6. WCF MSMQ消息队列与离线操作
  7. 【开学季】自学嵌入式开发|四核开发板|4412开发板|ARM+Android+linux技术
  8. 孙鑫视频学习:“operator +=” 不明确的问题解决方法
  9. codeforces 711A A. Bus to Udayland(水题)
  10. 酷派5890 ROM教程