计算javaFX中随机形状的质心

时间:2016-12-07 23:38:07

标签: java javafx shape

如何计算javaFX中任何形状的质心?
在这个代码中,我有一个圆和正方形之间的交集,它可以是任意2个形状之间的交集,所以我需要一种方法来独立于2个原始形状计算相交形状的质心。到目前为止,相交形状的质心是最大和最小边界之和除以2(实际上不是质心)。

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

public class Intersection extends Application {

    public static void main(String [] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Pane playfield = new Pane();
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 1200, 900);

        root.setCenter(playfield);
        primaryStage.setScene(scene);

        Circle c1 = new Circle();
        Rectangle r2 = new Rectangle();
        playfield.getChildren().addAll(c1,r2);
        c1.setCenterX(300);
        c1.setCenterY(300);
        c1.setRadius(200);
        c1.setFill(Color.RED);

        r2.setX(400);
        r2.setY(400);
        r2.setHeight(400);
        r2.setWidth(400);
        r2.setFill(Color.BLUE);

        Shape intersection = null;
        intersection = Shape.intersect(r2, c1);
        double gcX = (intersection.getBoundsInParent().getMaxX() + intersection.getBoundsInParent().getMinX()) / 2;
        double gcY = (intersection.getBoundsInParent().getMaxY() + intersection.getBoundsInParent().getMinY()) / 2;

        Circle gravityCenter = new Circle();
        gravityCenter.setCenterX(gcX);
        gravityCenter.setCenterY(gcY);
        gravityCenter.setRadius(2);
        gravityCenter.setFill(Color.WHITE);
        intersection.setFill(Color.BLACK);
        playfield.getChildren().add(intersection);
        playfield.getChildren().add(gravityCenter);

        System.out.println(gcX + " " + gcY);

        primaryStage.show();
    }
}

0 个答案:

没有答案