首页 > 其他 > 详细

计算几何三角形模板(持续更新)

时间:2021-06-09 21:45:19      阅读:25      评论:0      收藏:0      [点我收藏+]

三角形模板

const int N = 1e5 + 50;
const ld PI = acos(-1.0);
const ld eps=1e-8;

int sgn(ld x) {
	if(fabs(x)<eps)return 0;
	return x<0?-1:1;
}

struct Point {
	ld x,y;
	Point() {}
	Point(ld x,ld 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);
	}
	Point operator *(double k) {
		return Point(x*k,y*k);
	}
	Point operator /(double k) {
		return Point(x/k,y/k);
	}
	bool operator ==(Point B) {
		return sgn(x-B.x)==0&&sgn(y-B.y)==0;
	}
};

struct Line {
	Point p1,p2;
	Line() {}
	Line(Point p1,Point p2):p1(p1),p2(p2) {}

};
typedef Point Vector;

/*************三角形的四心***********/
//外心,中垂线交点
Point triangle_waixin(Point a,Point b,Point c) {
	Point s;
	ld a1=2*(b.x-a.x),b1=2*(b.y-a.y),a2=2*(c.x-a.x),b2=2*(c.y-a.y);
	ld c1=b.x*b.x-a.x*a.x+b.y*b.y-a.y*a.y;
	ld c2=c.x*c.x-a.x*a.x+c.y*c.y-a.y*a.y;
	s.x=(b1*c2-b2*c1)/(a2*b1-a1*b2);
	s.y=(a2*c1-a1*c2)/(a2*b1-a1*b2);
	return s;
}
//内心,角平分线交点
ld Dis(Point a,Point b) {
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point triangle_neixin(Point a,Point b,Point c) {
	ld A = Dis(b,c),B=Dis(a,c),C=Dis(a,b),S = A+B+C;
	ld x = (A*a.x+B*b.x+C*c.x)/S;
	ld y = (A*a.y+B*b.y+C*c.y)/S;
	return Point(x,y);
}
//重心
Point triangle_zhongxin(Point a,Point b,Point c) {
	return Point((a.x+b.x+c.x)/3.0,(a.y+b.y+c.y)/3.0);
}

//垂心
Point triangle_chuixinchuixin(Point a,Point b,Point c) {
	return triangle_zhongxin(a,b,c)*3.0-triangle_waixin(a,b,c)*2.0;
}
/*******************************************************/
//海伦公式求面积
ld triangle_hailun(Point a,Point b,Point c) {
	ld A = Dis(b,c),B=Dis(a,c),C=Dis(a,b),p=(A+B+C)/2.0;
	return sqrt(p*(p-A)*(p-B)*(p-C));
}

计算几何三角形模板(持续更新)

原文:https://www.cnblogs.com/LaiYiC/p/14868443.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!