2017-03-08 84 views
0

我有一個由中心的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,看看它是否會工作(它沒有)。什麼是更好的方式去做這件事?

+0

似乎points'的'元素的值與變量'x'和回答'y '。他們的價值是什麼?他們在哪裏定義? – RealSkeptic

+0

'x'和'y'在構造函數中被實例化。 –

回答

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