Regular Polygon intersection with a circle
我想画一个正多边形inscrit,我写了一些代码,通常公式angle=2*math.pi/numside ecc ecc。< BR>以这种方式,用木糖运动的圆周来画出它:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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); } } }); |
另一个代码:
1 2 3 4 5 6 7 8 9 10 11 | 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边开始,然后我将编写一些side-generic的代码,但是如果我以常规方式增加圆的半径(增加半径->三角形增量),一切都会很好的。
但是如果我减小了圆的半径,多边形就不会跟在圆的bheavior后面,我也不明白为什么,有什么建议吗?< BR>
PS:请忽略双重属性和一些只为测试而编写的代码,我知道如果我通过了圆对象,我不需要其他参数,现在我正在寻找多边形不跟随圆周的原因。
你从来没有从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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++]); } } |