• POJ1269(直线之间的关系)


    传送门

    很经典的基础题吧

    判断直线之间的关系

    有两种情况

    平行(包括重合) 和 相交

    那么我们首先考虑平行

    是不是斜率相等就可以了?

    也就是说ΔxΔy==ΔxΔyfrac{Δx}{Δy}==frac{Δx'}{Δy'}

    但是注意到如果给出的线段与y轴平行,也就是Δy=0Δy=0 的时候会RE

    所以把式子转换一下 变成ΔxΔy=ΔxΔyΔx*Δy'=Δx'*Δy 就完美解决了 这样就不同专门特判了

    那么如果两线段又重合呢?

    那么也就是说四个点中任意两个向量的叉积就都为0了

    那么如果不为0也就是不重合了

    那么判一下叉积是否为0就是了

    那么再来考虑求交点

    对于两条直线y=ax+by=ax+by=ax+by=a'x+b'

    我们可以直接得出x=bbaax=frac{b'-b}{a-a'} , y=abb+ababaay=frac{a*b'-b+a*b-a'*b}{a-a'}

    当然也可以直接叉积判断

    看代码吧

    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    #include<stdio.h>
    #include<iostream>
    #include<cmath>
    #include<math.h>
    using namespace std;
    #define eps 1e-5
    inline int read(){
    	char ch=getchar();
    	int res=0,f=1;
    	while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    	while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
    	return res*f;
    }
    struct point{
    	int x,y;
    	point(){}
    	point (int a,int b):
    		x(a),y(b){};
    	friend inline point operator -(const point &a,const point &b){
    		return point(a.x-b.x,a.y-b.y);
    	}
    	friend inline int operator *(const point &a,const point &b){
    		return a.x*b.y-a.y*b.x;
    	}
    }a1,a2,a3,a4;
    double ax,bx,cx,dx,ay,by,cy,dy;
    int n;
    inline void check(){
    	if((point(cx-ax,cy-ay)*point(bx-ax,by-ay))==0){
    		cout<<"LINE"<<'
    ';
    	}
    	else cout<<"NONE"<<'
    ';
    }
    inline void solve(){
    	double s1=(point(dx-ax,dy-ay))*(point(cx-ax,cy-ay));
    	double s2=(point(cx-bx,cy-by))*(point(dx-bx,dy-by));
    	double c=(bx-ax)*(double)(s1/(s1+s2));
    	double d=(by-ay)*(double)(s1/(s1+s2));
    	c+=ax;
    	d+=ay;
    	printf("POINT %.2lf %.2lf
    ",c,d);
    }
    int main(){
    //	freopen("223.cpp","r",stdin);
    	n=read();
    	cout<<"INTERSECTING LINES OUTPUT"<<'
    ';
    	for(int i=1;i<=n;i++){
    		ax=read(),ay=read(),bx=read(),by=read(),cx=read(),cy=read(),dx=read(),dy=read();	       
    		if(fabs((double)((double)(bx-ax)*(double)(dy-cy))-(double)((double)(dx-cx)*(double)(by-ay)))<=eps) check();
    		else solve();
    	}
    	cout<<"END OF OUTPUT"<<'
    ';
    	return 0;
    }
    
  • 相关阅读:
    使用mongoose--写接口
    数据结构上机实验(2)
    时间复杂度十道练习题目
    Python网络爬虫实战入门
    区分矩阵的三大关系
    用python检查矩阵的计算
    ab矩阵(实对称矩阵)
    Python大数据应用
    数据结构上机实验(1)
    0038. Count and Say (E)
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/10366470.html
Copyright © 2020-2023  润新知