用java語言實現這個算法,保證任意兩棵樹之間距離相等,現在有想法是設計正四面體,每個點放一課樹,保證任意兩個距離相等
package com.com.example.demo.Test12_15;
import java.math.BigDecimal;
/**
Created by ${SailZhang} on 2016/12/15 0015.
*/
public class Point {
Double x ;
Double y ;
Double z ;
public Point() {
}
public Point(Double x, Double y, Double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Double getX() {
return x;
}
public void setX(Double x) {
this.x = x;
}
public Double getY() {
return y;
}
public void setY(Double y) {
this.y = y;
}
public Double getZ() {
return z;
}
public void setZ(Double z) {
this.z = z;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
", z=" + z +
'}';
}
}
package com.com.example.demo.Test12_15;
import java.math.BigDecimal;
import java.math.MathContext;
/**
* com.com.example.demo.Test12_15
* Created by ${SailZhang} on 2016/12/15 0015.
*/
public class Test {
public static void main(String[] args) {
Point p1 = new Point(0.0,0.0,0.0);
Point p2 = new Point(1.0,1.0,1.0);
for (double i = -2.0;i<=2;i += 0.1){
for (double j = -2.0;j<=2;j += 0.1){
for (double k = -2.0;k<=2;k += 0.1){
Point p3 = new Point(i,j,k);
if (sub(getLengthPow(p1,p2),getLengthPow(p1,p3)) == 0
&& sub(getLengthPow(p1,p2),getLengthPow(p2,p3)) == 0){
System.out.println(p3.toString());
}
}
}
}
}
public static Double getLengthPow(Point p1, Point p2){
double xx = p1.getX() - p2.getX();
double yy = p1.getY() - p2.getY();
double zz = p1.getZ() - p2.getZ();
return Math.pow(xx,2)+ Math.pow(yy,2)+Math.pow(zz,2);
}
public static double sub(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
這個必然存在小數的. 我給自己坑了