判断两线段相交

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
int n;
struct point
{
    double x;
    double y;
};
struct v
{
    point s;
    point e;
} q[102];
int sum;
double multi(struct point p1,struct point p2,struct point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        sum=0;
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf%lf%lf",&q[i].s.x,&q[i].s.y,&q[i].e.x,&q[i].e.y);
        }
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(max(q[i].s.x,q[i].e.x)>=min(q[j].s.x,q[j].e.x)&&
                        min(q[i].s.x,q[i].e.x)<=max(q[j].s.x,q[j].e.x)&&
                        max(q[i].s.y,q[i].e.y)>=min(q[j].s.y,q[j].e.y)&&
                        min(q[i].s.y,q[i].e.y)<=max(q[j].s.y,q[j].e.y)&&
                        multi(q[j].s,q[i].e,q[i].s)*multi(q[i].e,q[j].e,q[i].s)>=0&&
                        multi(q[i].s,q[j].e,q[j].s)*multi(q[j].e,q[i].e,q[j].s)>=0)
                    sum++;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

  

时间: 2024-10-21 14:10:04

判断两线段相交的相关文章

You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交

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

poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are

BNUOJ33566 Cycling Roads(并查集+判断两线段相交)

Cycling Roads Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on Ural. Original ID: 1966 64-bit integer IO format: %lld      Java class name: (Any) Prev Submit Status Statistics Discuss Next Font Size:  +   - Type:   None Graph T

判断两线段是否相交 模板

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

ACM1558两线段相交判断和并查集

Segment set Problem Description A segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set. Input In the first line ther

判断两线段是否相交

今日集训第一日,遇到了判断线段相交问题.跟面积问题一样,这个同样可以用叉积来解决. 数学原理证明: 首先引出计算几何学中一个最基本的问题:如何判断向量在的顺时针方向还是逆时针方向? 把p0定为原点,p1的坐标是(x1,y1),p2的坐标是(x2,y2).向量的叉积(cross product)实际上就是矩阵的行列式: 代码实现: 1 int direction(Point p0, Point p1, Point p2) { 2 int px02 = p2.x - p0.x; 3 int py02

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

判断两直线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 {

两线段相交求交点

算法一 #include #include #include struct POINT { int x; int y; }; bool IsLineSegmentCross(POINT pFirst1, POINT pFirst2, POINT pSecond1, POINT pSecond2) { //每个线段的两点都在另一个线段的左右不同侧,则能断定线段相交 //公式对于向量(x1,y1)->(x2,y2),判断点(x3,y3)在向量的左边,右边,还是线上. //p=x1(y3-y2)+x2

uva11343 - Isolated Segments(两线段相交)

题意:给你一些线段,求没有和其他线段相交的线段数量 公式:p1*p2=(x1*x2,y1*y2)(内积),p1xp2=(x1*y2,x2*y1)(外积) 判断q是否在线段p1-p2上面,根据(p1-q)x(p2-q)=0来判断q是否在直线p1-p2上. 利用内积(p1-q)*(p2-q)<0判断q是否在p1-p2之间. p1-p2,q1-q2的交点: (x,y)=p1+(p2-p1)*((q2-q1)x(q1-p1)/((q2-q1)x(p2-p1))): 推理:把p1-p2直线写成点p1+t(