Java:在质心周围旋转三角形

时间:2015-12-03 11:48:54

标签: java geometry

我创建的方法应该围绕它的质心旋转三角形, 它应该工作,但我的检查表明它没有。我知道它可能不是精确的旋转,但仍然指出质量中心在三角形旋转后应该保持不变。

我做错了什么?

    public void rotateAroundMassCenter(double grad){
    System.out.println("Starting...");

    Point initial = this.massCenter();

    Point o =this.massCenter();
    o.printPoint();
    double angle=Math.toRadians(grad);
    System.out.println();

    //formulas
    //    x' = x0+(x-x0)*cos(A)+(y0-y)*sin(alpha);
    //    y' = y0+(x-x0)*sin(A)+(y-y0)*cos(alpha);

    //new points
    int ax = (int) (o.x+(this.getA().x-o.x)*Math.cos(angle)-(this.getA().y-o.y) *Math.sin(angle));
    int ay = (int) (o.y+(this.getA().x-o.x)*Math.sin(angle)+(this.getA().y-o.y) * Math.cos(angle));
    this.a.movePoint(ax, ay);

    int bx = (int) (o.x+(this.getB().x-o.x)*Math.cos(angle)-(this.getB().y-o.y) *Math.sin(angle));
    int by = (int) (o.y+(this.getB().x-o.x)*Math.sin(angle)+(this.getB().y-o.y) * Math.cos(angle));
    this.b.movePoint(bx, by);

    int cx = (int) (o.x+(this.getC().x-o.x)*Math.cos(angle)-(this.getC().y-o.y) *Math.sin(angle));
    int cy = (int) (o.y+(this.getC().x-o.x)*Math.sin(angle)+(this.getC().y-o.y) * Math.cos(angle));
    this.c.movePoint(cx, cy);

    Point finalCenter=this.massCenter();
    System.out.println();
    finalCenter.printPoint();

    //check center position after rotate
    if (initial==finalCenter  ){
        System.out.println("Rotation was correct");
    }
    else{
        System.out.println("Algorytm is wrong");
    }
    this.print();
}

这里我是如何找到群众中心的:

// mass centre
    public Point massCenter(){
        double x = (this.getA().x+this.getB().x+this.getC().x)/3;
        double y = (this.getA().y+this.getB().y+this.getC().y)/3;
        Point o= new Point(x,y);
        return o;
    }

结果:

Ready to go
Triangle is: 
A:(1.0; 1.0)B: (3.0; 1.0)C: (1.0; 4.0)
Starting...
(1.6666666666666667;2.0)Old center
New center
(3.0;3.3333333333333335)Algorithm is wrong
Triangle is: 
A:(2.0; 1.0)B: (6.0; 3.0)C: (1.0; 6.0)

1 个答案:

答案 0 :(得分:0)

非常小的错误^^

if (initial==finalCenter  ){...

从不真实,你必须使用相等的比较:

if (initial.equals(finalCenter)  ){...