2015-12-21 131 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(); 
    } 
}