计算几何 三角形四心(内心,外心,重心,垂心)模板

详细见https://blog.csdn.net/islittlehappy/article/details/81533090

准备的变量和函数

struct Point{
	double x,y;
	Point(){
	}
	Point(double xx,double yy){
		x=xx;
		y=yy;
	}
};
Point operator+(Point a,Point b){  //向量加 
	return Point(a.x+b.x,a.y+b.y);  
} 
Point operator-(Point a,Point b){ //向量减
	return Point(a.x-b.x,a.y-b.y);
} 
double sqr(double x){
	return x*x;
}
double dis(Point a,Point b){ //求ab的长度 
	return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}

重心

Point gravity(Point a,Point b,Point c){ // 重心  
	double x=(a.x+b.x+c.x)/3;
	double y=(a.y+b.y+c.y)/3;
	return Point(x,y);
}

内心

Point Incenter(Point a,Point b,Point c){  //三角形内心 
     double A=dis(b,c);
     double B=dis(a,c);
     double C=dis(a,b);
     double S=A+B+C; 
     double x=(A*a.x+B*b.x+C*c.x)/S;
     double y=(A*a.y+B*b.y+C*c.y)/S;
	 return Point(x,y);	
} 

外心

Point Circum(Point a,Point b,Point c){ //三角形外心 
	double x1=a.x,y1=a.y;
	double x2=b.x,y2=b.y;
	double x3=c.x,y3=c.y;
	
	double a1=2*(x2-x1);
	double b1=2*(y2-y1);
	double c1=x2*x2+y2*y2-x1*x1-y1*y1;
	
	double a2=2*(x3-x2);
	double b2=2*(y3-y2);
	double c2=x3*x3+y3*y3-x2*x2-y2*y2;
	
	double x=(c1*b2-c2*b1)/(a1*b2-a2*b1);
	double y=(a1*c2-a2*c1)/(a1*b2-a2*b1);
	
	return Point(x,y);
} 

垂心

Point ortho(Point a,Point b,Point c){  //垂心 
	double A1=b.x-c.x;
	double B1=b.y-c.y;
	double C1=A1*a.y-B1*a.x;
	
	double A2=a.x-c.x;
	double B2=a.y-c.y;
	double C2=A2*b.y-B2*b.x;
	
	double x=(A1*C2-A2*C1)/(A2*B1-A1*B2);
	double y=(B1*C2-B2*C1)/(A2*B1-A1*B2);
	
	return Point(x,y);
}

你可能感兴趣的:(计算几何)