我目前正在使用JavaFX处理不同形状的交集范围。我想在它们的点上检测两个多边形的碰撞,而不是在它们的范围上(即2个多边形)。
请参见图1:不希望的行为,和图2:期望的行为。
有没有现成的算法或库可以帮助我?
谢谢! :)
以下是我的解决方案:
请参见图1:不希望的行为,和图2:期望的行为。
有没有现成的算法或库可以帮助我?
谢谢! :)
以下是我的解决方案:
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);
}
}
输出:
20.0:16.0
检测到交叉点
看起来它正常工作,我将使用路径对象替换多边形对象进行测试。
mypolygon1.getBoundsInLocal().contains(myPolygon2.getBoundsInLocal())
- Aspirant