首页 文章

包含椭圆的组围绕任意轴旋转,如何获得组上椭圆的x和y距离变化?

提问于
浏览
0

我有一个带椭圆的组 . 该组围绕任意点旋转(通过向“组”变换添加“旋转”实例) . 如何找出椭圆(中心)改变的距离(x和y)?由于转换应用于组,因此椭圆的中心属性不会更改 .

我猜这可以用数学方法解决,也可以用Group / Ellipse上的变换实例来解决,但是,在这两个领域我没有太多的专业知识,似乎没有达到正确的结果 .

import javafx.animation.Transition;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.scene.Group;
import javafx.scene.GroupBuilder;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.layout.Pane;
import javafx.scene.layout.PaneBuilder;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.EllipseBuilder;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.RotateBuilder;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

@Override
public void start(Stage stage) throws Exception {
    Ellipse e = EllipseBuilder.create().centerX(110).centerY(110).radiusX(5).radiusY(5).style("-fx-fill: green;").build();
    Rectangle r = RectangleBuilder.create().x(100).y(100).width(30).height(30).style("-fx-fill: red;").build();
    Group group = GroupBuilder.create().children(r, e).build();

    Ellipse pivot = EllipseBuilder.create().centerX(60).centerY(100).radiusX(2).radiusY(2).style("-fx-fill: purple;").build();

    Pane p = PaneBuilder.create().children(group, pivot).build();

    Scene scene = SceneBuilder.create().root(p).width(200).height(200).build();
    stage.setScene(scene);
    stage.show();

    final Rotate rotate = RotateBuilder.create().pivotX(pivot.getCenterX()).pivotY(pivot.getCenterY()).build();
    group.getTransforms().add(rotate);

    RotationTransition trans = new RotationTransition(rotate.angleProperty());
    trans.playFromStart();
}

class RotationTransition extends Transition {
    private final DoubleProperty angle;

    public RotationTransition(DoubleProperty angle) {
        this.angle = angle;
        setCycleDuration(Duration.seconds(5));
    }

    @Override
    protected void interpolate(double frac) {
        angle.setValue(frac * 60);
    }
}

public static void main(String[] args) {
    launch(args);
}
}

上面的代码显示了一个旋转动画 . 如何找出旋转后绿色椭圆变化的x和y距离?或者,同样,我如何找出椭圆的新坐标?

1 回答

  • 1

    虽然我仍然无法理解复杂场景中的各种localToParent,parentToLocal等方法(多个嵌套组具有不同的变换),但我通过一些实验发现,在发布的代码示例中

    group.localToParent(e.getCenterX(), e.getCenterY())
    

    似乎工作 . 在我的实际应用中,我只对移动的距离而不是实际位置感兴趣,所以相同的方法在那里工作 . (尽管考虑到我复杂的群体层次,它可能没有给出有意义的绝对数字) .

相关问题