[英]Java: AffineTransform rotate Polygon, then get its points
我想繪制一個多邊形,並使用AffineTransform旋轉它。
float theta = 90;
Polygon p = new Polygon(new int[]{0, 4, 4, 0}, new int[]{0, 0, 4, 4}, 4);
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(theta), p.xpoints[0], p.ypoints[0]);
Shape transformed = transform.createTransformedShape(p);
g2.fill(transformed);
但是,我希望能夠以與多邊形相同的方式訪問這些點(transformed.xpoints [0])。 一種解決方法是將Shape轉換為Polygon,但據我所知這是不可能的。
最好的選擇是什么?
附帶說明:這是創建由4邊多邊形(矩形)組成的分形樹的練習。 我選擇使用“多邊形”以便分別將分支錨定到左上角和右上角。 如果這不必要地復雜,請告訴我。
您也可以使用AffineTransform
轉換單個點,如下所示:
Point2D[] srcPoints = new Point2D[] { new Point(0, 0), new Point(4, 0), new Point(4, 4), new Point(4, 0) };
Point2D[] destPoints = new Point2D[4];
transform.transform(srcPoints, 0, destPoints, 0, 4);
生成的destPoints
數組如下所示:
[Point2D.Float[-0.0, 0.0], Point2D.Float[-0.0, 4.0], Point2D.Float[-4.0, 4.0], Point2D.Float[-0.0, 4.0]]
您可以從createTransformedShape(...)
返回的路徑中獲取坐標。
Path2D.Double transformed = (Path2D.Double) transform.createTransformedShape(p);
List<Double> xpointsList = new ArrayList<>();
List<Double> ypointsList = new ArrayList<>();
PathIterator pi = transformed.getPathIterator(null);
while(!pi.isDone()){
double[] coords = new double[6];
int type = pi.currentSegment(coords);
if(type == PathIterator.SEG_MOVETO || type == PathIterator.SEG_LINETO){ // The only types we're interested in given the original shape
xpointsList.add(coords[0]);
ypointsList.add(coords[1]);
}
pi.next();
}
我已經有一段時間沒有這樣做了,可能有一種更簡單的方法來實現您想要的。 另外,對Path2D.Double
的轉換也不理想。
已經有一段時間了,但是對於那些以后會遇到此問題的人,我忍不住把它放在這里。
那么,我們只使用小數位數而不是AffineTransform怎么樣?
public class ROTOR {
/**
* Method for rotating a Point object in the XY plane or in any plane
* parallel to the XY plane. The Point object to be rotated and the one
* about which it is rotating must have the same Z coordinates.
*
* @param p a Point object in the xy plane The z coordinates of both Point
* objects must be either the same or must both be zero.
* @param cen a Point object about which the rotation of the first Point
* object will occur.
* @param angle the angle of rotation
* @return
*/
public static Point planarXYRotate(Point p,Point cen,double angle){
double sin = Math.sin(angle);
double cos = Math.cos(angle);
double X = p.x*cos-p.y*sin+cen.x*(1-cos)+cen.y*sin;
double Y = p.x*sin+p.y*cos+cen.y*(1-cos)-cen.x*sin;
return new Point( (int) X, (int) Y );
}//end method
/**
*
* @param polygon The polygon to rotate
* @param origin The point about which to rotate it.
* @param angle The angle of rotation.
* @return a new Polygon rotated through the specified angle and about the said point.
*/
public static Polygon rotate(Polygon polygon , Point origin , double angle){
Point cen = new Point(origin.x , origin.y);
Polygon newPolygon = new Polygon();
for(int i=0;i<polygon.npoints;i++){
Point p = new Point( polygon.xpoints[i] , polygon.ypoints[i] );
Point point = ROTOR.planarXYRotate(p, cen, angle);
newPolygon.addPoint((int) point.x, (int) point.y);
}
return newPolygon;
}
}
然后,您可以使用以下方法輕松獲取旋轉的多邊形並獲取所需的點:
Polygon rotatedPolygon = ROTOR.rotate(polygon, origin, angle);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.