poj 1269 Intersecting Lines

这是很基础的计算几何的题目。

两直线判交,有交点就输出交点,没有交点共线或重合则输出相应的结果。

题目链接:http://poj.org/problem?id=1269

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;
struct Point//
{
      double x, y;
      Point(){};
      Point(double x, double y):x(x), y(y){};
}a[105], b[105];
//直线相交,平行返回0 重合返回1 相交返回2,有交点则存在p中
int jiaodian(Point a, Point b, Point c, Point d, Point &p)
{
    double A1=b.y-a.y,A2=d.y-c.y,B1=a.x-b.x,B2=c.x-d.x;
    double C1=b.y*(b.x-a.x)-b.x*(b.y-a.y),C2=d.y*(d.x-c.x)-d.x*(d.y-c.y);
    if(A1*B2==B1*A2)//平行或重合
    {
        if(A2*C1==A1*C2&&B1*C2==B2*C1)
            return 1;//重合
        return 0;//平行
    }
    p.x=(B1*C2-B2*C1)/(B2*A1-B1*A2);
    p.y=(A1*C2-A2*C1)/(A2*B1-A1*B2);
    return 2;//相交
}

int main()
{
      int Case;
      Point a, b, c, d, p;
      scanf("%d",&Case);
      printf("INTERSECTING LINES OUTPUT\n");
      while (Case--)
      {
            scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &d.x, &d.y);
            if (jiaodian(a, b, c, d, p) == 0)
                  printf("NONE\n");
            else if (jiaodian(a, b, c, d, p) == 1)
                  printf("LINE\n");
            else if (jiaodian(a, b, c, d, p) == 2)
                  printf("POINT %.2f %.2f\n", p.x, p.y);
      }
      printf("END OF OUTPUT\n");
      return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值