计算几何-----》两直线是否相交

POJ 1127

  1 #include<set>
  2 #include<map>
  3 #include<stack>
  4 #include<bitset>
  5 #include<cmath>
  6 #include<string>
  7 #include<vector>
  8 #include<cstdio>
  9 #include<cstring>
 10 #include<iostream>
 11 #include<algorithm>
 12 #define pi acos(-1)
 13 #define close ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
 14 using namespace std;
 15 typedef long long ll;
 16 const int MAX_N=1000+50;
 17 const int INF=0x3f3f3f3f;
 18 const double EPS = 1e-10;
 19 ll mod = 1e9+7;
 20
 21
 22
 23 //考虑误差的加法运算
 24 double add(double a,double b){
 25     if(abs(a + b) < EPS * (abs(a) + abs(b))) return 0;
 26     return a+ b;
 27 }
 28
 29 //二维向量结构体
 30 struct P{
 31     double x,y;
 32     P(){}
 33     P(double x, double y) : x(x), y(y){
 34     }
 35     P operator + (P p){
 36         return P(add(x, p.x), add(y, p.y));
 37     }
 38     P operator - (P p){
 39         return P(add(x, -p.x), add(y, -p.y));
 40     }
 41     P operator * (double d){
 42         return P(x * d, y * d);
 43     }
 44     double dot(P p){  //内积
 45         return add(x * p.x, y * p.y);
 46     }
 47     double det(P p){ //外积
 48         return add(x * p.y, -y * p.x);
 49     }
 50 };
 51
 52 //判断点q是否在线段p1-p2上
 53 bool on_seg(P p1, P p2, P q){
 54     return (p1 - q).det(p2-q) == 0 && (p1 - q).dot(p2 - q) <= 0;
 55 }
 56
 57 //计算直线p1-p2与直线q1-q2的交点
 58 P intersection(P p1, P p2, P q1, P q2){
 59     return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
 60 }
 61
 62 int n;
 63 P p[MAX_N],q[MAX_N];
 64 int m;
 65 int a[MAX_N],b[MAX_N];
 66
 67 bool g[MAX_N][MAX_N];
 68
 69 void solve(){
 70     for(int i = 0; i < n; i++){
 71         g[i][i] = true;
 72         for(int j = 0; j < i; j++){
 73             //判断木棍i和木棍j是否有公共点
 74             if((p[i] - q[i]).det(p[j] - q[j]) == 0){
 75                 //平行时
 76                 g[i][j] = g[j][i] = on_seg(p[i], q[i], p[j])
 77                                 ||  on_seg(p[i], q[i], q[j])
 78                                 ||  on_seg(p[j], q[j], p[i])
 79                                 ||  on_seg(p[j], q[j], q[i]);
 80             }else{
 81                 //非平行时
 82                 P r = intersection(p[i], q[i], p[j], q[j]);
 83                 g[i][j] = g[j][i] = on_seg(p[i], q[i], r) && on_seg(p[j],q[j],r);
 84             }
 85         }
 86     }
 87     //通过Floyd_Warshall算法判断任意两点间是否相连
 88     for(int k = 0; k < n; k++){
 89         for(int i = 0; i< n; i++){
 90             for(int j = 0; j < n; j++){
 91                 g[i][j] |= g[i][k] && g[k][j];
 92             }
 93         }
 94     }
 95
 96     for(int i = 0; i < m; i++){
 97         puts(g[a[i] - 1][b[i] - 1] ? "CONNECTED" : "NOTCONNECTED");
 98     }
 99 }
100
101 int main(){
102     cin>>n;
103     for(int i = 0; i < n; i++){
104         cin>>p[i].x>>p[i].y>>q[i].x>>q[i].y;
105     }
106     solve();
107     int c, d;
108     while(~scanf("%d%d", &c, &d)) {
109         if(c==0 && d==0)    break;
110         if(g[c-1][d-1])
111             printf("CONNECTED\n");
112         else
113             printf("NOT CONNECTED\n");
114     }
115      return 0;
116 }
117
118 /*
119                 ********
120                ************
121                ####....#.
122              #..###.....##....
123              ###.......######              ###            ###
124                 ...........               #...#          #...#
125                ##*#######                 #.#.#          #.#.#
126             ####*******######             #.#.#          #.#.#
127            ...#***.****.*###....          #...#          #...#
128            ....**********##.....           ###            ###
129            ....****    *****....
130              ####        ####
131            ######        ######
132 ##############################################################
133 #...#......#.##...#......#.##...#......#.##------------------#
134 ###########################################------------------#
135 #..#....#....##..#....#....##..#....#....#####################
136 ##########################################    #----------#
137 #.....#......##.....#......##.....#......#    #----------#
138 ##########################################    #----------#
139 #.#..#....#..##.#..#....#..##.#..#....#..#    #----------#
140 ##########################################    ############
141 */

原文地址:https://www.cnblogs.com/Yinchen-One/p/9399449.html

时间: 2024-08-02 21:12:44

计算几何-----》两直线是否相交的相关文章

判断两直线是否相交 hdu1086

1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 struct line 7 { 8 double x1; 9 double y1; 10 double x2; 11 double y2; 12 }l[110]; 13 14 bool test(int i,int j) 15 { 16 int s=0; 17 double acd = (l[j].x1-l[i].x1)*(l[j].y1-l

判断两直线是否相交的叉积方法

struct point { double x,y; }; struct line { point a,b; }lines[110]; double chaji(point a,point b,point c,point d) { return (b.x-a.x)*(d.y-c.y)-(b.y-a.y)*(d.x-c.x); } bool isin(line l1,line l2) { if(chaji(l1.a,l2.a,l1.a,l1.b)*chaji(l1.a,l1.b,l1.a,l2.b

poj1039——计算几何 求直线与线段交点,判断两条直线是否相交

poj1039——计算几何  求直线与线段交点,判断两条直线是否相交 Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9439   Accepted: 2854 Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the de

POJ 3304 Segments(计算几何:直线与线段相交)

POJ 3304 Segments 大意:给你一些线段,找出一条直线能够穿过所有的线段,相交包括端点. 思路:遍历所有的端点,取两个点形成直线,判断直线是否与所有线段相交,如果存在这样的直线,输出Yes,但是注意去重. struct Point { double x, y; } P[210]; struct Line { Point a, b; } L[110]; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.

使用叉积判断两条直线是否相交

两条平面直线是否相交(直线长度大于0,可以重叠) 直线为(x1,y1),(x2,y2)和(x3,y3),(x4,y4) #include <iostream> using namespace std; int cj(int a1,int a2,int b1,int b2,int c1,int c2){     return (c1-a1)*(b2-a2)-(c2-a2)*(b1-a1);} int main(){    int x1,x2,x3,x4,y1,y2,y3,y4;   while(

判断两线段是否相交 模板

1 struct point 2 { 3 double x, y; 4 point( double _x = 0, double _y = 0 ) 5 { 6 x = _x; 7 y = _y; 8 } 9 point operator-( point t ) 10 { 11 return point( x - t.x, y - t.y ); 12 } 13 double operator*( point t ) 14 { 15 return x * t.y - y * t.x; 16 } 17

判断平面上两线段是否相交

计算几何基础——矢量和叉积 矢量 如果一条线段的端点是有次序之分的话,那么这种线段就称为 有向线段,如果有向线段p1p2的起点p1在坐标的原点,则可以把它称为矢量 p2 矢量的加减 设二维矢量 P = (x1, y1), Q = (x2, y2),则 P + Q = (x1 + x2, y1 + y2), P - Q = (x1 - x2, y1 - y2),且有 P + Q = Q + P, P - Q = -(Q - P) 矢量叉积 设矢量 P = (x1, y1), Q = (x2, y2

向量叉积判断两线段是否相交

判断两直线p1p2与q1q2是否相交,用向量叉积来判断 如果P x Q >0,则P在Q的顺时针方向: 如果P x Q <0,则P在Q的逆时针方向: 如果P x Q=0,则P与Q共线,可能同向也可能反向 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <iostream> typedef struct node {

编程求取直线一般式表达式,两直线交点

背景介绍 ??最近在水面无人艇(USV)模拟仿真中,用到了一些点和线的关系求解,本文主要讲述一下两点确认直线,点到直线距离,两条直线的交点等问题的解决方法,并给出python程序.部分内容非原创,文中给出链接,需要者可以参考. 两点确定直线 表达式定义 ??空间直线的表达式有多种,比如一般式Ax+By+C=0.点斜式y-y0=k(x-x0).截距式x/a+y/b=1.两点式:(y-y1)/(y1-y2)=(x-x1)/(x1-x2)等,它们具有彼此的约束条件,如下所示. ??由上可以看出来,一般