关于直线与线段

求直线与线段的交点

求交点

定比分点法,用叉积求面积,用面积比代替线段长度比

Point inter_point(Point A,Point B,Point C,Point D)///返回直线AB和线段CD的交点
{
    if(!intersect(A,B,C,D)) return {-INF*1.0,0};///判断直线AB是否与线段CD相交,不相交必须须特判
    double area1=fabs((B-A)*(C-A));
    double area2=fabs((B-A)*(D-A));
    double x=(area1*D.x+area2*C.x)/(area1+area2);
    double y=(area1*D.y+area2*C.y)/(area1+area2);
    return {x,y};
}

判断相交,叉积判断左右拐(跨立实验)

bool intersect(Point A,Point B,Point C,Point D)///判断直线AB是否与线段CD相交
{
    if(((C-A)*(B-A))*((D-A)*(B-A))<eps) return true;
    return false;
}

如果判断线段相交则须用快速排除实验和跨立实验

求线段交点必须先判断相交

时间: 2024-11-02 02:47:15

关于直线与线段的相关文章

poj 3304 直线与线段相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12161   Accepted: 3847 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

直线与直线相交 直线与线段相交 线段与线段相交

int sgn(double x) { if(fabs(x) < eps) return 0; return x < 0 ? -1:1; } struct Point { double x,y; Point() {} Point(double _x,double _y) { x = _x,y = _y; } Point operator -(const Point &b)const { return Point(x - b.x,y - b.y); } //叉积 double opera

求直线与线段的交点

1,求点到直线的带符号距离: float getSignedDistance(点P,直线AB) //求点P到直线AB的带符号距离(当P在AB左侧时距离为正,右侧时为负) { dir=直线AB的方向向量 根据dir求出直线AB的左手法线向量leftNormal = (-dir.y,dir.x).normalized 线段AP在leftNormal上的投影即为P到直线AB的带符号距离: signedDistance=dot(AP,leftNormal); return signedDistance;

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.

POJ 1039 直线和线段相交

题意: 题意很好理解,从左边射过来的光线,最远能经过管道到右边多少距离. 分析: 光线一定经过一个上端点和一个下端点,这一点很容易想到.然后枚举上下端点即可 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define eps 1e-8 #define INF 1e9 #define OK sgn(tmp

hdu 3304(直线与线段相交)

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12042   Accepted: 3808 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

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(判断直线与线段是否相交)

http://poj.org/problem?id=3304 等价于是否存在一条直线穿过所有线段 判断直线与线段是否相交: 首先用两点p1,p2确定了一条直线 在用p1,p2分别与计算线段两个端点计算叉乘即可 ,叉乘之积>0就说明线段两端点在直线的同侧,也就是直线不经过此线段 注意顺序不要搞反了 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #incl

POJ 3304 Segments[直线与线段相交]

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments