绘制箭头不起作用

时间:2015-12-21 15:44:53

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

1 个答案:

答案 0 :(得分:5)

很难用“...”来回答这个问题(非问题)

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

  • 应该是public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. displayView(item.getItemId()); return true; } //method to replace Views in ID = content_frame in content_main public void displayView(int viewID) { fragment = null; title = getString(R.string.app_name); switch (viewID) { case R.id.nav_frag0: fragment = new OldStory(); title = getString(R.string.story); viewIsAtHome = true; break; case R.id.nav_frag1: fragment = new Fragment1(); title = getString(R.string.fragment1); viewIsAtHome = false; break; case R.id.nav_frag2: fragment = new Fragment2(); title = getString(R.string.fragment2); viewIsAtHome = false; break; case R.id.nav_frag3: fragment = new Fragment3(); title = getString(R.string.fragment3); viewIsAtHome = false; break; case R.id.nav_frag4: fragment = new Fragment4(); viewIsAtHome = false; title = getString(R.string.fragment4); break; case R.id.nav_share: fragment = new Fragment5(); title = getString(R.string.fragment5); viewIsAtHome = false; break; } if (fragment != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content_frame,fragment); ft.commit(); } //set the toolbar title if(getSupportActionBar() != null) { getSupportActionBar().setTitle(title); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); } sin而不是cosasin
  • 应该是acos,而不是sin(x)*length
  • sin(x/length)sin已被换掉
  • 最好使用cos来计算线条的角度(您使用的atan2函数存在一些问题,显然尤其是atan
  • 应将“偏移量”添加到线条角度 - 尤其应该是starty==endy而不是lineAngle - arrowAngle

整个代码,更新:

arrowAngle - lineAngle