POJ 1269 Intersecting Lines

来源:互联网 发布:网络代维管理系统下载 编辑:程序博客网 时间:2024/06/06 02:12

题目大意:

        众所周知直线有三种相互关系,一种是重合,一种是平行,另一种是相交。

        现有多个测例(测例数题中给出),每个测例中给定四点的坐标P1(x1, y2)、P2(x2, y2)、P3、P4(分别确定两条直线,P1P2和P3P4),都是整数,且范围为[-1000, 1000],对于每组测例,判断其相互关系,如果是重合输出"LINE",如果是平行输出"NONE",如果相交于一点则输出"POINT x y",x、y是交点坐标,用实数表示,精确到小数点后两位。

题目链接

注释代码:

/*      * Problem ID : POJ 1269 Intersecting Lines * Author     : Lirx.t.Una      * Language   : C++     * Run Time   : 0 ms      * Run Memory : 240 KB     */ #include <iostream>#include <cstdio>using namespace std;structPoint {//表示整数点intx, y;Point(void) {}Point( int xx, int yy ) : x(xx), y(yy) {}friend istream &//输入,几何计算中类型都包装起来比较方便//因此所有计算几何题都采用类包装的形式而不图一时之“快”operator>>( istream &is, Point &p ) {is >> p.x >> p.y;return is;}Pointoperator-(Point &p) {//求向量p1->p2//p2(x2, y2), p1(x1, y1)//p1->p2 = p2 - p1 = (x2 - x1, y2 - y1)return Point( x - p.x, y - p.y );}intoperator*(Point p) {//求两个向量的叉积//此时两个点都表示起点为原点的向量了//o->p1 = (x1, y1), o->p2 = (x2, y2)//o->p1 × o->p2 = x1 * y2 - x2 * y1//注意!!!:由于题中处理的时候都是求两个向量之交//但是连个向量都是由点相减得来的//相减返回的是临时变量//C++标准不允许对临时变量引用传参,因此这里直接采用值传递//否则会编译报错return x * p.y - y * p.x;}};structRealPoint {//表示实数点doublex, y;RealPoint(void) {}RealPoint( double xx, double yy ) : x(xx), y(yy) {}};structLine {//表示直线//两点Pointp1;Pointp2;//和直线方程的系数(一般式)//由于计算机无法处理斜率很大或者无穷大的直线(溢出)//所以一般都采用一般式处理inta, b, c;Line(void) {}friend istream &//输入operator>>( istream &is, Line &l ) {is >> l.p1 >> l.p2;return is;}booloperator==(Line &oth) {//判断平行//判断直线p1 p2和直线p3 p4是否平行的方法是叉积法//不用一般式也是因为防止出现无穷大等情况//因此叉积法是最简洁最一般的方法//首先判断是否三点共线://p1->p2 × p1->p3 = 0则表示p1、p2、p3三点共线//同理p1->p2 × p1->p4可以判断p1、p2、p4是否三点共线//这样就可以判断四点共线了return !( ( p2 - p1 ) * ( oth.p1 - p1 ) ) &&   !( ( p2 - p1 ) * ( oth.p2 - p1 ) );}booloperator>=(Line &oth) {//判断是否平行//方法是若p1->p2 × p3->p4 = 0//表示平行//注意!!:在判断平行之前一定要现判断是否共线//因为共线也满足这个等式return !( ( p2 - p1 ) * ( oth.p2 - oth.p1 ) );}voidform(void) {//计算直线方程的系数//a = y2 - y1//b = x1 - x2//c = x2 * y1 - x1 * y2a = p2.y - p1.y;b = p1.x - p2.x;c = p2.x * p1.y - p1.x * p2.y;}RealPointoperator*(Line &oth) {//计算交点//之前必须现判断过共线和平行!!!doubled;//先计算方程系数form();oth.form();//分母divisor为a1 * b2 - a2 * b1d = double(a * oth.b - oth.a * b);//两个分子为:b1 * c2 - b2 * c1和a2 * c1 - a1 * c2return RealPoint( double(b * oth.c - oth.b * c) / d,           double(oth.a * c - a * oth.c) / d );}};intmain() {intt;Linel1, l2;scanf("%d", &t);printf("INTERSECTING LINES OUTPUT\n");while ( t-- ) {cin >> l1 >> l2;if ( l1 == l2 ) {puts("LINE");continue;}if ( l1 >= l2 ) {puts("NONE");continue;}RealPointrp;//real pointrp = l1 * l2;printf("POINT %.2lf %.2lf\n", rp.x, rp.y);}puts("END OF OUTPUT");return 0;}

无注释代码:

#include <iostream>#include <cstdio>using namespace std;structPoint {intx, y;Point(void) {}Point( int xx, int yy ) : x(xx), y(yy) {}friend istream &operator>>( istream &is, Point &p ) {is >> p.x >> p.y;return is;}Pointoperator-(Point &p) {return Point( x - p.x, y - p.y );}intoperator*(Point p) {return x * p.y - y * p.x;}};structRealPoint {doublex, y;RealPoint(void) {}RealPoint( double xx, double yy ) : x(xx), y(yy) {}};structLine {Pointp1;Pointp2;inta, b, c;Line(void) {}friend istream &operator>>( istream &is, Line &l ) {is >> l.p1 >> l.p2;return is;}booloperator==(Line &oth) {return !( ( p2 - p1 ) * ( oth.p1 - p1 ) ) &&   !( ( p2 - p1 ) * ( oth.p2 - p1 ) );}booloperator>=(Line &oth) {return !( ( p2 - p1 ) * ( oth.p2 - oth.p1 ) );}voidform(void) {a = p2.y - p1.y;b = p1.x - p2.x;c = p2.x * p1.y - p1.x * p2.y;}RealPointoperator*(Line &oth) {doubled;form();oth.form();d = double(a * oth.b - oth.a * b);return RealPoint( double(b * oth.c - oth.b * c) / d,           double(oth.a * c - a * oth.c) / d );}};intmain() {intt;Linel1, l2;scanf("%d", &t);printf("INTERSECTING LINES OUTPUT\n");while ( t-- ) {cin >> l1 >> l2;if ( l1 == l2 ) {puts("LINE");continue;}if ( l1 >= l2 ) {puts("NONE");continue;}RealPointrp;rp = l1 * l2;printf("POINT %.2lf %.2lf\n", rp.x, rp.y);}puts("END OF OUTPUT");return 0;}

单词解释:

parallel:adj, 平行的; vt, 与...平行; n, 平行线

algebraic:adj, 代数学的

planar:adj, 平面的,二维的


0 0
原创粉丝点击