sách gpt4 ai đã đi

Vấn đề va chạm giữa đường nét dày javafx (đường thẳng) và đường tròn

In lại 作者:行者123 更新时间:2023-11-30 03:24:00 29 4
mua khóa gpt4 Nike

我正在使用 JavaFx 8 库。

我的任务很简单:我想检查圆是否与周围有粗笔划的路径碰撞。问题是 Path.intersect() 和 Shape.intersect() 函数都会忽略路径/线周围的笔划。

Path tempPath = new Path(player.getPath().getElements());
//player.getDot() is Circle
if(tempPath.intersects(player.getDot().getBoundsInParent())){
Shape intersect = Shape.intersect(tempPath, player.getDot());
if(intersect.getBoundsInLocal().getWidth() != -1){
System.out.println("Path Collision occurred");
}
}

我的路径由许多 LineTo 对象组成。格式是这样的:

/** Creates path and player dot */
private void createPath() {
this.path = new Path();
this.path.setStrokeWidth(20);
this.path.setStroke(Color.RED);
this.path.setStrokeLineCap(StrokeLineCap.ROUND);
this.path.setStrokeLineJoin(StrokeLineJoin.ROUND);

this.dot = new Circle(10, Color.BLUE);
this.dot.setOpacity(1);
}

如何实现成功的碰撞检测?

1 Câu trả lời

碰撞检测工作得很好:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Shape;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeLineJoin;
import javafx.stage.Stage;

public class Main extends Application {
@Ghi đè
public void start(Stage primaryStage) {

BorderPane root = new BorderPane();

Circle circle = new Circle(100, 100, 30);
Path path = new Path();

double x = 144;

path.getElements().add(new MoveTo(x, 140));
path.getElements().add(new LineTo(500, 100));
path.getElements().add(new ClosePath());

path.setStrokeWidth(60);
path.setStrokeLineCap(StrokeLineCap.ROUND);
path.setStrokeLineJoin(StrokeLineJoin.ROUND);

Shape shape = Shape.intersect(circle, path);

boolean intersects = shape.getBoundsInLocal().getWidth() != -1;

System.out.println("Intersects: " + intersects);

Pane pane = new Pane();
pane.getChildren().addAll(circle, path);
root.setCenter(pane);

Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();

}

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

将 x = 144 切换为 x = 145 进行测试。控制台显示是否有交叉点。

您遇到的问题是您正在比较不同的界限。

请参阅 Nút 的文档,“边界矩形”部分介绍了如何计算各种边界。

关于javafx粗笔画路径(线)与圆之间的碰撞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30705426/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com