gpt4 book ai didi

java - 正多边形与圆相交

转载 作者:行者123 更新时间:2023-12-02 10:42:27 25 4
gpt4 key购买 nike

我想绘制一个正多边形 inscrit ,并且我用通常的公式 angle = 2*Math.Pi/numside ecc 等编写了一些代码。

通过鼠标移动动态绘制的多边形是这样的:

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

最佳答案

您永远不会清除多边形中的任何点。您需要确保点列表中的坐标保持为边数的两倍。此外,setPolygon 的参数没有很好地定义:您通过前 3 个和最后一个参数传递相同的信息;此外,您还没有明显的原因使用引用类型 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++]);
}
}

关于java - 正多边形与圆相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52839642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com