简体   繁体   English

正多边形与圆相交

[英]Regular Polygon intersection with a circle

I would like to Draw an Regular Polygon inscrit , and I have wrote some code with the usually formula angle = 2*Math.Pi/numside ecc etc.我想画一个正多边形 inscrit ,我写了一些代码,通常公式为angle = 2*Math.Pi/numside ecc 等。

The Poylgon it's drawn with the circle dynamically with mouse movement in this way:以这种方式通过鼠标移动动态绘制圆的 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);
            }
        }
  });

And another pieace of code :还有一段代码:

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

THE PROBLEM: I started with 3 side for then I'll write the code for a number of side generic, but All going fine if I increase dynamically the radius of circle (increment the radius -> triangle increase ).问题:我从 3 边开始,然后我将编写一些通用边的代码,但是如果我动态增加圆的半径(增加半径 -> 三角形增加),一切都会好起来的。

But if I decrease the radius of circle, the polygon doesn't follow the behaviuor of the circle, and I can't understand why, any suggestion please?但是如果我减小圆的半径,多边形不遵循圆的行为,我不明白为什么,有什么建议吗?

PS: please ignore the Doubleproperty and some code written only for a test, I know that if I pass the circle object I don't need the other parameter. PS:请忽略Doubleproperty和一些仅为测试编写的代码,我知道如果我传递了circle对象,我不需要其他参数。 At the moment I'm looking at the reason why the polygon does not follow the circumference.目前我正在研究多边形不遵循圆周的原因。

这是不遵循的示例的图像

You never clear any of the points from the Polygon .您永远不会清除Polygon任何点。 You need to make sure the coordinates in the points list remains twice the number of sides.您需要确保points列表中的坐标保持为边数的两倍。 Furthermore the parameters of setPolygon are not defined well: You pass the same info though the first 3 and the last parameter;此外, setPolygon的参数没有很好地定义:您通过前 3 个和最后一个参数传递相同的信息; also you're using the reference type Double for no obvious reason.您也无缘无故地使用引用类型Double The following code improves on the code a bit:以下代码对代码进行了一些改进:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM