判断线段是否相交

struct Point{
    int x1,y1,x2,y2;
}A[20];
int arr[20];

struct Node{
    int x,y;
};
bool judge(Point p1,Point p2){   //相交最基本的条件
    if(max(p1.x1,p1.x2)<min(p2.x1,p2.x2)||max(p1.y1,p1.y2)<min(p2.y1,p2.y2)||min(p1.x1,p1.x2)>max(p2.x1,p2.x2)||min(p1.y1,p1.y2)>max(p2.y1,p2.y2))
        return false;
    return true;
}

int cultilate(Point p1,Point p2){      //判断是不是在异侧,或有个点在另外一个点另外一条直线上
    Node AB,AC,AD,CD,CB,CA;
    AB.x=p1.x2-p1.x1,AB.y=p1.y2-p1.y1;
    AC.x=p2.x1-p1.x1,AC.y=p2.y1-p1.y1;
    AD.x=p2.x2-p1.x1,AD.y=p2.y2-p1.y1;
    CD.x=p2.x2-p2.x1,CD.y=p2.y2-p2.y1;
    CB.x=p1.x2-p2.x1,CB.y=p1.y2-p2.y1;
    CA.x=p1.x1-p2.x1,CA.y=p1.y1-p2.y1;  
    if((AB.x*AC.y-AB.y*AC.x)*(AB.x*AD.y-AB.y*AD.x)<=0&&(CD.x*CB.y-CD.y*CB.x)*(CD.x*CA.y-CD.y*CA.x)<=0)
        return 1;
    return 0;
}

原文地址:https://www.cnblogs.com/qq-1585047819/p/10701972.html

时间: 2024-08-09 00:07:21

判断线段是否相交的相关文章

Jack Straws(判断线段是否相交 + 并查集)

/** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840  题意: 判断线段是否相交  (包括间接相交) 输入: N(代表有n条线段) sx  sy  ex  ey(一条直线的两端点的坐标) : : : a b(判断第a条和第b条线段是否相交) : : : 0 0输入0 0 询问结束 输出:若相交输出  CONNECTED 否则         NOT CONNECTED */ #includ

zoj 1010 Area 判断线段是否相交(把线段扩充一倍后 好处理) + 多边形求面积

题目来源: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 题意:  给定n个点的, 如果这n个点不能形成多边形 以及 n < 3 时, 输出, "Impossible",  否则 输出 多边形的面积. 分析: 这题主要在 分析  n 个点 是否形成 多边形.  枚举 每条边,  看 这条边 是否与 其他 n - 3 条边 不规范相交. (当处理 其他 边时, 我们采用 扩充线段一倍) 代码如下: con

zoj 1648 判断线段是否相交

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=648 Circuit Board Time Limit: 2 Seconds      Memory Limit: 65536 KB On the circuit board, there are lots of circuit paths. We know the basic constrain is that no two path cross each other,

POJ 1127 Jack Straws ( 求直线交点, 判断线段是否相交(含端点) )

题目:传送门 题意: 给你 n 条线段的两个端点, 然后有多次询问, 每次询问, 问你线段 x 和 线段 y 是否相交. 若线段 A 和线段 B 相交且线段 A 和线段 C 相交,那么线段 B 和线段 C 相交.     1 < n < 13 题解: 暴力求线段是否相交, 然后再跑个 Floyd 或者并查集都可以的. #include <iostream> #include <stdio.h> #include <string.h> #include <

codeForce-589D Boulevard(判断线段是否相交)

题目大意:n个人.一个区间.每个人都会在某个时间段内按相同的速度(所有人的速度都一样,都是1或-1)在他的区间内从一个端点走到另一个端点(只走一次).问每个人会与几个人碰面. 题目分析:将时间看成一个维度,区间位置看成另一个维度.那么每个人的状态便构成了一条二维线段.只需判断有几条线段与该线段相交. 判断两平面线段是否相交: 设线段1的两端点分别为A.B,线段2的两端点分别为C.D. 当两条线段平行时,只需要判断点C或点D是否在线段AB上即可.点C在线段AB上的判断:先判断向量CA与向量CB是否

hdu 1558 Segment set(并查集+判断线段是否相交)

代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int father[1005]; int son_cnt[1005]; char s[5]; //int cnt; struct point { double x,y; }; point a[1005],b[1005]; int find_father(int x) { int r=x; while(fathe

hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8683 Accepted Submission(s): 4227 Problem Description Many geometry(几何)problems were designed in the ACM/ICPC.

poj Pick-up sticks(判断线段相交)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11537   Accepted: 4337 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

POJ 2653 Pick-up sticks (判断线段相交)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10330   Accepted: 3833 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin