我的应用程序中有一个圆圈数组列表。基本上,我正在绘制一个由六边形瓷砖制成的游戏板。我想在每个瓷砖的每个角上画圆圈。我将所有图 block 存储在 Tile 对象数组中。每个图 block 都有一个组成该图 block 的点数组。所以我基本上遍历每个图 block 和图 block 的每个角。我的问题是大多数图 block 共享交叉点。因此,为共享该交点的每个图 block 绘制一个圆圈。但我只想在每个路口画一个。我需要做的是检查数组中是否存在具有特定坐标集的圆。如果确实存在,则不要绘制圆圈。我知道 ArrayList.contains(),但不知道如何将其用于我的目的。有什么想法吗?
for (int t = 0; t < tiles.length; t++) {
//Loop through the vertices of the current tile
for (int v = 0; v < tiles[t].vertices.length; v++) {
final double x = tiles[t].vertices[v].x;
final double y = tiles[t].vertices[v].y;
Platform.runLater(() -> {
//This if statement doesn't work because the arraylist contains shapes, not points!
if(!highlightedVertices.contains(new Point((int)x, (int)y))){
Circle cir = new Circle();
cir.setFill(Color.web("rgba(255, 235, 138, .3)"));
cir.setCenterX(x);
cir.setCenterY(y);
cir.setRadius(windowHeight * .025);
highlightedVertices.add(cir);
wrapperPane.getChildren().add(cir);
}
});
}
}
最佳答案
由于 Collection
类,您正在使用的 Circle
类可以在方法 contains(Object) 中实现方法
,使用逻辑:equals
o)
(o == null ? e == null: o.equals(e));
其中 e
是列表中的当前对象,o
是要比较的对象。
因此,您的 Circle
类将是:
类(class)圈子
public class Circle {
private double centerX; //Asumming is a double
private double centerY; //Asumming is a double
private double radius;
// +getters and setters
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (o instance of Circle) {
Circle paramCircle = (Circle) o;
if (paramCircle.getCenterX() == this.centerX && paramCircle.getCenterY() == this.centerY && paramCircle.getRadius() == this.radius) {
return true;
}
return false;
}
}
关于java - 检查arraylist中是否存在具有特定坐标的圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42008047/