UVA_378_Intersecting Lines

<pre name="code" class="cpp">#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
class Point
{
public:
	double x, y;
	Point() {}
	Point(const double &X,const double &Y)
	{
		x = X;
		y = Y;
	}
};
class Line
{
public:
	double a, b, c;//ax+by=c
	double x_min, x_max, y_min, y_max;//直线的值域
	Line(const Point &first,const Point &second)
	{
		x_min = std::min(first.x, second.x);
		x_max = std::max(first.x, second.x);
		y_min = std::min(first.y, second.y);
		y_max = std::max(first.y, second.y);
		if (first.x != second.x)//斜率式可行
		{
			b = 1;
			a = -(first.y - second.y) / (first.x - second.x);
			c = first.y + a*first.x;
		}
		else//k->无穷
		{
			b = 0;
			a = 1;
			c = first.x;
		}
	}
	bool lineParallel(const Line&line)//判断两直线是否平行
	{
		if (a*line.b - b*line.a==0)
		{
			return true;
		}
		return false;
	}
	bool lineOverlap(const Line&line)
	{
		auto D = a*line.b - line.a*b;
		auto Dx = c*line.b - line.c*b;
		auto Dy = a*line.c - line.a*c;
		if (!D&&!Dx&&!Dy)
		{
			return true;
		}
		return false;
	}
	Point getIntersection(const Line&line)//行列式求两直线交点
	{
		auto D = a*line.b - line.a*b;
		auto Dx = c*line.b - line.c*b;
		auto Dy= a*line.c - line.a*c;
		return{ Dx / D,Dy / D };
	}
	bool segmentIntersected(const Line &line)
	{
		auto point = getIntersection(line);
		if (point.x >= x_min&&point.x <= x_max
			&&point.y >= y_min&&point.y <= y_max
			&&point.x >= line.x_min&&point.x <= line.x_max
			&&point.y >= line.y_min&&point.y <= line.y_max
			)//交点在两线段的值域内
		{
			return true;
		}
		return false;
	}
};
class Rectangle
{
public:
	int left, right, top, bottom;
	bool inRectangle(const Point &point)
	{
		if (point.x >= left&&point.x <= right&&point.y >= bottom&&point.y <= top)
		{
			return true;
		}
		return false;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cout << "INTERSECTING LINES OUTPUT" << endl;
	int n;
	while (cin >> n)
	{
		while (n--)
		{
			double x1, y1, x2, y2, x3, y3, x4, y4;
			cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
			Line line1({ x1,y1 }, { x2,y2 }), line2({ x3,y3 }, { x4,y4 });
			if (line1.lineParallel(line2))
			{
				if (line1.lineOverlap(line2))
				{
					cout << "LINE" << endl;
				}
				else
				{
					cout << "NONE" << endl;
				}
			}
			else
			{
				auto point = line1.getIntersection(line2);
				cout << "POINT ";
				printf("%.2lf %.2lf",point.x,point.y);
				cout << endl;
			}
		}
	}
	cout << "END OF OUTPUT" << endl;
	return 0;
}

时间: 2024-10-18 09:09:25

UVA_378_Intersecting Lines的相关文章

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废

lines(最大区间和)

lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1350    Accepted Submission(s): 558 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which

【POJ 3668】Game of Lines

Game of Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6555   Accepted: 2439 Description Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i ha

HDU5124:lines(线段树+离散化)或(离散化思想)

http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input The first line conta

HDOJ 5031 Lines

枚举角度DFS.... Lines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 479    Accepted Submission(s): 140 Problem Description You play a game with your friend. He draws several lines on the paper wi

[POJ 1269]Intersecting Lines

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16311   Accepted: 7040 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

[Grid Layout] Describe a grid layout using named grid lines

We can use named grid lines to describe our grid layout. Let’s see how to apply this to our grid-template-columns and grid-template-rows, and how to refer to these from our grid items. Syntax like: grid-template-columns: [left-sidebar-start] 1fr [mai

rviz学习笔记(二)——Markers: Points and Lines (C++) 点和线

一.在using_marker/src中编写点和线代码 vim ~/catkin_ws/src/using_marker/src/points_and_lines.cpp 编写代码,其中有注释 #include <ros/ros.h> #include <visualization_msgs/Marker.h> #include <cmath> int main( int argc, char** argv ) { //创建一个发布器 ros::init(argc, a

POJ1269 Intersecting Lines[线段相交 交点]

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15145   Accepted: 6640 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