7.3 Given two lines on a Cartesian plane, determine whether the two lines would intersect.

这道题说是在笛卡尔坐标系中,让我们确定两条直线是否相交。

那么我们首先要写个直线的类来表示直线,最常见的表示方法为y=kx+b,k为斜率,b为与y轴的交点,然后我们来考虑什么情况下两条直线会相交,首先,如果两条直线完全重合的话,应该也算是相交的,其次如果两条直线不平行的话,那么也是相交的,那么我们判断相交条件主要基于这两点,首先看它们的斜率是否相等,如果斜率不相等,肯定是相交的,其次看它们的于y轴的交点是否相同,如果相同肯是相交的,这样就把两条直线重合的情况也包括了。还有需要注意的是,我们不能用int型来表示k和b,而且判断相等的时候最好也不要用'==',而是引入epsilon,赋一个超小值,只要两个数之差小于epsilon,我们就可认定它们相等,反之大于epsilon,则不等。

class Line {
public:
const static double _epsilon = 0.00001;
double _slope;
double _yintercept;
Line(double s, double y): _slope(s), _yintercept(y) {};
bool intersect(Line line2) {
return abs(_slope - line2._slope) > _epsilon || abs(_yintercept - line2._yintercept) < _epsilon;
}
};

[CareerCup] 7.3 Line Intersection 直线相交的更多相关文章

  1. Two analytical 2d line intersection in OpenCASCADE

    Two analytical 2d line intersection in OpenCASCADE eryar@163.com Abstract. OpenCASCADE geometric too ...

  2. POJ 1269 Intersecing Lines (直线相交)

    题目: Description We all know that a pair of distinct points on a plane defines a line and that a pair ...

  3. Revit API判断直线相交关系移动风管

    start )             );         )) )) );         XYZ xyz12 = lCurve1.Curve.get_EndPoint();         XY ...

  4. poj 1269 Intersecting Lines(直线相交)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8637   Accepted: 391 ...

  5. 2017 ACM-ICPC乌鲁木齐网络赛 B. Out-out-control cars(计算几何 直线相交)

    题目描述 Two out-of-control cars crashed within about a half-hour Wednesday afternoon on Deer Park Avenu ...

  6. poj2074Line of Sight(直线相交)

    链接 几何细节题. 对于每一个障碍物可以求出它在地产线上的覆盖区间,如下图. 紫色部分即为每个障碍物所覆盖掉的区间,求出所有的,扫描一遍即可. 几个需要注意的地方:直线可能与地产线没有交点,可视区间可 ...

  7. 直线相交 POJ 1269

    // 直线相交 POJ 1269 // #include <bits/stdc++.h> #include <iostream> #include <cstdio> ...

  8. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  9. poj 1556 zoj1721 BellmanFord 最短路+推断直线相交

    http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

随机推荐

  1. IOS的UI基础02

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. apt-get

    更新版本: apt-get --reinstall install apache2 卸载: apt-get remove apache2          只删除软件包 apt-get autorem ...

  3. 用luke看索引

    Luke是一个用于Lucene搜索引擎的第三方工具,它可以访问现有Lucene的索引,并允许您显示和修改.可以看每篇文档建立了哪些索引,验证有没有成功建立了索引.不然建立了,不能确定有没有成功. 可以 ...

  4. Effective Java 38 Check parameters for validity

    For public methods, use the Javadoc @throws tag to document the exception that will be thrown if a r ...

  5. Java API 快速速查宝典

    Java API 快速速查宝典 作者:明日科技,陈丹丹,李银龙,王国辉 著 出版社:人民邮电出版社 出版时间:2012年5月 Java编程的最基本要素是方法.属性和事件,掌握这些要素,就掌握了解决实际 ...

  6. ELK 信息统计分析-2

    Range 按数值类型的字段聚合统计 { "query": { "match_all": {} }, "aggs": { "ter ...

  7. JS键盘事件监听

    window.onload=function(){ var keyword = document.getElementById("keyword"); keyword.onkeyu ...

  8. makefile自动生成目标与依赖的关系

    有main.c: #include <stdio.h> #include "command.h" int main(int argc, const char *argv ...

  9. OpenStack虚拟机状态

    OpenStack创建一个虚拟机,涉及到三种状态,vm_state,task_state和power_state. 先总结几点: 电源状态(power_state):是hypervisor的状态,从计 ...

  10. PL/0 词法分析器

    PL/0 词法分析器 #include<stdio.h> #include <ctype.h> #include <stdlib.h> #include <s ...