简体   繁体   English

JavaFX多边形平移,旋转,缩放及其点

[英]JavaFX Polygon Translation, Rotation, Scaling and its Points

I ran into a strange issue with the polygon class from javafx (java 8). 我遇到了来自javafx(java 8)的多边形类的奇怪问题。 When I apply a set translate, rotate or scale on the polygon instance it is correctly moving the polygon around on my shape. 当我在多边形实例上应用平移,旋转或缩放时,它可以在我的形状上正确移动多边形。 The problem is, the points in the getPoints() method stay the same. 问题是,getPoints()方法中的点保持不变。 I started now to create my own methods and moving around the points and resetting them, the methods do what they should, but is it the right way? 我现在开始创建自己的方法,并在各个要点周围移动并重置它们,这些方法将执行应有的作用,但这是正确的方法吗?

Here an example: 这里是一个例子:

private void translatePoints(double translateX, double translateY) {
    List<Double> newPoints = new ArrayList<>();
    for (int i = 0; i < getPoints().size(); i += 2) {
        newPoints.add(getPoints().get(i) + translateX);
        newPoints.add(getPoints().get(i + 1) + translateY);
    }
    getPoints().clear();
    getPoints().addAll(newPoints);
}

Is there a way to get the translated, rotated and scaled points after a couple of operations? 经过几次操作后,是否可以获取转换,旋转和缩放的点?

Or do I have to implement them all separatly? 还是我必须分开实施它们?

Take a look at the subclasses of Transform ( Affine , Rotate , Scale , Shear and Translate ). 看一下Transform的子类( AffineRotateScaleShearTranslate )。 They allow you to transform points stored in a double[] array using the transform2DPoints method. 它们使您可以使用transform2DPoints方法转换存储在double[]数组中的点。

double[] points = new double[] {
    0, 0,
    0, 1,
    1, 1,
    1, 0
};
Rotate rot = new Rotate(45, 0.5, 0.5);
Translate t = new Translate(5, 7);
Scale sc = new Scale(3, 3);

for (Transform transform : Arrays.asList(rot, t, sc)) {
    transform.transform2DPoints(points, 0, points, 0, 4);
}

System.out.println(Arrays.toString(points));

this way you need to take care of determining the pivot point of transforms where this is relevant on your own. 这样,您需要自己确定与变换相关的变换的枢轴点。

You could also get resulting transform for a node using Node.getLocalToParentTransform . 您还可以使用Node.getLocalToParentTransform获得节点的最终转换。

double[] points = polygon.getPoints().stream().mapToDouble(Number::doubleValue).toArray();
polygon.getLocalToParentTransform().transform2DPoints(points, 0, points, 0, points.length/2);
System.out.println(Arrays.toString(points));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM