在unity3d中实现求一个三角形的外接圆

时间:2022-08-18 10:26:00

假设三角形由三个点A(x0,y0,z0)B(x1,y1,z1),C(x2,y2,z2)组成,如图所示。已知一个三角形的外接圆圆心是其两条边的中垂线的交点,则在三维空间中,圆心在过其两条边的中点并垂直直线平面ABC的相交线的中线上(则得式1,2),并且三角形的外接圆的圆心一定在平面ABC上(得式3)。

在unity3d中实现求一个三角形的外接圆

(x1-x0)(x-(x1+x0)/2)+(y1-y0)(y-(y1+y0)/2)+(z1-z0)(z-(z1+z0)/2)=0      式1

(x2-x0)(x-(x2+x0)/2)+(y2-y0)(y-(y2-y0)/2)+(z2-z0)(z-(z2+z0/2))=0      式2

a=(x1-x0,y1-y0,z1-z0)

b=(x2-x0,y2-y0,z2-z0)

n=aXb        式3 平面法向量

下面是在纸上的演算

在unity3d中实现求一个三角形的外接圆

求取圆心代码如下:

    public static Vector3 GetCenter(Vector3 Point0,Vector3 Point1,Vector3 Point2)
{
//用于表达过点0,1中垂线的平面
float x10 = Point1.x - Point0.x;
float xx10 = Point1.x + Point0.x;
float y10 = Point1.y - Point0.y;
float yy10 = Point1.y + Point0.y;
float z10 = Point1.z - Point0.z;
float zz10 = Point1.z + Point0.z;
//用于表达过点0,2中垂线的平面
float x20 = Point2.x - Point0.x;
float xx20 = Point2.x + Point0.x;
float y20 = Point2.y - Point0.y;
float yy20 = Point2.y + Point0.y;
float z20 = Point2.z - Point0.z;
float zz20 = Point2.z + Point0.z;
//平面的法向量
Vector3 a = Point1-Point0;
Vector3 b = Point2 - Point0;
Vector3 n = Vector3.Cross(a,b);

float t1 = (x10 * xx10 + y10 * yy10 + z10 * zz10) / 2;
float t2 = (x20 * xx20 + y20 * yy20 + z20 * zz20) / 2;
float t3 = n.x * Point0.x + n.y * Point0.y + n.z * Point0.z;

float D = countDeterminant(
x10,y10,z10,
x20,y20,z20,
n.x,n.y,n.z);
float D1 = countDeterminant(
t1,y10,z10,
t2,y20,z20,
t3,n.y,n.z);
float D2 = countDeterminant(
x10,t1,z10,
x20,t2,z20,
n.x,t3,n.z);
float D3 = countDeterminant(
x10,y10,t1,
x20,y20,t2,
n.x,n.y,t3);

float Circlex = D1 / D;
float Circley = D2 / D;
float Circlez = D3 / D;

return new Vector3(Circlex,Circley,Circlez);
}
private static float countDeterminant(float a11,float a12,float a13,
float a21,float a22,float a23,
float a31,float a32,float a33)
{
return (a11 * a22 * a33 + a21 * a32 * a13 +
a31 * a12 * a23 - a13 * a22 * a31 -
a23 * a32 * a11 - a33 * a12 * a21);
}