gpt4 book ai didi

形状和多边形的 JavaFX 交集

转载 作者:行者123 更新时间:2023-12-02 03:40:47 28 4
gpt4 key购买 nike

我目前正在使用 JavaFX 研究不同形状之间的边界相交。我想检测两个多边形在它们的点上而不是在它们的边界上的碰撞(即 2 个多边形)。

请参见图 1:不需要的行为和图 2:需要的行为。

是否有任何现有算法可以帮助我或任何图书馆使用?提前致谢:)

enter image description here

在这里找到我的解决方案:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

public class Tester extends Application {

@Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
root.setStyle("-fx-background-color:cyan;");
Polygon p1 = new Polygon();
p1.getPoints().addAll(new Double[]{
50.,50.,
50.,100.,
60.,100.,
60.,80.,
80.,70.,
80.,100.,
100.,100.,
100.,50.
});
p1.setFill(Color.GREEN);

Polygon p2 = new Polygon();
p2.getPoints().addAll(new Double[]{
65.,100.,
65.,90.,
75.,80.,
100.,100.
});
p2.setFill(Color.RED);

root.getChildren().addAll(p1,p2);

stage.setScene(new Scene(root));
stage.show();

Shape inter = Shape.intersect(p1, p2);
root.getChildren().add(inter);

System.out.println(inter.getLayoutBounds().getWidth() + ":" + inter.getLayoutBounds().getHeight());
if(inter.getLayoutBounds().getHeight()<=0 || inter.getLayoutBounds().getWidth()<=0) {
System.out.println("No intersection");
}
else {
System.out.println("intersection detected");
}
}

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

输出:

enter image description here

20.0:16.0
检测到交叉路口

它似乎工作正常,我将用 Path 对象测试以替换 Polygon objets。

最佳答案

我在玩你的代码\,我找到了这个解决方案,我将鼠标事件添加到 polygon2,然后我修改了 if 语句。如果还不算太晚,请尝试。

public class Tester extends Application {

Shape inter;
Point2D pc;
double initX, initY, mx, my;

@Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
root.setStyle("-fx-background-color:cyan;");
final Polygon p2 = new Polygon();
final Polygon p1 = new Polygon();
p1.getPoints().addAll(new Double[]{
50., 50.,
50., 100.,
60., 100.,
60., 55.,
80., 70.,
80., 100.,
100., 100.,
100., 50.
});
p1.setFill(Color.GREEN);
p2.getPoints().addAll(new Double[]{
65., 100.,
65., 45.,
75., 80.,
100., 100.
});

p2.setFill(Color.RED);

p1.setTranslateX(400);
p1.setTranslateY(250);

p2.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
initX = p2.getTranslateX();
initY = p2.getTranslateX();
pc = new Point2D(me.getSceneX(), me.getSceneY());
}
});

p2.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
double dragX = me.getSceneX() - pc.getX();
double dragY = me.getSceneY() - pc.getY();
double newXPosition = initX + dragX;
double newYPosition = initY + dragY;
p2.setTranslateX(newXPosition);
p2.setTranslateY(newYPosition);
System.out.println("no intersection");
if (Shape.intersect(p2, p1).getBoundsInLocal().isEmpty() == false) {
p1.setTranslateX(p2.getTranslateX() + mx);
p1.setTranslateY(p2.getTranslateY() + my);
System.out.println("colision");

} else {
mx = p1.getTranslateX() - p2.getTranslateX();
my = p1.getTranslateY() - p2.getTranslateY();
}

}
});
root.getChildren().addAll(p1, p2);

final Scene scene = new Scene(root, 1200, 850);
stage.setScene(scene);
stage.show();
}

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

关于形状和多边形的 JavaFX 交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20247355/

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