判断直线是否相交,若相交,则返回交点
2016-08-05 11:22
357 查看
//判断直线是否相交,若相交,则返回交点 #include<iostream> #include<vector> using namespace std; #define EPS 1e-6 typedef pair<double,double> CVector; typedef pair<double,double> CPoint; typedef pair<CPoint,CPoint> CLine; bool IsZero(double x){ return -EPS<x&&x<EPS; } CVector operator - (CPoint p,CPoint q){ return CVector(p.first-q.first,p.second-q.second); } double operator ^ (CVector p,CVector q){ return p.first*q.second-p.second*q.first; } double dist(CLine p,CLine q){//该距离是当p//q是的距离 return p.second.second-q.second.second; } CPoint operator + (CPoint p,CVector q){ return CPoint(p.first+q.first,p.second+q.second); } CVector operator * (double k,CVector p){ return CVector(k*p.first,k*p.second); } void intersect(CLine l,CLine m){ double x=(m.first-l.first)^(l.second-l.first); double y=(l.second-l.first)^(m.second-l.first); if(IsZero(x+y)){ if(IsZero(dist(l,m))) printf("重合\n"); else printf("平行\n"); return ; } CPoint u=m.first+(x/(x+y))*(m.second-m.first); printf("(%.2f,%.2f)\n",u.first,u.second); } int main() { vector<CLine> v; CPoint p,q; double x1,y1,x2,y2; for(int i=0;i<2;i++){ cin>>x1>>y1>>x2>>y2; p=CPoint(x1,y1),q=CPoint(x2,y2); v.push_back(CLine(p,q)); } intersect(v[0],v[1]); return 0; }即使两条线段没有交点也没关系,因为它求的是直线的交点。
相关文章推荐
- 判断两个链表是否相交并且返回第一个交点
- poj 1039 Pipe (判断 直线和 线段 是否相交 并 求交点)
- 判断直线与线段是否相交,相交则输出交点x轴坐标
- 判断线段是否相交的函数和求直线交点的函数
- poj 1269 知识点:直线相交判断,求相交交点
- Python判断直线和矩形是否相交的方法
- POJ - 3304 :Segments__判断直线和线段是否 相交
- POJ 1269 Intersecting Lines(直线相交判断,求交点)
- 判断单链表是否有环,如果有找出环的入口位置=>求两个相交链表的交点
- 如何判断两个链表是否相交,以及交点
- poj 3304 判断是否有与所有线段相交的直线
- 判断两个单链表是否相交及找到第一个交点
- 判断两个链表是否相交并找出交点
- 链表逆序、判断是否有环、求环的起点;两个链表是否相交、交点
- poj 1410(判断直线是否相交)
- poj 1269 Intersecting Lines(直线相交判断,求相交交点)
- POJ 3304 Segments (计算几何、判断直线与线段是否相交)
- 《链表》之判断两链表是否相交/交点
- 链表逆序、判断是否有环、求环的起点;两个链表是否相交、交点
- 判断两个链表是否相交?若相交,求交点(假设链表带环)