题意:给两条直线,判断相交,重合或者平行

思路:判断重合可以用叉积,平行用斜率,其他情况即为相交。

求交点:

这里也用到叉积的原理。假设交点为p0(x0,y0)。则有:

(p1-p0)X(p2-p0)=0

(p3-p0)X(p2-p0)=0

展开后即是

(y1-y2)x0+(x2-x1)y0+x1y2-x2y1=0

(y3-y4)x0+(x4-x3)y0+x3y4-x4y3=0

将x0,y0作为变量求解二元一次方程组。

假设有二元一次方程组

a1x+b1y+c1=0;

a2x+b2y+c2=0

那么

x=(b1*c2-b2*c1)/(a1*b2-a2*b1)
y=(a2*c1-a1*c2)/(a1*b2-a2*b1)

#include<stdio.h>
#include<string.h>
#include<math.h>
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 operator *(const point &b)const{//点积
return x*b.x+y*b.y;
}
double operator ^(const point &b)const{//叉积
return x*b.y-y*b.x;
}
}A,B,C,D;
double cal(point p0,point p1,point p2){//小于0表示在p1处左折,大于0右折,等于0同线
return (p1-p0)^(p2-p0);
}
const double eps=1e-;
int main(){
int n,i;
while(scanf("%d",&n)!=EOF){
puts("INTERSECTING LINES OUTPUT");
for(i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y);
scanf("%lf%lf%lf%lf",&C.x,&C.y,&D.x,&D.y);
if(fabs(cal(C,A,B))<eps&&fabs(cal(D,A,B))<eps) puts("LINE");
else if((B.x-A.x)*(D.y-C.y)==(D.x-C.x)*(B.y-A.y)) puts("NONE");
else{
double a1=A.y-B.y;
double b1=B.x-A.x;
double c1=A.x*B.y-B.x*A.y;
double a2=C.y-D.y;
double b2=D.x-C.x;
double c2=C.x*D.y-D.x*C.y;
double x=(b1*c2-b2*c1)/(a1*b2-a2*b1);
double y=(a2*c1-a1*c2)/(a1*b2-a2*b1);
printf("POINT %.2f %.2f\n",x,y);
}
}
puts("END OF OUTPUT");
}
return ;
}

POJ 1269 Intersecting Lines【判断直线相交】的更多相关文章

  1. POJ 1269 Intersecting Lines(直线相交判断,求交点)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8342   Accepted: 378 ...

  2. POJ 1269 Intersecting Lines (判断直线位置关系)

    题目链接:POJ 1269 Problem Description We all know that a pair of distinct points on a plane defines a li ...

  3. POJ 1269 Intersecing Lines (直线相交)

    题目: Description We all know that a pair of distinct points on a plane defines a line and that a pair ...

  4. POJ 1269 Intersecting Lines(线段相交,水题)

    id=1269" rel="nofollow">Intersecting Lines 大意:给你两条直线的坐标,推断两条直线是否共线.平行.相交.若相交.求出交点. ...

  5. poj 1269 Intersecting Lines(判断两直线关系,并求交点坐标)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12421   Accepted: 55 ...

  6. POJ 1269 Intersecting Lines(直线求交点)

    Description We all know that a pair of distinct points on a plane defines a line and that a pair of ...

  7. POJ 1269 Intersecting Lines 判断两直线关系

    用的是初中学的方法 #include <iostream> #include <cstdio> #include <cstring> #include <al ...

  8. POJ 1269 Intersecting Lines(判断两直线位置关系)

    题目传送门:POJ 1269 Intersecting Lines Description We all know that a pair of distinct points on a plane ...

  9. poj 1269 Intersecting Lines(直线相交)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8637   Accepted: 391 ...

随机推荐

  1. SQL数据库基础(八)

    连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join   on 2.union     在关 ...

  2. 【GOF23设计模式】享元模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_享元模式.享元池.内部状态.外部状态.线程池.连接池 package com.test.flyweight; /** * ...

  3. StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance

    http://highscalability.com/blog/2014/7/21/stackoverflow-update-560m-pageviews-a-month-25-servers-and ...

  4. Javascript-回调函数浅谈

    回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直接调用,而是在特定 ...

  5. C语言位运算详解

    位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进制位的问题.C语言提供了6个位操作运算符.这些运算符只能用于整形操作数,即只能用于带符号或无符号的char.short.int与long类型 ...

  6. location对象及history对象

     history对象 location 是最有用的BOM对象之一,它提供了与当前窗口中加载的文档有关的信息,还提供了一些导航功能.事实上,location 对象是很特别的一个对象,因为它既是windo ...

  7. Android主线程不能访问网络异常解决办法

    从两个方面说下这个问题: 1. 不让访问网络的原因 2. 解决该问题的办法 不让访问网络的原因: 由于对于网络状况的不可预见性,很有可能在网络访问的时候造成阻塞,那么这样一来我们的主线程UI线程 就会 ...

  8. IOS 开发一些常用的地址

    1.开发者中心 https://developer.apple.com/membercenter/index.action 2.itunesconnect https://itunesconnect. ...

  9. Java.lang.OutOfMemoryError处理

    此错误对于新手做项目的时候经常会发生,而且不容易处理 默认情况下,每个android程序的dailvik虚拟机的最大堆空间大小为16M 当加载的图片太多或图片过大时经常出现OOM问题 而出现此类问题最 ...

  10. iOSQuartz2D-02-绘制炫酷的下载进度条

    效果图 实现思路 要实现绘图,通常需要自定义一个UIView的子类,重写父类的- (void)drawRect:(CGRect)rect方法,在该方法中实现绘图操作 若想显示下载进度,只需要实例化自定 ...