获取正多边形的顶点列表

时间:2017-03-08 12:29:07

标签: java geometry

我有一个由中心的x和y坐标,边数,边长和水平旋转定义的正多边形。它为每个坐标一直吐出零。这是代码:

public Point[] getPoints() {
    Point[] points = new Point[n];
    double radius = s/(2*Math.sin(Math.PI/n));
    double angle = (Math.PI*2)/n;
    points[0] = new Point((int)Math.round(r),0);
    for(int i = 1; i < n; i++) {
        points[i] = multiplyByRotationMatrix(points[i-1],angle);
        System.out.println(points[i]);
    }
    for(int i = 0; i < n; i++) {
        points[i].x += x;
        points[i].y += y;
    }
    for(int i = 0; i < n; i++) {
        points[i] = multiplyByRotationMatrix(points[i],r);
    }
    return points;
}

private Point multiplyByRotationMatrix(Point p, double angle) {
    if(angle==0) return p;
    Point2D pNew = new Point2D.Float();
    pNew = AffineTransform.getRotateInstance(angle,p.x,p.y).transform(p, pNew);
    System.out.println(pNew.toString() + " , " + p.x + "," + p.y);
    return new Point((int)Math.round(pNew.getX()),(int)Math.round(pNew.getY()));
}

旋转矩阵位实际上是某个点上的旋转矩阵,但我将其更改为AffineTransform以查看它是否可行(它没有)。有什么更好的方法来解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的AffineTransform.getRotateInstance(angle,p.x,p.y)创建了围绕本身旋转点p的矩阵,因此其位置不会改变。

您的方法假定应该对原点进行轮换,因此您可以使用getRotateInstance的单参数版本。

请注意,您可以创建一次旋转矩阵并在循环内重复使用。

在我看来,在一个周期内在圆周上生成点更简单一点:

 p[i].x = center_x + radius * Cos(rotation_shift + i * 2 * Pi / n) 
 the same for y with Sin
相关问题