三角形质心定位 java_Java:围绕质心旋转三角形(Java: Rotate triangle around mass center)...

博客内容描述了一个Java方法,该方法尝试将三角形绕其质心旋转,但经过验证发现旋转后质心位置发生了变化。作者提供了计算质心和旋转三角形顶点的代码,并展示了错误的结果。
摘要由CSDN通过智能技术生成

Im creating method that should rotate triangle around it's mass center, it should work, but my check shows that it doesn't. I understand that it might be not precise rotation, but still point that is mass center should remains the same after rotation of triangle.

What I did wrong?

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();

}

here how i find mass center:

// 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;

}

Result:

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值