POJ 1269 Intersecting Lines (两直线之间的位置关系)

题意:判断两直线之间的位置关系,平行,重合,相交

题解:注意精度

#include <cmath>
#include <iostream>
using namespace std;

#define MAX 100
#define eps 1e-8
#define zero(x) ( ((x) > 0 ? (x) : -(x)) < eps )

struct Point { double x, y; };
struct Line { Point a, b; };
struct Result { int flag; Point a; }; 
Result res[MAX];

int parallel ( Line u, Line v )  // 判断是否平行
{
	return zero ( (u.a.x-u.b.x)*(v.a.y-v.b.y) - (v.a.x-v.b.x)*(u.a.y-u.b.y) );
}

int top_on ( Line u, Line v ) // 判断是否重合,但前提是先判定平行
{
	return zero ( (u.a.x-v.b.x)*(v.a.y-v.b.y) - (u.a.y-v.b.y)*(v.a.x-v.b.x) );
}

Point intersection ( Line u, Line v )  // 求交点
{
	Point ret = u.a;
	double t = ( (u.a.x-v.a.x)*(v.a.y-v.b.y) - (u.a.y-v.a.y)*(v.a.x-v.b.x) )
		     / ( (u.a.x-u.b.x)*(v.a.y-v.b.y) - (u.a.y-u.b.y)*(v.a.x-v.b.x) ) ;
	ret.x += (u.b.x-u.a.x) * t;
	ret.y += (u.b.y-u.a.y) * t;
	return ret;
}


int main()
{
	int n, i;
	scanf("%d",&n);
	for ( i = 1; i <= n; i++ )
	{
		Line u, v;
		scanf("%lf %lf %lf %lf", &u.a.x, &u.a.y, &u.b.x, &u.b.y );
		scanf("%lf %lf %lf %lf", &v.a.x, &v.a.y, &v.b.x, &v.b.y );
		if ( parallel ( u, v ) )
		{
			if ( top_on ( u, v ) )
				res[i].flag = 2;
			else
				res[i].flag = 1;
		}
		else
		{
			res[i].flag = 3;
			res[i].a = intersection ( u, v );
		}
	}

	printf("INTERSECTING LINES OUTPUT\n");
	for ( i = 1; i <= n; i++ )
	{
		if ( res[i].flag == 1 )
			printf("NONE\n");
		else if ( res[i].flag == 2 )
			printf("LINE\n");
		else
			printf("POINT %.2lf %.2lf\n", res[i].a.x, res[i].a.y);
	}
	printf("END OF OUTPUT\n");
	return 0;
}

			
	


 

你可能感兴趣的:(struct,output,parallel)