在JavaFX中反映任意轴周围的几何节点

时间:2015-10-11 23:20:20

标签: javafx geometry

javafx中是否有一种方法可以反映/镜像任意轴周围的几何节点(例如线) - 一个是用户定义的?

我看到旋转,缩放,剪切和平移方法,但没有反射(镜像)。

我对一篇建议使用从左到右或从右到左的对齐的帖子进行了讨论,但它并没有放弃我正在寻找的内容。

我还没有构建应用程序来发布示例代码,因为我在启动之前仍然在寻找相当多的库。

社区的任何提示都会很棒!

编辑:不确定如何将图像添加到评论中。我在下面尝试了James的代码,我在下面的图片中得到了结果,但我试图在阅读中注明这一点。

enter image description here

1 个答案:

答案 0 :(得分:4)

如果您有两个点p1p2定义的线段,那么反映在该线段中相当于

  1. p1翻译为原点(T
  2. p2旋转到正x轴(R
  3. 在x轴上进行反射(您可以使用x=1y=-1进行比例)
  4. R的反向
  5. T的反向
  6. 所以这看起来像

    private Transform createReflection(Point2D p1, Point2D p2) {
        Translate translation = new Translate(-p1.getX(), -p1.getY());
        double deltaX = p2.getX() - p1.getX();
        double deltaY = p2.getY() - p1.getY();
        Rotate rotate = new Rotate(-Math.toDegrees(Math.atan2(deltaY, deltaX)));
        Scale scale = new Scale(1, -1);
        Affine reflection = new Affine();
        reflection.append(translation.createInverse());
        try {
            // not sure how a rotation could possibly be non-invertible:
            reflection.append(rotate.createInverse());
        } catch (NonInvertibleTransformException e) {
            throw new RuntimeException(e);
        }
        reflection.append(scale);
        reflection.append(rotate);
        reflection.append(translation);
        return reflection ;
    }
    

    SSCCE:

    import javafx.application.Application;
    import javafx.geometry.Point2D;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.shape.Line;
    import javafx.scene.text.Text;
    import javafx.scene.transform.Affine;
    import javafx.scene.transform.NonInvertibleTransformException;
    import javafx.scene.transform.Rotate;
    import javafx.scene.transform.Scale;
    import javafx.scene.transform.Transform;
    import javafx.scene.transform.Translate;
    import javafx.stage.Stage;
    
    
    public class ArbitraryReflection extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
            Text original = new Text("Through the looking glass");
            original.relocate(200,  350);
    
            Text reflected = new Text("Through the looking glass");
            reflected.relocate(200, 350);
    
            Group reflectedGp = new Group(reflected);
    
    
            Point2D start = new Point2D(0, 400);
            Point2D end = new Point2D(400, 150);
            Line axis = new Line(start.getX(), start.getY(), end.getX(), end.getY());
            Pane root = new Pane(original, reflectedGp, axis);
            reflectedGp.getTransforms().add(createReflection(start, end));
    
            Scene scene = new Scene(root, 400, 400);
            primaryStage.setScene(scene);
            primaryStage.show();
    
    
    
        }
    
        private Transform createReflection(Point2D p1, Point2D p2) {
            Translate translation = new Translate(-p1.getX(), -p1.getY());
            double deltaX = p2.getX() - p1.getX();
            double deltaY = p2.getY() - p1.getY();
            Rotate rotate = new Rotate(-Math.toDegrees(Math.atan2(deltaY, deltaX)));
            Scale scale = new Scale(1, -1);
            Affine reflection = new Affine();
            reflection.append(translation.createInverse());
            try {
                // not sure how a rotation could possibly be non-invertible:
                reflection.append(rotate.createInverse());
            } catch (NonInvertibleTransformException e) {
                throw new RuntimeException(e);
            }
            reflection.append(scale);
            reflection.append(rotate);
            reflection.append(translation);
            return reflection ;
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }