Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 绘制箭头不需要';行不通_Java_Javafx_Trigonometry - Fatal编程技术网

Java 绘制箭头不需要';行不通

Java 绘制箭头不需要';行不通,java,javafx,trigonometry,Java,Javafx,Trigonometry,我试图在JavaFX中绘制箭头。我做了所有的计算,甚至计算了弧度。由于某种原因,我的箭头画得不正确。我几乎认为它与trig函数的域/范围有关,但我不能确定 这是我的密码: package com.neonorb.test; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Line; import java

我试图在JavaFX中绘制箭头。我做了所有的计算,甚至计算了弧度。由于某种原因,我的箭头画得不正确。我几乎认为它与trig函数的域/范围有关,但我不能确定

这是我的密码:

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();
    }
}
很难用比…更多的东西来“回答”这个(非问题)

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

  • 它应该是
    sin
    cos
    而不是
    asin
    acos
  • 它应该是
    sin(x)*length
    ,而不是
    sin(x/length)
  • 已交换
    sin
    cos
  • 最好使用
    atan2
    计算线的角度(您使用的
    atan
    函数存在一些问题,尤其是
    starty==endy
  • 应将“偏移量”添加到线条角度-尤其是,它应为
    lineAngle-箭头角度
    而不是
    arrowAngle-直线角度
整个代码已更新:

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();
    }
}
很难用比…更多的东西来“回答”这个(非问题)

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

  • 它应该是
    sin
    cos
    而不是
    asin
    acos
  • 它应该是
    sin(x)*length
    ,而不是
    sin(x/length)
  • 已交换
    sin
    cos
  • 最好使用
    atan2
    计算线的角度(您使用的
    atan
    函数存在一些问题,尤其是
    starty==endy
  • 应将“偏移量”添加到线条角度-尤其是,它应为
    lineAngle-箭头角度
    而不是
    arrowAngle-直线角度
整个代码已更新:

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();
    }
}

计算x1时,将arcsinus(范围约为-1.57到1.57)的结果添加到等于100的endx。y1、x2和y2的情况也一样。这就是为什么在行尾有一个小黑点。@StephaneM这就是为什么我认为它与域/范围有关。当你计算x1时,你将arcsinus(范围从-1.57到1.57)的结果加在endx上,endx等于100。y1、x2和y2的情况也一样。这就是为什么在行尾有一个小黑点。@StephaneM这就是为什么我认为它与域/范围有关。