我有一条有点(x1,y1)和(x2,y2)的线。我想要一个Button,它应该通过基于线段点旋转来对齐线。我需要一些帮助来计算Button的旋转角度。
答案 0 :(得分:2)
您可以使用Math.atan2(dy, dx)
从直角坐标(x,y)到极坐标(r,theta)的转换中获取角度theta
。稍后使用它将其转换为度数。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
double startX = 100;
double endX = 250;
double startY = 150;
double endY = 250;
Line line = new Line(startX, startY, endX, endY);
Button button = new Button("Button");
double rad = Math.atan2(endY - startY, endX - startX);
double degree = rad * 180/Math.PI;
button.setRotate(degree);
StackPane box = new StackPane(line, button);
Scene scene = new Scene(box, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
}
<强>输出强>