Intersecting Lines(计算几何基础)
来源:互联网 发布:淘宝自定义代码 编辑:程序博客网 时间:2024/06/07 22:04
题目来源:[NWPU][2014][TRN][19]二维计算几何基础
http://vjudge.net/vjudge/contest/view.action?cid=54080#problem/D
作者:npufz
题目:
D - Intersecting Lines
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
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
代码:
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;typedef struct point{ double x,y;}poi;int intersection(point be[2],point ed[2]){ double x,y,k1,b1,k2,b2; if((ed[0].x-be[0].x)*(ed[1].y-be[1].y)==(ed[0].y-be[0].y)*(ed[1].x-be[1].x)) { if((ed[0].x-be[0].x)*(be[1].y-be[0].y)==(ed[0].y-be[0].y)*(be[1].x-be[0].x)) printf("LINE\n"); else printf("NONE\n"); } else { /*if() f2=ed[0].x*ed[1].x*(be[1].y-be[0].y)+ ed[0].x*be[1].x*(be[0].y-ed[1].y)+ be[0].x*ed[1].x*(ed[0].y-be[1].y)+ be[0].x*be[1].x*(ed[1].y-ed[0].y); f1=(be[1].y*ed[0].x+be[0].x*ed[1].y- ed[0].y*be[1].x-be[0].y*ed[1].x); x=f2/f1; y=*/ if(be[0].x==ed[0].x) { x=be[0].x; y=be[1].y+(ed[1].y-be[1].y)*(x-be[1].x)/(ed[1].x-be[1].x); } else if(be[1].x==ed[1].x) { x=be[1].x; y=be[0].y+(ed[0].y-be[0].y)*(x-be[0].x)/(ed[0].x-be[0].x); } else { k1=(ed[0].y-be[0].y)/(ed[0].x-be[0].x); b1=be[0].y-k1*be[0].x; k2=(ed[1].y-be[1].y)/(ed[1].x-be[1].x); b2=be[1].y-k2*be[1].x; x=(b2-b1)/(k1-k2); y=k1*x+b1; } printf("POINT %.2lf %.2lf\n",x,y); }return 0;}int main(){ int i,t; poi be[2],ed[2]; scanf("%d",&t); for(i=1;i<=t;i++) { if(i==1) printf("INTERSECTING LINES OUTPUT\n"); scanf("%lf%lf%lf%lf",&be[0].x,&be[0].y,&ed[0].x,&ed[0].y); scanf("%lf%lf%lf%lf",&be[1].x,&be[1].y,&ed[1].x,&ed[1].y); intersection(be,ed); } printf("END OF OUTPUT\n"); return 0;}反思:一开始直接进行了解析求解,发现可能有分母为零的情况,就放弃了
0 0
- Intersecting Lines(计算几何基础)
- Intersecting Lines (计算几何)
- POJ 1269 Intersecting Lines 计算几何基础
- poj 1269 Intersecting Lines(计算几何)
- POJ 1269 Intersecting Lines(计算几何)
- poj 1269 Intersecting Lines(计算几何)
- POJ 1269 Intersecting Lines(计算几何)
- [POJ1269]Intersecting Lines(计算几何)
- 1269---Intersecting Lines---计算几何
- 【计算几何】 poj1269 Intersecting Lines
- POJ1269 Intersecting Lines 计算几何
- POJ1269 Intersecting Lines(计算几何)
- POJ1269---Intersecting Lines (计算几何基础:直线相交求交点)
- poj 1269 Intersecting Lines(计算几何:线段相交)
- POJ 1269 Intersecting Lines --计算几何
- POJ 1269 Intersecting Lines(计算几何)
- poj 1269 Intersecting Lines(计算几何)
- poj 1269 Intersecting Lines 计算几何
- 工厂三兄弟之抽象工厂模式
- java + jni + mingw实例开发(基于命令行窗口模式)
- c++ 学习笔记(高级linux编程) day4
- AndroidManifest.xml 主配置清单文件 各句解释
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- Intersecting Lines(计算几何基础)
- c++ 学习笔记(高级linux编程) day5
- ios7点击statusBar返回列表顶部
- c++ 学习笔记(高级linux编程) day6
- LA 6657 GCD XOR
- 【HDU】4965 Fast Matrix Calculation 矩阵快速幂
- UI设计师需要记住的那些东西——iOS规范篇
- Android View绘制流程
- HDU 4961(杭电多校#9 1002题)Boring Sum(瞎搞)