码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2021-06-10 17:53:30      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:int()   三角形   type   operator   ble   ==   def   交点   struct   

三角形模板

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));
}

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

标签:int()   三角形   type   operator   ble   ==   def   交点   struct   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!