JavaFX多边形翻译,旋转,缩放及其要点

时间:2022-12-24 16:59:43

I ran into a strange issue with the polygon class from 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. 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?

我遇到了javafx(java 8)中的polygon类的一个奇怪问题。当我在多边形实例上应用一组平移,旋转或缩放时,它正确地在我的形状上移动多边形。问题是,getPoints()方法中的点保持不变。我现在开始创建自己的方法并移动点并重置它们,方法按照它们应该做的,但这是正确的方法吗?

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?

或者我必须分开实施它们?

1 个解决方案

#1


2  

Take a look at the subclasses of Transform (Affine, Rotate, Scale, Shear and Translate). They allow you to transform points stored in a double[] array using the transform2DPoints method.

看一下Transform的子类(仿射,旋转,缩放,剪切和平移)。它们允许您使用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));

#1


2  

Take a look at the subclasses of Transform (Affine, Rotate, Scale, Shear and Translate). They allow you to transform points stored in a double[] array using the transform2DPoints method.

看一下Transform的子类(仿射,旋转,缩放,剪切和平移)。它们允许您使用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));