三維空間中,**正四面體**的 4 個頂點兩兩距離相等。邊長為 a,高為 a * sqrt(2/3)。任取一頂點為原點,可構造座標:
P1 = (0, 0, 0)
P2 = (a, 0, 0)
P3 = (a/2, a*sqrt(3)/2, 0)
P4 = (a/2, a*sqrt(3)/6, a*sqrt(6)/3)
public class Point3D {
double x, y, z;
public Point3D(double x, double y, double z) { this.x=x; this.y=y; this.z=z; }
double dist2(Point3D o) {
double dx=x-o.x, dy=y-o.y, dz=z-o.z;
return dx*dx + dy*dy + dz*dz;
}
@Override public String toString() {
return String.format("(%.3f, %.3f, %.3f)", x, y, z);
}
}
public class Tetrahedron {
public static Point3D[] regularTetrahedron(double edge) {
double h = edge * Math.sqrt(2.0/3.0);
double r = edge / Math.sqrt(3.0); // 底面外接圓半徑
return new Point3D[] {
new Point3D(0, 0, 0),
new Point3D(edge, 0, 0),
new Point3D(edge/2, edge*Math.sqrt(3)/2, 0),
new Point3D(edge/2, edge*Math.sqrt(3)/6, h)
};
}
public static void verify(Point3D[] pts) {
for (int i=0;i<4;i++)
for (int j=i+1;j<4;j++)
System.out.printf("%s <-> %s : %.6f%n", pts[i], pts[j], Math.sqrt(pts[i].dist2(pts[j])));
}
}
若不使用解析解,可在立方體區域內網格搜索滿足距離相等的點組(原代碼用 0.1 步長,-2~2 範圍)。但解析解更精確高效。
(0.000, 0.000, 0.000) <-> (1.000, 0.000, 0.000) : 1.000000
(0.000, 0.000, 0.000) <-> (0.500, 0.866, 0.000) : 1.000000
(0.000, 0.000, 0.000) <-> (0.500, 0.289, 0.816) : 1.000000
(1.000, 0.000, 0.000) <-> (0.500, 0.866, 0.000) : 1.000000
(1.000, 0.000, 0.000) <-> (0.500, 0.289, 0.816) : 1.000000
(0.500, 0.866, 0.000) <-> (0.500, 0.289, 0.816) : 1.000000
