Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

算法导论上的判断方法(也注意了坐标顺序问题,还是WA)

Posted by yogafrank at 2008-08-15 00:49:03 on Problem 1410
简直疯狂了。。。

#include <iostream>
#include <fstream>
#define in cin
using namespace std;

class Point
{
public:
	int x, y;
};

int direction ( Point a, Point b, Point c )
{
	return ( c.x - a.x ) * ( b.y - a.y ) - ( b.x - a.x ) * ( c.y - a.y );
}

int min ( int a, int b )
{
	return a > b ? b : a;
}

int max ( int a, int b )
{
	return a > b ? a : b;
}

bool onsegment ( Point a, Point b, Point c )
{
	if ( min ( a.x, b.x ) <= c.x && c.x <= max ( a.x, b.x ) && 
		 min ( a.y, b.y ) <= c.y && c.y <= max ( a.y, b.y ) )
		 return true;
	return false;
}

bool segmentsintersect ( Point p1, Point p2, Point p3, Point p4 )
{
	int d1 = direction ( p3, p4, p1 );
	int d2 = direction ( p3, p4, p2 );
	int d3 = direction ( p1, p2, p3 );
	int d4 = direction ( p1, p2, p4 );

	if ( ( ( d1 > 0 && d2 < 0 ) || ( d1 < 0 && d2 > 0 ) ) &&
		 ( ( d3 > 0 && d4 < 0 ) || ( d3 < 0 && d4 > 0 ) ) )
		 return true;
	else if ( d1 == 0 && onsegment ( p3, p4, p1 ) )
		return true;
	else if ( d2 == 0 && onsegment ( p3, p4, p2 ) )
		return true;
	else if ( d3 == 0 && onsegment ( p1, p2, p3 ) )
		return true;
	else if ( d4 == 0 && onsegment ( p1, p2, p4 ) )
		return true;
	else 
		return false;
}

int main ()
{
	Point start, end, leftup, rightbottom;
    int t;
	for ( in >> t; t > 0; t -- )
	{
		in >> start.x >> start.y >> end.x >> end.y >>
		      leftup.x >> leftup.y >> rightbottom.x >> rightbottom.y;

		if ( leftup.x > rightbottom.x )
		{
			int t = leftup.x;
			leftup.x = rightbottom.x;
			rightbottom.x = leftup.x;
		}

		if ( leftup.y < rightbottom.y )
		{
			int t = leftup.y;
			leftup.y = rightbottom.y;
			rightbottom.y = t;
		}

		Point rightup, leftbottom;
		rightup.x = rightbottom.x;
		rightup.y = leftup.y;
        leftbottom.x = leftup.x;
		leftbottom.y = rightbottom.y;

		if ( segmentsintersect ( start, end, leftup, leftbottom ) ||
			 segmentsintersect ( start, end, leftbottom, rightbottom ) ||
			 segmentsintersect ( start, end, rightbottom, rightup ) || 
			 segmentsintersect ( start, end, rightup, leftup ) )
			cout << "T";
		else
			cout << "F";
		cout << endl;
	}

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator