如何使用Java围绕点旋转多边形? [英] How to rotate a polygon around a point with Java?

查看:432
本文介绍了如何使用Java围绕点旋转多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Canvas对象(线,顶点,三角形,...),我想对它们应用一个点的旋转。

I'm creating a Canvas object ( lines, vertices, triangle, ...) and I would like to apply to them a rotation around a point.

我不能使用Canvas的rotate()方法,因为点在地图上附加到GeoPoint,所以如果我使用rotate()方法,所有的地图都在旋转...

I can't use the rotate() method of Canvas because points are attached to GeoPoint on a Map, so if I use the rotate() method the all map is rotating ...

问题是Canvas需要Point(int,int),并且应用旋转因为cos和sin函数而创建double。所以,当我应用旋转到所有的点,因为转换double到int,我有一些图形问题发生...

The problem is that Canvas needs Point(int,int) and applying a rotation creates double because of cos and sin functions. So when I apply the rotation to the all points, because of casting double to int, I have some graphical issue that happens...

所以我在寻找最好的解决方案。

So I'm looking for the best solution.

这里我的旋转代码:

public Point rotatePoint(Point pt, Point center)
{
    this.angle = ((this.angle/180)*Math.PI);
    double cosAngle = Math.cos(this.angle);
    double sinAngle = Math.sin(this.angle);

    pt.x = center.x + (int) ((pt.x-center.x)*cosAngle-(pt.y-center.y)*sinAngle);
    pt.y = center.y + (int) ((pt.x-center.x)*sinAngle+(pt.y-center.y)*cosAngle);
    return pt;
}


推荐答案

好。一个小的改进将添加0.5到坐标,然后将它们转换为整数,这样你将有我们通常使用的舍入 - 大于0.5的所有值将得到四舍五入为例如。除此之外,我不认为你可以避免做四舍五入,因为你想把一个连续的空间放置成离散的(即飞机到画布)。

I believe your solution is quite good. A small improvement would be to add 0.5 to the coordinates before casting them to integer and that way you will have the rounding we are usually used to - everything above 0.5 will get rounded to 1 for instance. Other than that I don't think you can avoid doing rounding as you want to place a continuous space into discrete one(i.e. the plane to a canvas).

这篇关于如何使用Java围绕点旋转多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆