中心坐标,等边三角形

时间:2013-02-22 01:46:03

标签: java implementation

我目前的三角类看起来像:

public class TriangleEquilateral {
    private Point cornerA;
    private Point cornerB;
    private Point cornerC;
    private double x1 = 0;
    private double y1 = 0;
    private double x2 = 10;
    private double y2 = 0;
    private double x3 = 5;
    private double y3 = Math.sqrt(75);

    public TriangleEquilateral(){
        cornerA = new Point(x1,y1);
        cornerB = new Point(x2,y2);
        cornerC = new Point(x3,y3);
    }

    public TriangleEquilateral(double X1,double Y1,double X2,double Y2,double X3,double Y3){
        x1 = X1;
        y1 = Y1;
        x2 = X2;
        y2 = Y2;
        x3 = X3;
        y3 = Y3;

        cornerA = new Point(X1,Y1);
        cornerB = new Point(X2,Y2);
        cornerC = new Point(X3,Y3);
    }

    public boolean isEquilateral(){
        double lengthAB = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
        double lengthBC = Math.sqrt(Math.pow(x2-x3,2) + Math.pow(y2-y3,2));
        double lengthCA = Math.sqrt(Math.pow(x3-x1,2) + Math.pow(y3-y1,2));

        boolean isEquilateral = false;
        if(lengthAB == lengthBC && lengthBC == lengthCA && lengthCA == lengthAB){
            isEquilateral = true;
        }
        System.out.println(lengthAB);
        System.out.println(lengthBC);
        System.out.println(lengthCA);
        return isEquilateral;
    }

    public double sideLength(){
        double sL = 0;
        if(this.isEquilateral() == true){
            sL = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
        }
        return sL;
    }

如何确定等边三角形中点的坐标?我知道中点是 x =(base / 2) y = heigth / 2 但这仅在基数为水平时有效(两个角具有相同的y值) )

2 个答案:

答案 0 :(得分:4)

对于等边三角形,三角形中心的坐标与其内圆的坐标相同。

查找the formula for the incircle's center on Wikipedia

{ (aXa+bXb+cXc)/(a+b+c), (aYa+bYb+cYc)/(a+b+c) }

a = b = c开始,很容易看出等边三角形中心的坐标只是

{ (x0+x1+x2)/3, (y0+y1+y2)/3 }

答案 1 :(得分:1)

这更像是一个数学问题,而不是一个java问题。无论如何,找到重心:

x = (x1 + x2 + x3) / 3
y = (y1 + y2 + y3) / 3