判断两条直线是否相交

时间:2021-11-05 21:26:23

给出两条直线的起始坐标和终点坐标

判断两条直线是否相交;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct node
{
double x,y;
} st1,ed1,st2,ed2;
double get_area(node a0,node a1,node a2)
{
double s=a0.x*a1.y+a2.x*a0.y+a1.x*a2.y-a2.x*a1.y-a0.x*a2.y-a1.x*a0.y;
return s;
}
int fun()
{
double s1=get_area(st1,ed1,st2);
double s2=get_area(st1,ed1,ed2);
double s3=get_area(st2,ed2,st1);
double s4=get_area(st2,ed2,ed1);
if(s1*s2<=0&&s3*s4<=0)
printf("intersect\n");
else
printf("not intersect\n");
}
int main()
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&st1.x,&st1.y,&ed1.x,&ed1.y,&st2.x,&ed2.y,&ed2.x,&ed2.y);
fun();
}