java - 画箭头不起作用

标签 java javafx trigonometry

我正在尝试在 JavaFX 中绘制箭头。我做了所有的数学计算,甚至还考虑了弧度的问题。由于某种原因,我的箭头没有正确绘制。我几乎认为这与三角函数的域/范围有关,但我不能确定。

enter image description here

这是我的代码:

package com.neonorb.test;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class ArrowTest extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        double startx = 200;
        double starty = 100;
        double endx = 100;
        double endy = 300;

        double arrowAngle = Math.toRadians(45.0);
        double arrowLength = 10.0;

        double lineAngle = Math.atan((startx - endx) / (starty - endy));

        double x1 = Math.asin((arrowAngle + lineAngle) / arrowLength) + endx;
        double y1 = Math.acos((arrowAngle + lineAngle) / arrowLength) + endy;

        double x2 = Math.asin((arrowAngle - lineAngle) / arrowLength) + endx;
        double y2 = Math.acos((arrowAngle - lineAngle) / arrowLength) + endy;

        Group root = new Group();

        Line line = new Line(startx, starty, endx, endy);
        Line arrowHead1 = new Line(endx, endy, x1, y1);
        Line arrowHead2 = new Line(endx, endy, x2, y2);

        root.getChildren().addAll(line, arrowHead1, arrowHead2);

        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }
}

最佳答案

很难用比......更多的东西来“回答”这个(非问题)

...你的数学在几个方面都搞砸了:

  • 应该是 sincos,而不是 asinacos
  • 应该是sin(x)*length,而不是sin(x/length)
  • sincos 已交换
  • 直线的角度最好使用 atan2 计算(您使用的 atan 函数有一些问题,显然,特别是使用 starty==endy )
  • “偏移”应添加到线角度 - 特别是,它应该是 lineAngle - arrowAngle 而不是 arrowAngle - lineAngle

更新后的整个代码:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;


public class ArrowTest extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        double startx = 200;
        double starty = 100;
        double endx = 100;
        double endy = 300;

        double arrowAngle = Math.toRadians(45.0);
        double arrowLength = 10.0;

        double lineAngle = Math.atan2(starty - endy, startx - endx);

        double x1 = Math.cos(lineAngle + arrowAngle) * arrowLength + endx;
        double y1 = Math.sin(lineAngle + arrowAngle) * arrowLength + endy;

        double x2 = Math.cos(lineAngle - arrowAngle) * arrowLength + endx;
        double y2 = Math.sin(lineAngle - arrowAngle) * arrowLength + endy;

        Group root = new Group();

        Line line = new Line(startx, starty, endx, endy);
        Line arrowHead1 = new Line(endx, endy, x1, y1);
        Line arrowHead2 = new Line(endx, endy, x2, y2);

        root.getChildren().addAll(line, arrowHead1, arrowHead2);

        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }
}

关于java - 画箭头不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399415/

相关文章:

java - 如何在java中将列表字符串列表转换为列表整数列表

java - 添加两个数组列表的问题

JavaFX TreeTableView 图像工件

java - 在可执行 JAR 中将 JavaFX 库与 Maven 捆绑在一起(适用于 Java 11+)

windows - 如何在 Windows 上更改 JPackage 应用程序的窗口图标?

java - Firebase JAVA 管理 sdk FirebaseAuthException

java - 如何将 Set<Integer> (Java) 中的所有整数加 1?

javascript - ThreeJS 运动 - 在固定时间内绕圈移动

java - Java的快速超越/三角函数

C++使用if-else语句计算sin和cos