gpt4 book ai didi

JavaFX - 旋转后绘制箭头中的额外水平空间

转载 作者:行者123 更新时间:2023-12-01 09:15:02 25 4
gpt4 key购买 nike

我有这部分代码:

Group g = new Group();

double xMid = (end.getCenterX() - start.getCenterX())/2;
double yMid = (end.getCenterY() - start.getCenterY())/2;
Line a = new Line(xMid, yMid-4, xMid, yMid+4);
Line b = new Line(xMid, yMid-4, xMid-10, yMid);
Line c = new Line(xMid, yMid+4, xMid-10, yMid);

g.getChildren().add(a);
g.getChildren().add(b);
g.getChildren().add(c);

g.setRotate(getAngle(start, end));
g.setTranslateX(start.getCenterX());
g.setTranslateY(start.getCenterY());

上面的代码负责在一条线的中间绘制一个箭头,从startend(这些是Circle s 作为起点和终点)。箭头必须朝正确的方向旋转。以下是带有此类箭头的多行的输出:

output

如您所见,如果角度接近 ±90°,则会向左移动。

read那:

[...] if transforms and effects are set directly on children of this Group, those will be included in this Group's layout bounds.

但这并没有真正帮助我解决这个问题。您有任何解决此问题的想法吗?

最佳答案

首先,构成三角形的 3 条线最容易使用多边形

要修复根据布局边界选择的枢轴点,您可以使用 Rotate 转换和 Translate 转换作为 transform或者简单地自行计算两个矩阵的串联并使用仿射:

private static final double[] POLYGON_POINTS = {
-5, -4,
-5, 4,
5, 0
};

private static void makeArrow(Pane parent, Circle start, Circle end, double t) {
Polygon arrow = new Polygon(POLYGON_POINTS);
arrow.setFill(null);
arrow.setStroke(Color.BLACK);
double dx = end.getCenterX() - start.getCenterX();
double dy = end.getCenterY() - start.getCenterY();

double d = Math.hypot(dx, dy);

double sin = dy / d;
double cos = dx / d;

// matrix:
// [ cos -sin 0 t * dx + start.getCenterX() ]
// [ sin cos 0 t * dy + start.getCenterY() ]
// [ 0 0 1 0 ]
// [ 0 0 0 1 ]
Affine affine = new Affine(cos, -sin, t * dx + start.getCenterX(), sin, cos, t * dy + start.getCenterY());

arrow.getTransforms().add(affine);
parent.getChildren().add(arrow);
}

@Override
public void start(Stage primaryStage) {
Circle end = new Circle(200, 20, 5);
Circle start = new Circle(20, 200, 5);
Line line = new Line(start.getCenterX(), start.getCenterY(), end.getCenterX(), end.getCenterY());

Pane root = new Pane(line, start, end);

makeArrow(root, start, end, 0.5);

Scene scene = new Scene(root, 400, 400);

primaryStage.setScene(scene);
primaryStage.show();
}

关于JavaFX - 旋转后绘制箭头中的额外水平空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40619578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com