一些基础计算模板
#include <bits/stdc++.h>
using namespace std;
const double eps =1e-8;
typedef long long ll;
//#define double long double
int n,m;
//判断x跟0的关系: -1负数、0等于零、1正数
int sgn(double x)
{
if(fabs(x)<eps)return 0;
else return x<0?-1:1;
}
//点
struct Point{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
Point operator + (Point B){
return Point(x+B.x,y+B.y);
}
Point operator - (Point B){
return Point(x-B.x,y-B.y);
}
bool operator == (Point B){
return sgn(x-B.x)==0&&sgn(y-B.y)==0;
}
Point operator *(double k){
return Point(x*k,y*k);
}
Point operator /(double k){
return Point(x/k,y/k);
}
}t[10005],s;
typedef Point Vector;//向量
//点积,>0锐角,=0直角
double Dot(Vector A, Vector B)
{
return A.x*B.x+A.y*B.y;
}
//叉积,AxB>0,B在A的逆时针方向
double Cross(Vector A, Vector B)
{
return A.x*B.y-A.y*B.x;
}
//两点距离
double Distance(Point A, Point B) {
return sqrt((A.x-B.x) * (A.x-B.x) + (A.y-B.y) * (A.y-B.y));
}
//直线
struct Line
{
Point p1,p2;
Line(){}
Line(Point p1, Point p2):p1(p1),p2(p2){}
};
//线段
typedef Line Segment;
//3点和直线的位置关系
int Point_line_relation(Point p, Line v)
{
int c=sgn(Cross(p-v.p1,v.p2-v.p1));
if(c<0)return 1;//p在v左侧
if(c>0)return 2;//p在v右侧
return 0;//p在v上
}
//4点和线段的位置关系
bool Point_on_seg(Point p,Line v)
{ //0p不在v上,1p在线段v上
return sgn(Cross(p-v.p1,v.p2-v.p1))==0&&sgn(Dot(p-v.p1,p-v.p2))<=0;
}
//5点到直线的距离
double Dis_point_line(Point p, Line v)
{
return fabs(Cross(p-v.p1,v.p2-v.p1))/Distance(v.p1,v.p2);
}
//求向量A的长度
double Len2(Vector A)
{
return Dot(A,A);
}
//6点在直线上的投影
Point Point_line_proj(Point p,Line v)
{
double k=Dot(v.p2-v.p1,p-v.p1)/Len2(v.p2-v.p1);
return v.p1+(v.p2-v.p1)*k;
}
//7点关于直线的对称点
Point Point_line_symmetry(Point p,Line v)
{
Point q=Point_line_proj(p,v);
return Point(2*q.x-p.x,2*q.y-p.y);
}
//8点到线段的距离
double Dis_point_seg(Point p, Segment v)
{
if(sgn(Dot(p-v.p1,v.p2-v.p1))<0||sgn(Dot(p-v.p2,v.p1-v.p2))<0)
{
return min(Distance(p,v.p1),Distance(p,v.p2));
}
return Dis_point_line(p,v);
}
//9两条直线的关系
int Line_relation(Line v1, Line v2)
{
if(sgn(Cross(v1.p2-v1.p1,v2.p2-v2.p1))==0)
{
if(Point_line_relation(v1.p1,v2)==0)return 1;//重合
else return 0;//平行
}
return 2;//相交
}
//10两条直线的交点
Point Cross_Point(Point a,Point b,Point c,Point d)
{
double s1=Cross(b-a,c-a);
double s2=Cross(b-a,d-a);//叉集有正负
return Point(c.x*s2-d.x*s1,c.y*s2-d.y*s1)/(s2-s1);
}
//11两条线段是否相交
bool Cross_segment(Point a,Point b,Point c,Point d)
{
double c1=Cross(b-a,c-a),c2=Cross(b-a,d-a);
double d1=Cross(d-c,a-c),d2=Cross(d-c,b-c);
return sgn(c1)*sgn(c2)<0&&sgn(d1)*sgn(d2)<0;//1相交,0不交
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}