2015-12-21 134 views
0

我想在JavaFX中绘制箭头。我做了所有的数学,甚至占了弧度的东西。出于某种原因,我的箭头没有被正确绘制。我几乎认为它与trig函数的域/范围有关,但我无法确定。绘制箭头不起作用

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

当你计算X1添加arcsinus(从约-1.57至1.57),以endx等于100同样的事情Y1的结果, x2和y2。这就是为什么你会在你的产品线末端产生一个小黑点。 – StephaneM

+0

@StephaneM这就是为什么我认为它必须处理域/范围。 –

回答

5

很难 “答案” 本(非问题)的东西比更多...

...您数学是在几个方面搞砸了:

  • 它应该是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(); 
    } 
}