声明:这个帖不是讨论板题解,代码仅为直线方程的写法参考,这只是题目需求的一小部分。
考虑到斜率在直线 x=c(c 为常数)的情况下会变成 ∞ 或者说 NaN。想到亦可用直线方程形如 ax+by=c 的形式维护,联系方程 x2−x1x−x1=y2−y1y−y1 即可解出 a,b,c 整数解,用 long long 记录即可。写法也不难,这里放的代码还额外维护了一个求交点,利用行列式解一个二元方程组即可。
template<class T=ld>
T det2(T a,T b,T c,T d){return a*c-b*d;}
//Solve the det like
/*
|a b|
|c d|
*/
template<class T=ld>
struct point
{
T x,y;
template<class T1>
point<T> operator = (point<T1> q){return (point){x=q.x,y=q.y};}
};
point<ll> v,w;
template<class T=ld>
struct line
{
T a,b,c; //Represented for linear equation ax+by=c
point<T> p1,p2; //Represented for two end points of the line
point<T> x2p(T x){return (point<T>){x,b?(c-a*x)/b:max(p1.y,p2.y)};}
point<T> y2p(T y){return (point<T>){a?(c-b*y)/a:max(p1.x,p2.x),y};}
template<class T1>
line<T> operator = (line<T1> q){return (line){a=q.a,b=q.b,c=q.c,p1=q.p1,p2=q.p2};}
};
template<class T=ld>
point<T> operator & (line<T> p,line<T> q){ld d=det2(p.a,p.b,q.a,q.b);return (point<T>){det2(p.b,p.c,q.b,q.c)/d,det2(p.a,p.c,q.a,q.c)/d};}
template<class T=ld>
line<T> operator | (point<T> p,point<T> q)
{
if(p.x>q.x) swap(p,q);
return (line<T>){q.y-p.y,p.x-q.x,(q.y-p.y)*p.x+(p.x-q.x)*p.y,p,q};
}