poj1269 直线求交点
来源:互联网 发布:淘宝里的天猫店 编辑:程序博客网 时间:2024/06/06 08:43
Intersecting Lines
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11708 Accepted: 5265
Description
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
Input
The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).
Output
There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".
Sample Input
50 0 4 4 0 4 4 05 0 7 6 1 0 2 35 0 7 6 3 -6 4 -32 0 2 27 1 5 18 50 3 4 0 1 2 2 5
Sample Output
INTERSECTING LINES OUTPUTPOINT 2.00 2.00NONELINEPOINT 2.00 5.00POINT 1.07 2.20END OF OUTPUT
求交点参考大白书上面的方法,判平行用叉积,判重合加一条辅助线再用一次叉积,例如直线AB和CD,在平行的基础上,只要A在CD上就是重合,也就是AC和CD平行。
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#define type doubleusing namespace std;const double eps=1e-8;int sgn(double x){ if(fabs(x)<eps) return 0; if(x<0) return -1; return 1;}struct vec{ //向量 type x,y; vec(type xx=0,type yy=0):x(xx),y(yy){} bool operator==(vec a){ return !sgn(x-a.x)&&!sgn(y-a.y); } vec operator*(type k){ return vec(k*x,k*y); }};struct point{ //点 type x,y; point(type xx=0,type yy=0):x(xx),y(yy){} vec operator-(point a){ return vec(x-a.x,y-a.y); } point operator+(vec a){ return point(x+a.x,y+a.y); } bool operator==(point a){ return !sgn(x-a.x)&&!sgn(y-a.y); }};type crossp(vec a,vec b){ //叉积a×b return a.x*b.y-b.x*a.y;}//直线ab和直线cd求交点//调用前保证ab和cd有交点,即sgn(crossp(v,w))!=0point line_ins(point a,point b,point c,point d){ vec u=a-c,v=b-a,w=d-c; double t=crossp(w,u)/crossp(v,w); return a+v*t;}int main(){ int n; point a,b,c,d; cin>>n; puts("INTERSECTING LINES OUTPUT"); for(int i=0;i<n;i++){ cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y; if(sgn(crossp(b-a,d-c))){ point ans=line_ins(a,b,c,d); printf("POINT %.2f %.2f\n",ans.x,ans.y); } else if(sgn(crossp(a-c,d-c))){ //ca和cd是否平行,也就是否是同一直线 puts("NONE"); } else puts("LINE"); } puts("END OF OUTPUT");return 0;}
0 0
- poj1269 直线求交点
- poj1269直线交点
- POJ1269-Intersecting Lines(求两直线交点)
- poj1269——Intersecting Lines//直线交点
- poj1269求两条直线的关系(相交(求交点)---叉乘),平行,共线)
- POJ1269---Intersecting Lines (计算几何基础:直线相交求交点)
- poj1269-Intersecting Lines(求线段交点)
- 求直线交点
- 求直线交点 叉积
- 求直线交点 叉积
- 两直线求交点
- 俩直线求交点
- 求两直线交点
- 求两直线交点
- 2直线求交点
- 求直线交点
- 求两直线交点
- 求直线交点 叉积
- 还是畅通工程
- 文档类网站开发等项目
- 支付
- shell十三问之4:""(双引号)与''(单引号)差在哪?
- Swift调用OC之NSArray
- poj1269 直线求交点
- 自制OS 第7步
- 欢迎使用CSDN-markdown编辑器
- 23种设计模式(15):迭代器模式
- Android measure 简单理解
- iOS开发之KVC、KVO
- V8引擎之Binding
- LeetCode--Next Permutation
- myplan