带圆的常规多边形相交

时间:2018-10-16 16:03:42

标签: java javafx polygon

我想绘制一个正多边形inscrit,并且我编写了一些代码,它们的公式通常为angle = 2 * Math.Pi / numside ecc ecc。
用圆环以灵巧的运动通过二维绘制的Poylgon:

public void Set_beahvior(Pane DrawPane, BottomPane bottompane )
   {

  this.selectedProperty().addListener(new ChangeListener<Boolean>() 

      {

        private final EventHandler<MouseEvent> pressedHandler = (event)    -> {

                cerchio = new MyCircle(event.getX(), event.getY(), 0.0, DrawPane ,bottompane1);
                cerchio.setCenterX(event.getX());
                cerchio.setCenterY(event.getY());
                cerchio.setStrokeType(StrokeType.OUTSIDE);
                cerchio.setStroke(Color.RED);
                cerchio.setFill(Color.TRANSPARENT);
                cerchio.setStrokeWidth(1);
                currentPolygon= new MyPolygon2(DrawPane,bottompane);

                DrawPane.getChildren().addAll(cerchio);
                DrawPane.getChildren().addAll(currentPolygon);              
        };

        private final EventHandler<MouseEvent> draggedHandler = (event) -> {
            cerchio.setRadius((event.getX()-cerchio.getCenterX()));
            currentPolygon.setPolygon(cerchio.getCenterX(), cerchio.getCenterY(), cerchio.getRadius(),cerchio);

        };

        private final EventHandler<MouseEvent> releasedHandler = (event) -> {
            cerchio.setRadius((event.getX()-cerchio.getCenterX()));
            cerchio.setStroke(Color.TRANSPARENT);

        };

        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            // add/remove event handlers based on toggle state of the button
            if (newValue) {
                DrawPane.addEventHandler(MouseEvent.MOUSE_PRESSED, pressedHandler);
                DrawPane.addEventHandler(MouseEvent.MOUSE_DRAGGED, draggedHandler);
                DrawPane.addEventHandler(MouseEvent.MOUSE_RELEASED, releasedHandler);
            } else {
                DrawPane.removeEventHandler(MouseEvent.MOUSE_PRESSED, pressedHandler);
                DrawPane.removeEventHandler(MouseEvent.MOUSE_DRAGGED, draggedHandler);
                DrawPane.removeEventHandler(MouseEvent.MOUSE_RELEASED, releasedHandler);
            }
        }
  });

还有另一段代码:

     public void setPolygon(Double x, Double y, Double radius, MyCircle circle){

          DoubleProperty raggio = new SimpleDoubleProperty(circle.getRadius());

          double section = 2.0 * Math.PI/Numside;

          this.getPoints().addAll((x + raggio.doubleValue() *  Math.cos(0)), (y + raggio.doubleValue() * Math.sin(0)));
          for(int i=1; i<Numside; i++){ 
               this.getPoints().addAll((x + raggio.doubleValue() * Math.cos(section * i)), (y + raggio.doubleValue() * Math.sin(section * i)));
              }
         } 

问题:我从3面开始,然后我将写一些通用面的代码,但是如果我将圆的半径右增加(增加半径->三角形递增),一切都会很好。 /> 但是如果我减小圆的半径,多边形就不会跟随圆的行为,而且我不明白为什么,会有任何建议吗?

PS:请忽略Doubleproperty和一些只为测试编写的代码,我知道如果我通过了圆对象,则不需要其他参数,此刻,我正在研究多边形的原因。不跟随圆周。

Here image of an example of not following

1 个答案:

答案 0 :(得分:1)

您永远不会清除Polygon中的任何要点。您需要确保points列表中的坐标保持为边数的两倍。此外,setPolygon的参数定义不正确:您通过前三个参数和最后一个参数传递了相同的信息;同样,您使用引用类型Double的原因也不明显。以下代码对代码进行了一些改进:

private final int numside;
private final double[] points;

public MyPolygon2(int numside) {
    this.numside = numside;
    this.points = new double[numside * 2];
    double step = 2 * Math.PI / numside;

    for (int i = 0; i < numside; i++) {
        int index = 2 * i;
        double angle = i * step;
        points[index] = Math.cos(angle);
        points[index + 1] = Math.sin(angle);
    }
}

public void setPolygon(double x, double y, double radius) {
    getPoints().clear();
    for (int i = 0; i < points.length;) {
        getPoints().addAll(x + radius * points[i++], y + radius * points[i++]);
    }
}