UVA378 Intersecting Lines 题解

UVA378 Intersecting Lines 题解

怎么这么多点斜式邪教啊。

解法

在计算几何中,我们应该尽可能地避免使用浮点数的计算,尽可能地使用向量计算。

本篇题解默认读者具有向量基础。

为了方便讲解,我们将输入的四个点分别记作 A , B , C , D A,B,C,D A,B,C,D。

考虑两条直线 A B , C D AB,CD AB,CD 何时平行。根据向量叉乘的几何意义,如果 A B → × C D → = 0 \overrightarrow{AB} \times \overrightarrow{CD}=0 AB ×CD =0,则两直线平行。

直线重合是在直线平行的基础上,如果 C , D C,D C,D 中任一点在直线 A B AB AB 上,则两直线平行。即 A B → × A C → = 0 \overrightarrow{AB} \times \overrightarrow{AC}=0 AB ×AC =0(这里取点 C C C)。

剩下的情况就是直线相交了。如图,设两直线交点为 E E E。

根据小学四年级(雾)学的燕尾模型, A E E B = S Δ A D C S Δ B D C \dfrac{AE}{EB}=\dfrac{S_{\Delta ADC}}{S_{\Delta BDC}} EBAE=SΔBDCSΔADC,所以 A E A B = S Δ A D C S Δ A D C + S Δ B D C \dfrac{AE}{AB}=\dfrac{S_{\Delta ADC}}{S_{\Delta ADC}+S_{\Delta BDC}} ABAE=SΔADC+SΔBDCSΔADC,三角形面积可以用叉积轻松求出。

所以两直线交点为 ( X A + ( X B − X A ) × A E A B , Y A + ( Y B − Y A ) × A E A B ) (X_A+(X_B-X_A) \times \dfrac{AE}{AB},Y_A+(Y_B-Y_A) \times \dfrac{AE}{AB}) (XA+(XB−XA)×ABAE,YA+(YB−YA)×ABAE)。

最后提一句,这道题是早期 UVA 题,没有自动忽略文末换行,这题需要有文末换行。

代码

cpp 复制代码
#include<bits/stdc++.h>
namespace fast_IO
{
	/**
	 * 省略了一部分
	*/
	inline void read(int &x,char c=Getchar())
	{
		bool f=c!=45;
		x=0;
		while(c<48 or c>57) c=Getchar(),f&=c!=45;
		while(c>=48 and c<=57) x=(x<<3)+(x<<1)+(c^48),c=Getchar();
		x=f?x:-x;
	}
	inline void write(int x)
	{
		if(x<0) Putchar(45),x=-x;
		if(x>=10) write(x/10),x%=10;
		Putchar(x^48);
	}
	inline void read(__int128 &x,char c=Getchar())
	{
		bool f=c!=45;
		x=0;
		while(c<48 or c>57) c=Getchar(),f&=c!=45;
		while(c>=48 and c<=57) x=(x<<3)+(x<<1)+(c^48),c=Getchar();
		x=f?x:-x;
	}
	inline void write(__int128 x)
	{
		if(x<0) Putchar(45),x=-x;
		if(x>=10) write(x/10),x%=10;
		Putchar(x^48);
	}
	inline bool inrange(const char &ch)
	{
		if(ch>=33 && ch<=126) return true;
		return false;
	}
	inline void read(std::string &st,char c=Getchar())
	{
		st.clear();
		while(!inrange(c)) c=Getchar();
		while(inrange(c)) st+=c,c=Getchar();
	}
	inline void write(std::string st)
	{
		for(int i=0;i<st.size();i++) Putchar(st[i]);
	}
	inline void read(char &ch)
	{
		ch=Getchar();
		while(!inrange(ch)) ch=Getchar();
	}
	inline void write(const char &ch)
	{
		Putchar(ch);
	}
	inline void write(double x,int fix=2)
	{
		x+=x>0?my_round[fix+1]:-my_round[fix+1],write((__int128)x),x=x>0?x:-x,x-=(__int128)x;
		if(fix)
		{
			Putchar(46);
			while(fix--) x*=10,Putchar(((int)x)^48),x-=(int)x;
		}
	}
	class fastin
	{
	public:
		template<typename T>
		inline fastin &operator>>(T &x)
		{
			read(x);
			return *this;
		}
	};
	class fastout
	{
	public:
		template<typename T>
		inline fastout &operator<<(T x)
		{
			write(x);
			return *this;
		}
	};
	fastin in;
	fastout out;
};
using namespace fast_IO;
int n;
struct point
{
	int x,y;
	point()
	{
		x=y=0;
	}
	point(int x,int y)
	{
		this->x=x,this->y=y;
	}
	inline point operator-(const point &rhs) const
	{
		return point(x-rhs.x,y-rhs.y);
	}
	inline int operator*(const point &rhs)
	{
		return x*rhs.y-y*rhs.x;
	}
};
inline int sgn(int x)
{
	return x==0?0:(x>0?1:-1);
}
struct seg
{
	point s,t;
};
seg a,b;
inline void calc()
{
	double ix,iy,rat;
	rat=(b.t-a.s)*(b.s-a.s)*1.0/((b.t-a.s)*(b.s-a.s)-(b.t-a.t)*(b.s-a.t));
	ix=a.s.x*1.0+(a.t.x-a.s.x)*rat,iy=a.s.y*1.0+(a.t.y-a.s.y)*rat;
	out<<"POINT "<<ix<<' '<<iy<<'\n';
}
int main()
{
	in>>n,out<<"INTERSECTING LINES OUTPUT\n";
	for(int i=1;i<=n;i++)
	{
		in>>a.s.x>>a.s.y>>a.t.x>>a.t.y>>b.s.x>>b.s.y>>b.t.x>>b.t.y;
		if((a.t-a.s)*(b.t-b.s)==0)
		{
			if((a.t-a.s)*(b.s-a.s)==0) out<<"LINE\n";
			else out<<"NONE\n";
		}else calc();
	}
	out<<"END OF OUTPUT\n";
	fwrite(Ouf,1,p3-Ouf,stdout),fflush(stdout);
	return 0;
}
相关推荐
咸鱼爱学习26 分钟前
【题解】B2613【深基1.习5】打字速度
数据结构·c++·算法
一匹电信狗28 分钟前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
syt_biancheng36 分钟前
C++ 多态(1)
jvm·c++·学习
云青黛1 小时前
轮廓系数(一个异型簇的分类标准)
人工智能·算法·机器学习
云青黛1 小时前
肘部法找k
人工智能·算法·机器学习·聚类
std78792 小时前
用C++ 实现屏幕保护程序
开发语言·c++
CHANG_THE_WORLD2 小时前
if条件语句 三目运算符 汇编分析
汇编·算法·条件语句·if 语句·汇编分析·条件语句汇编分析
tumu_C2 小时前
无用知识研究:在trailing return type利用decltype,comma operator在对函数进行sfinae原创 [二]
开发语言·c++·算法
web安全工具库2 小时前
告别刀耕火种:用 Makefile 自动化 C 语言项目编译
linux·运维·c语言·开发语言·数据库·算法·自动化
红糖生姜2 小时前
P12874 [蓝桥杯 2025 国 Python A] 巡逻||题解||图论
c++·蓝桥杯·图论