poj-1269 Intersecting Lines(计算几何+求两直线交点)

http://poj.org/problem?id=1269

题意:

给定四个点,构成两条直线,求两直线的交点。

思路:

计算几何+数学,写就好

代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iomanip>
#include<algorithm>
#include<string.h>
#include<queue>
#include<cmath>
#include<stack>

using namespace std;
const int maxn=3e5+10;
const int inf=0x7f7f7f7f;
typedef long long ll;

typedef struct
{
    int x,y;
}Point;

typedef struct 
{
    int a,b,c;
}Line;

int line(int x1,int y1,int x2,int y2){
    return x1*y2-y1*x2;//结果为0时,向量平行
}

Line lineform(int x1,int y1,int x2,int y2)//ax+by+c=0
{
    Line temp;
    temp.a=y2-y1;
    temp.b=x1-x2;
    temp.c=x2*y1-x1*y2;
    return temp;
}
double x,y;
void lineintersect(Line l1,Line l2)
{
    double d=l1.a*l2.b-l2.a*l1.b;
    x=(l2.c*l1.b-l1.c*l2.b)/d;
    y=(l2.a*l1.c-l1.a*l2.c)/d;
}

int main()
{
    int n;
    Point p1,p2,p3,p4;
    Line l1,l2;
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT\n");
    while(n--)
    {
        scanf("%d%d%d%d%d%d%d%d",&p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&p4.x,&p4.y);
        if(line(p2.x-p1.x,p2.y-p1.y,p3.x-p1.x,p3.y-p1.y)==0&&line(p2.x-p1.x,p2.y-p1.y,p4.x-p1.x,p4.y-p1.y)==0)
          printf("LINE\n");
        else if(line(p2.x-p1.x,p2.y-p1.y,p4.x-p3.x,p4.y-p3.y)==0) 
          printf("NONE\n");
        else{
            l1=lineform(p1.x,p1.y,p2.x,p2.y);
            l2=lineform(p3.x,p3.y,p4.x,p4.y);
            lineintersect(l1,l2);
            printf("POINT %.2lf %.2lf\n",x,y);
        }
    }
    printf("END OF OUTPUT\n");
    system("pause");
    return 0;
}

 

posted on 2021-02-01 14:45  mmn  阅读(89)  评论(0编辑  收藏  举报