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.
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
5
0 0 4 4 0 4 4 0
5 0 7 6 1 0 2 3
5 0 7 6 3 -6 4 -3
2 0 2 27 1 5 18 5
0 3 4 0 1 2 2 5
Sample Output
INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT
题意: 由四个点确定两条线, (x1, y1) (x2, y2) 确定一条, (x3, y3) (x4, y4) 确定另一条
然后分类讨论吧, 贴出的代码有分类的注释~
注意点:
求斜率时, 注意分母为零的情况, 即直线垂直x轴时!
AC代码:
#include<stdio.h>
int main() {
int N;
scanf("%d", &N);
printf("INTERSECTING LINES OUTPUT\n");
while(N--) {
double x1, y1, x2, y2, x3, y3, x4, y4;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
scanf("%lf %lf %lf %lf", &x3, &y3, &x4, &y4);
double k1, k2, kt;
double c1, c2;
double tx, ty;
//两直线斜率都存在(即没有直线垂直x轴)的情形
if(x2-x1 != 0 && x4-x3 != 0) {
k1 = (y2-y1) / (x2-x1);
k2 = (y4-y3) / (x4-x3);
if(x1-x3 != 0)
kt = (y1-y3) / (x1-x3);
else if(x1-x4 != 0)
kt = (y1-y4) / (x1-x4);
else if(x2-x3 != 0)
kt = (y2-y3) / (x2-x3);
else
kt = (y2-y4) / (x2-x4);
//若两直线斜率相同, 可能平行, 可能重合
if(k1 == k2) {
if(kt == k1) //重合
printf("LINE\n");
else //平行
printf("NONE\n");
}
//不然就是斜率不同, 要相交
else {
c1 = y2 - k1 * x2;
c2 = y4 - k2 * x4;
tx = (c2-c1) / (k1-k2);
ty = k1 * tx + c1;
printf("POINT %.2lf %.2lf\n", tx, ty);
}
}
else {
//两直线都垂直于x轴的情形
if(x2-x1 == 0 && x4-x3 == 0) {
if(x2 == x3) //重合
printf("LINE\n");
else //平行
printf("NONE\n");
}
//第一条不垂直而第二条垂直于x轴的情形(必相交)
else if(x2-x1 != 0 && x4-x3 == 0) {
k1 = (y2-y1) / (x2-x1);
c1 = y2 - k1 * x2;
tx = x4;
ty = k1 * tx + c1;
printf("POINT %.2lf %.2lf\n", tx, ty);
}
//第一条垂直而第二条不垂直与x轴的情形(必相交)
else if(x4-x3 != 0 && x2-x1 == 0) {
k2 = (y4-y3) / (x4-x3);
c2 = y4 - k2 * x4;
tx = x2;
ty = k2 * tx + c2;
printf("POINT %.2lf %.2lf\n", tx, ty);
}
}
}
printf("END OF OUTPUT\n");
return 0;
}
分享到:
相关推荐
poj.grids.cn题型汇总 Dp状态设计与方程总结 1.不完全状态记录 青蛙过河问题 利用区间dp 2.背包类问题 <1> 0-1背包,经典问题 无限背包,经典问题 判定性背包问题 带附属关系的背包问题 <5> + -1背包问题 ...
相信大家在做poj上的题目的时候如果没有分类的话很迷茫吧....这里有一份目前比较全面的poj题目分类..
poj 2442 Sequence.md
poj 3579 Median.md
poj 2362 Square.md
poj 1077 Eight.md
poj 2918 Tudoku.md
poj 3071 Football.md
poj 3484 Showstopper.md
poj 3050 Hopscotch.md
poj 1523 SPF.md
poj 2323 PERMS.md
poj 3532 Resistance.md
poj 2447 RSA.md
poj 3356 AGTC.md
poj 3914 DuLL.md
poj 2672 Hotkeys.md
poj 1780 Code.md
poj 1149 PIGS.md
poj 1628 Deduction.md